如何区分通过鼠标滚动和通过在javascript/jQuery中拖动窗口滚动来完成滚动 [英] How to differentiate between Scrolling by Mouse and scrolling done by dragging a window's scroll in javascript/jQuery

查看:69
本文介绍了如何区分通过鼠标滚动和通过在javascript/jQuery中拖动窗口滚动来完成滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于滚动实现的功能. 这是小片段::

I have a functionality to implement based on scrolling. Here is small snippet ::

$(window).bind('scroll',function(event)
{
  console.log(event.type);
  //Task to do
}

在这里我想区分是通过mouseScroll还是通过拖动滚动来完成绑定. 通过检查event.type,两个都将"scroll"作为事件类型返回给我.

Here I want to differentiate that whether the binding is done by mouseScroll or by dragging a scroll. By inspecting the event.type both are returning me "scroll" as event type.

推荐答案

您可以使用

You could use the wheel DOM event to detect mousewheel events:

var isMouseScroll = false;

window.addEventListener('wheel',function(e)
{
  console.log('mouse wheel');
  isMouseScroll = true;
});

window.addEventListener('scroll',function(e)
{
  if(!isMouseScroll) {
    console.log('scroll');
  }

  isMouseScroll = false;
});

JSFiddle

注意-不要将转盘与已弃用的非标准 mousewheel 事件.

Note - don't confuse wheel with the deprecated, non-standard mousewheel event.

这篇关于如何区分通过鼠标滚动和通过在javascript/jQuery中拖动窗口滚动来完成滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆