如何区分鼠标滚动和 JavaScript 中以编程方式滚动?
我通过更改 Javascript 中的 scrollLeft
属性来滚动溢出的 DIV 内容:
setInterval(function(){
$('#scrollbox').scrollLeft($('#scrollbox').scrollLeft()+1);
}, 50);
但是,我想在用户使用鼠标自行滚动内容时立即停止此操作。我尝试使用滚动事件来检测这一点
$('#scrollbox').scroll(function(){...});
,但是,我上面的自动滚动也会触发该事件。我如何区分这一点并仅对用户启动的滚动做出反应? (或者:如何阻止上面的代码触发滚动事件?这也可以解决问题)
I am scrolling an overflowing DIV's content by changing the scrollLeft
property in Javascript:
setInterval(function(){
$('#scrollbox').scrollLeft($('#scrollbox').scrollLeft()+1);
}, 50);
However, I want to stop this as soon as the user scrolls the content themselves, using the mouse. I tried to detect this using the scroll event
$('#scrollbox').scroll(function(){...});
however, my automatic scrolling above also triggers that event. How can I distinguish this and only react to user-initiated scrolling? (or: how can I stop the above code from firing a scroll event? That would also do the trick)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 .hover(): 函数在鼠标悬停在滚动框元素上时停止滚动:
http://jsfiddle.net/bGHAH/1/
编辑
基于你的评论我设法找到了一个jquery来自以下站点的插件:jquery 的特殊滚动事件。
该插件包含一个事件,该事件尝试根据最后一个滚动步骤和进行检查的时间之间经过的时间段来确定滚动是否已停止。
为了让它发挥作用,我需要将时间间隔减慢到刚好超过插件使用的延迟(经计算为 310 毫秒)。这样做意味着我必须增加滚动步骤以保持其明显移动。
这是链接:
http://jsfiddle.net/EWACn/1/
这是代码:
希望这有帮助。
You could use the .hover(): function to stop the scrolling when the mouse is over the scrollbox element:
http://jsfiddle.net/bGHAH/1/
Edit
Based on your comments I managed to find a jquery plugin from the following site: special scroll events for jquery.
This plugin contains an event which attempts to determine whether scrolling has stopped based on the period of time that has elapsed between the last scroll step and the time the check was made.
To get this to work I needed to slow your interval to just over the latency used by the plugin which worked out to be 310 milliseconds. Doing this meant I had to increase the scroll step to keep it visibly moving.
Here is the link:
http://jsfiddle.net/EWACn/1/
and here is the code:
Hope this helps.
对于 FF (Mozilla):
document.addEventListener('DOMMouseScroll', handler, false);
对于 IE、Opera 和 Chrome:
document.onmousewheel = handler;
For FF (Mozilla):
document.addEventListener('DOMMouseScroll', handler, false);
For IE, Opera and Chrome:
document.onmousewheel = handler;
另一种选择是拥有一个外部标志,您可以在编程滚动之前设置该标志,然后重置后记。如果触发了滚动事件并且未设置此标志,您就知道用户负责并可以采取相应的操作。
不幸的是,虽然这是独立于浏览器并且易于阅读的,但它可能会让您相信某些用户滚动是程序化的。不过,我认为这种情况的发生率很小,并且可能值得,具体取决于您正在编写的应用程序。
Another option is to have an external flag that you can set prior to the programmatic scrolling, and then reset afterwords. If the scroll event is fired and this flag isn't set you know that the user is responsible and can act accordingly.
Unfortunately while this is browser independent and easy to read it could lead you to believe that some user scrolls are programmatic ones. However I would think the occurrences of this is small and may be worth it depending on the app you are writing.
对于大多数现代浏览器,尝试 wheel 事件
Try wheel event, for most modern browsers