如何区分鼠标滚动和 JavaScript 中以编程方式滚动?

发布于 2024-12-01 11:31:47 字数 387 浏览 1 评论 0原文

我通过更改 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

还如梦归 2024-12-08 11:31:47

您可以使用 .hover(): 函数在鼠标悬停在滚动框元素上时停止滚动:

http://jsfiddle.net/bGHAH/1/

setInterval(function(){
    if(!mouseover)
    {
       $('#scrollbox').scrollLeft($('#scrollbox').scrollLeft()+1);
    }
}, 50);

var mouseover = false;
$('#scrollbox').hover(function(){
    mouseover = true;
},function(){
    mouseover = false;    
});

编辑

基于你的评论我设法找到了一个jquery来自以下站点的插件:jquery 的特殊滚动事件

该插件包含一个事件,该事件尝试根据最后一个滚动步骤和进行检查的时间之间经过的时间段来确定滚动是否已停止。

为了让它发挥作用,我需要将时间间隔减慢到刚好超过插件使用的延迟(经计算为 310 毫秒)。这样做意味着我必须增加滚动步骤以保持其明显移动。

这是链接:

http://jsfiddle.net/EWACn/1/

这是代码:

var stopAutoScroll = false;

$(document).ready(function(){

setInterval(function(){
    if(!stopAutoScroll)
    {
       $('#status').html('scrolling');
       $('#scrollbox').scrollLeft($('#scrollbox').scrollLeft()+10);
    }else{
       $('#status').html('not scrolling');
    }
}, 310);

$('#scrollbox').bind('scrollstart', function(e){
    stopAutoScroll = true;
});

$('#scrollbox').bind('scrollstop', function(e){
    stopAutoScroll = false;
});

});

希望这有帮助。

You could use the .hover(): function to stop the scrolling when the mouse is over the scrollbox element:

http://jsfiddle.net/bGHAH/1/

setInterval(function(){
    if(!mouseover)
    {
       $('#scrollbox').scrollLeft($('#scrollbox').scrollLeft()+1);
    }
}, 50);

var mouseover = false;
$('#scrollbox').hover(function(){
    mouseover = true;
},function(){
    mouseover = false;    
});

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:

var stopAutoScroll = false;

$(document).ready(function(){

setInterval(function(){
    if(!stopAutoScroll)
    {
       $('#status').html('scrolling');
       $('#scrollbox').scrollLeft($('#scrollbox').scrollLeft()+10);
    }else{
       $('#status').html('not scrolling');
    }
}, 310);

$('#scrollbox').bind('scrollstart', function(e){
    stopAutoScroll = true;
});

$('#scrollbox').bind('scrollstop', function(e){
    stopAutoScroll = false;
});

});

Hope this helps.

桃扇骨 2024-12-08 11:31:47

对于 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;

白云不回头 2024-12-08 11:31:47

另一种选择是拥有一个外部标志,您可以在编程滚动之前设置该标志,然后重置后记。如果触发了滚动事件并且未设置此标志,您就知道用户负责并可以采取相应的操作。

不幸的是,虽然这是独立于浏览器并且易于阅读的,但它可能会让您相信某些用户滚动是程序化的。不过,我认为这种情况的发生率很小,并且可能值得,具体取决于您正在编写的应用程序。

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.

探春 2024-12-08 11:31:47

对于大多数现代浏览器,尝试 wheel 事件

当旋转指点设备(通常是鼠标)的滚轮按钮时,会触发滚轮事件。

Try wheel event, for most modern browsers

The wheel event is fired when a wheel button of a pointing device (usually a mouse) is rotated.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文