在Firefox中标准化wheelEvent

时间:2019-01-13 20:20:08

标签: javascript firefox

如何规范Firefox中的窗口滚动事件?

此浏览器在mousewheelChrome浏览器中的Safari上可以正常工作,但在Firefox浏览器中不能正常工作

var normalizeWheelEvent = function(event) {           

   if (!!event.wheelDelta) {

      var wheelDeltaRate = 10;
      event.wheelX = event.wheelDeltaX / wheelDeltaRate;
      event.wheelY = event.wheelDeltaY / wheelDeltaRate;

   }

}; 

target.addEventListener('mousewheel',normalizeWheelEvent, useCapture);

如何在Firefox中更新event以规范mousewheel

1 个答案:

答案 0 :(得分:0)

对于Firefox,您需要使用e.detail,如下所示:

if (!!event.detail) {

    // Similar to your wheelDeltaRate, detailRate will change the speed
    var detailRate = 1.20;
    // let's add some magic for scrolling
    event.wheelX = event.axis == event.HORIZONTAL_AXIS ? (-1 * event.detail) * detailRate : 0;
    event.wheelY = event.axis == event.VERTICAL_AXIS ? (-1 * event.detail) * detailRate : 0;
}

不要忘记使用:

target.addEventListener('DOMMouseScroll',normalizeWheelEvent, useCapture);

让我知道它是否对您有用