2017-06-22 135 views
1

我有一个绑定到mousewheel的事件,我在其中使用该事件进行一些计算,我希望能够使用相同的函数来执行触摸设备的相同计算。用触摸设备触发mousewheel事件

我的代码是这样的:

$('#element').bind("mousewheel DOMMouseScroll", function(event) { 
      event.preventDefault(); 
      if (event.originalEvent.wheelDelta > 0 || event.originalEvent.detail < 0) 
       //do stuff 
      else { 
       //do other stuff 
      } 
     }); 

编辑:由于从评论中我明白,我是不完全清楚,我的目标是获得的值等“wheelDelta”,使我认识到,如果用户正在滚动(touchmoving)向上或向下

+0

没有'触摸设备的mousewheel'事件。也许你应该一起使用'mousemove'和'touchmove'? –

+0

你可以试试我的插件[jquery-do-scroll](https://github.com/WashingtonGuedes/jquery-do-scroll)。它适用于鼠标,触摸和轮子事件。 –

回答

0

这可能不是合适的解决方案,但您可以这样做。

var lastScroll = 0; 

    $('#element').on("scroll", function() { 
    var st = $(this).scrollTop(); 
    if (st > lastScroll){ 
     // downscroll code 
     //$(this).trigger("mousewheeldown"); 
    } else { 
     // upscroll code 
     //$(this).trigger("mousewheelup"); 
    } 
    lastScroll = st; 
    $(this).trigger("mousewheel"); 
    }); 

我希望帮助:)

+0

他们为什么要这样做? –

+0

他基本上可以检测到一个用户生成的事件并手动触发另一个。根据我的理解,这基本上就是他想要的。 –

+0

有没有点击我的代码我不明白为什么我会尝试像你建议 – Plastic