【发布时间】:2023-03-31 18:10:01
【问题描述】:
下面是我的 javascript 代码。 previousPage 和 NextPage 与 HTML 按钮元素一起正常工作。 但我不确定为什么 addEventListener('wheel') 不起作用。所以我插入了一些console.log,然后我意识到var delta 是正确的。但是,我猜'if (delta ~ ~' area...
const pageList = document.querySelectorAll('.verticalPaging');
const previousPage = document.querySelector('#previousScroller');
const nextPage = document.querySelector('#nextScroller');
const idlePeriod = 100;
const animationDuration = 1000;
let lastAnimation = 0;
let pageIndex = 0;
previousPage.addEventListener('click', ()=> {
if (pageIndex < 1) return;
pageIndex--;
pageList.forEach((section, i) => {
if (i === pageIndex) {
section.scrollIntoView({behavior: "smooth"});
}
});
});
nextPage.addEventListener('click', () => {
if (pageIndex > 4) return;
pageIndex++;
pageList.forEach((section, i) => {
if (i === pageIndex) {
section.scrollIntoView({behavior: "smooth"});
}
})
});
document.addEventListener('wheel', event => {
var delta = event.wheelDelta;
var timeNow = new Date().getTime();
// Cancel scroll if currently animating or within quiet period
if(timeNow - lastAnimation < idlePeriod + animationDuration) {
event.preventDefault();
return;
}
if (delta < 0) {
var event = new Event('click');
nextPage.dispatchEvent(event);
} else {
var event = new Event('click');
previousPage.dispatchEvent(event);
}
lastAnimation = timeNow;
},{passive: false});
【问题讨论】: