【问题标题】:Capturing mouse wheel while preventing page scroll in JavaScript在 JavaScript 中防止页面滚动时捕获鼠标滚轮
【发布时间】:2017-05-18 16:52:22
【问题描述】:

我需要在鼠标位于 div 或 div 内的元素上时捕获鼠标滚轮事件,并防止页面滚动。

本论坛有一些解决方案(如thisthis),但页面一直滚动。

解决方案必须是纯 JavaScript(即没有 jQuery 等)。

【问题讨论】:

    标签: javascript


    【解决方案1】:

    你只需要在捕获事件后取消它。

    // 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
    
    });

    【讨论】:

      猜你喜欢
      • 2022-11-12
      • 2014-02-11
      • 2012-05-05
      • 1970-01-01
      • 2013-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多