javascript - 使用触摸设备触发鼠标滚轮事件

标签 javascript jquery touch mousewheel

我有一个事件绑定(bind)到鼠标滚轮,在其中我使用该事件进行一些计算,我希望能够使用相同的函数从触摸设备执行相同的计算。

我的代码是这样的:

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

编辑:从评论中我知道我并不完全清楚,我的目标是获得像“wheelDelta”这样的值,让我识别用户是否是向上或向下滚动(触摸移动)

最佳答案

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

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

希望有帮助:)

关于javascript - 使用触摸设备触发鼠标滚轮事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44702986/

相关文章:

javascript - 在 Kendo Treeview 中将箭头符号设为加号(+)和减号(-)

javascript - 获取asp :checkbox inside a datalist using jquery的id

javascript - 附加字段中的自动完成

javascript - 如何让此菜单在悬停时保持在​​图像的右上角? - CSS,jQuery

javascript - 如何将鼠标功能更改为在 JS Canvas 中触摸

ios - 如何知道一个 View 是否触及 Ios 中的其他 View

JavaScript 逗号分隔不适用于 switch case

javascript - ReactJS,循环遍历一个项目以及标记上的 if 条件

javascript - 创建后分配对象原型(prototype)

delphi - 在 Windows 的 Delphi 应用程序中检测触摸功能(平板电脑)的最佳实践是什么?