我正在尝试使用javascript获取滚动方向。
我的代码可在除Firefox之外的所有浏览器中使用。
以下是该方向的代码。

var wheelDirection = function(evt){
if (!evt) evt = event;
return (evt.detail<0) ? 1 : (evt.wheelDelta>0) ? 1 : -1;
};

var direction = wheelDirection();


以下是我尝试使用Greensock js库进行滚动的鼠标滚轮缓和的完整代码...它适用于除Firefox之外所有浏览器的滚动浏览器,即使Firefox滚动时滚动也会不断向下。用于检测支持触摸的设备的js ...我只是不知道问题出在哪里...请-没有jQuery解决方案。我想要纯JavaScript ...

if (feature.touch) {
  //touch supported
} else {
  //touch not supported
  function domLoad() {

    scrollTime = 0.6;
    scrollDistance = 360;

    function scroll(event) {

      event.preventDefault();

      var wheelDirection = function(evt){
        if (!evt) evt = event;
        return (evt.detail<0) ? 1 : (evt.wheelDelta>0) ? 1 : -1;
      };

      var direction = wheelDirection();
      console.log(direction);
      var scrollTop = window.pageYOffset;
      var finalScroll = scrollTop - parseInt(direction*scrollDistance);

      TweenMax.to(window, scrollTime, {
        scrollTo : { y:finalScroll, easing:Sine.easeOut ,autoKill:true }, overwrite: 10
      });

    }

    window.addEventListener("wheel", scroll);

  };

  window.addEventListener("DOMContentLoaded", domLoad);

};

最佳答案

这可能值得尝试,我已经在几个项目中使用了它,并且看起来效果不错。

var scroll = e.originalEvent.deltaY * -1 || e.originalEvent.wheelDelta || e.originalEvent.detail * -1;


从哪个运行:

.on("mousewheel DOMMouseScroll")


然后,您将必须使用应该为int的scroll var检查方向。

我通过console.log找到了滚动事件值并浏览了事件数据,但是您需要在这样做时检查支持。

关于javascript - 无法使用JavaScript在Firefox中获取鼠标滚轮滚动方向,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52409531/

10-12 12:58
查看更多