【发布时间】:2020-05-07 07:18:50
【问题描述】:
当我将页面滚动到特殊块时,我使用 javascript 禁用鼠标滚轮。 我使用了很多方法,但它们都在 Firefox 中工作,而不是在 chrome 中。 对于 chrome,我找到了一种带有 preventDefault 的特殊方法。 但我不知道如何启用它们。 这是代码:
const options = {
rootMargin: "-400px"
};
const observer = new IntersectionObserver(function (entries, observer) {
entries.forEach(entry => {
console.log(entry.isIntersecting);
if (entry.isIntersecting === true) {
document.querySelector('body').classList.add('fixed');
document.body.addEventListener('wheel', function (e) {
e.preventDefault();
}, {passive: false});
$('html, body').animate({
scrollTop: scrollCenter
}, 1000);
setTimeout(function () {
document.querySelector('body').classList.remove('fixed');
document.body.addEventListener('wheel', function (e) {
// override prevented flag to prevent jquery from discarding event
e.isDefaultPrevented = function () {
return false;
}
});
}, 1000);
}
});
}, options);
observer.observe(produtsBlock);
感谢您的帮助。
【问题讨论】:
标签: javascript mousewheel preventdefault