【发布时间】:2017-05-18 16:52:22
【问题描述】:
我需要在鼠标位于 div 或 div 内的元素上时捕获鼠标滚轮事件,并防止页面滚动。
本论坛有一些解决方案(如this 和this),但页面一直滚动。
解决方案必须是纯 JavaScript(即没有 jQuery 等)。
【问题讨论】:
标签: javascript
我需要在鼠标位于 div 或 div 内的元素上时捕获鼠标滚轮事件,并防止页面滚动。
本论坛有一些解决方案(如this 和this),但页面一直滚动。
解决方案必须是纯 JavaScript(即没有 jQuery 等)。
【问题讨论】:
标签: javascript
你只需要在捕获事件后取消它。
// This disables the event for the entire document.
// Substitute a reference to the DOM element you wish to
// deal with instead of "document" below.
document.addEventListener("mousewheel", function(evt){
console.log("mousewheel event detected");
evt.preventDefault(); // Cancel the event
evt.stopPropagation() // Don't bubble
});
【讨论】: