阻止默认滚动事件后,scrollIntoView();在 Firefox 桌面上不能正常工作?
After preventing the default scroll event, scrollIntoView(); doesn't work Properly on Firefox desktop?
在我的场景中,代码应该阻止默认滚动事件并使用 scrollIntoView()
根据滚动方向将用户移动到特定部分。
我用这个方法来防止滚动默认事件
我这样检测方向,
preventDefault(e) {
e.preventDefault();
if(this.waiting == false && this.forceInitialScroll != true) {
if(e.deltaY && e.deltaY > 7) {
this.scrollDirection = 'down';
this.checkScroll()
}else if(e.deltaY && e.deltaY < -7) {
this.scrollDirection = 'up';
this.checkScroll()
}else {
}
}
},
and Im pretty sure that both are working fine.
在阻止滚动并检测滚动方向后,我尝试将用户滚动到这样的部分,
checkScroll() {
let element;
if(this.scrollDirection == 'down' && this.scrollIndex != 4 ) {
element = document.getElementById(`section-${this.scrollIndex+1}`);
}else if(this.scrollDirection == 'up' && this.scrollIndex != 0) {
element = document.getElementById(`section-${this.scrollIndex-1}`);
}
this.waiting = true;
if(element) {
console.log(element)
element.scrollIntoView({
behavior: 'smooth',
block: 'center',
})
}
setTimeout(() => {
if(this.waiting == true) {
this.waiting = false;
this.scrollDirection = null;
}
}, 450)
},
this.waiting
用于防止用户表单一次滚动多个部分。
在 firefox 上,浏览器无法正确滚动用户,尽管它在 chrome.
上工作正常
显然 Firefox event.preventDefault()
的行为是阻止任何类型的用户滚动,甚至阻止 js 手动滚动。
我的问题是通过使用 overflow: hidden;
在 css 而不是 js 中隐藏滚动,然后手动控制滚动而不是阻止默认行为来解决的。
在我的场景中,代码应该阻止默认滚动事件并使用 scrollIntoView()
根据滚动方向将用户移动到特定部分。
我用这个方法来防止滚动默认事件
我这样检测方向,
preventDefault(e) {
e.preventDefault();
if(this.waiting == false && this.forceInitialScroll != true) {
if(e.deltaY && e.deltaY > 7) {
this.scrollDirection = 'down';
this.checkScroll()
}else if(e.deltaY && e.deltaY < -7) {
this.scrollDirection = 'up';
this.checkScroll()
}else {
}
}
},
and Im pretty sure that both are working fine.
在阻止滚动并检测滚动方向后,我尝试将用户滚动到这样的部分,
checkScroll() {
let element;
if(this.scrollDirection == 'down' && this.scrollIndex != 4 ) {
element = document.getElementById(`section-${this.scrollIndex+1}`);
}else if(this.scrollDirection == 'up' && this.scrollIndex != 0) {
element = document.getElementById(`section-${this.scrollIndex-1}`);
}
this.waiting = true;
if(element) {
console.log(element)
element.scrollIntoView({
behavior: 'smooth',
block: 'center',
})
}
setTimeout(() => {
if(this.waiting == true) {
this.waiting = false;
this.scrollDirection = null;
}
}, 450)
},
this.waiting
用于防止用户表单一次滚动多个部分。
在 firefox 上,浏览器无法正确滚动用户,尽管它在 chrome.
显然 Firefox event.preventDefault()
的行为是阻止任何类型的用户滚动,甚至阻止 js 手动滚动。
我的问题是通过使用 overflow: hidden;
在 css 而不是 js 中隐藏滚动,然后手动控制滚动而不是阻止默认行为来解决的。