【问题标题】:Preventing the default for a passive default防止默认为被动默认
【发布时间】:2019-04-18 10:18:40
【问题描述】:

我试图阻止默认的滚动行为。

最初我尝试过这个,来自theseanswers

$(window).on('mousewheel DOMMouseScroll', wheel);

function wheel(e) {
    e.preventDefault()
}

这导致了这个错误: [Intervention] Unable to preventDefault inside passive event listener due to target being treated as passive. See <URL>

当然。我找到了thesequestions 并添加了他们的解决方案。

$(window).on('mousewheel DOMMouseScroll', wheel, {passive:false});

但是现在我收到以下错误:

Uncaught TypeError: ((p.event.special[l.origType] || {}).handle || l.handler).apply is not a function at dispatch (0dfbbab736b8.js:formatted:1850) at h (0dfbbab736b8.js:formatted:1685)

The only related question I could find 只是告诉我删除{passive:false},这意味着我又回到了原来的问题上。

我做错了什么?如何防止此处出现默认行为?

【问题讨论】:

    标签: javascript dom-events


    【解决方案1】:

    看起来像是一个不起眼的 jQuery 错误 - 为什么不直接放弃 jQuery 并使用内置事件呢?

    window.addEventListener('wheel', e => e.preventDefault(), { passive: false });
    body {
      height: 2000px;
    }

    【讨论】: