页面停止滚动时触发功能

时间:2022-02-13 23:55:49

How can I trigger a function when the browser window stops scrolling? Either by mouse wheel, click, space bar or arrow key? Is there any event for such action? I have tried to search online but couldn't get any solution. I'm fine with a jQuery solution.

如何在浏览器窗口停止滚动时触发功能?通过鼠标滚轮,单击,空格键或箭头键?这样的行动有什么事吗?我试图在线搜索,但无法得到任何解决方案。我对jQuery解决方案很好。

1 个解决方案

#1


11  

There's no "event" but you could make your own, like this:

没有“事件”,但你可以制作自己的,如下:

$(function() {
  var timer;
  $(window).scroll(function() {
    clearTimeout(timer);
    timer = setTimeout(function() {
      $(window).trigger("scrollStop");
    }, 250);
  });
});

Then you could bind to it, like this:

然后你可以绑定它,像这样:

$(window).bind("scrollStop", function() {
  alert("No one has scrolled me in 250ms, where's the love?");
});

This creates an event, there's no "stop" for this, but you can define your own... in this case "stop" is defined as "hasn't scrolled in 250ms", you can adjust the timer to your liking, but that's the idea.

这会创建一个事件,对此没有“停止”,但您可以定义自己的......在这种情况下,“停止”定义为“未在250毫秒内滚动”,您可以根据自己的喜好调整计时器,但是这就是主意。

Also, if you're just doing one thing there's no need for an event, just put your code where I'm calling $(window).trigger("scrollStop") and it'll run n milliseconds after the scroll stops.

另外,如果你只做一件事就不需要事件,只需将你的代码放在我调用$(window).trigger(“scrollStop”)的地方,它就会在滚动停止后运行n毫秒。

#1


11  

There's no "event" but you could make your own, like this:

没有“事件”,但你可以制作自己的,如下:

$(function() {
  var timer;
  $(window).scroll(function() {
    clearTimeout(timer);
    timer = setTimeout(function() {
      $(window).trigger("scrollStop");
    }, 250);
  });
});

Then you could bind to it, like this:

然后你可以绑定它,像这样:

$(window).bind("scrollStop", function() {
  alert("No one has scrolled me in 250ms, where's the love?");
});

This creates an event, there's no "stop" for this, but you can define your own... in this case "stop" is defined as "hasn't scrolled in 250ms", you can adjust the timer to your liking, but that's the idea.

这会创建一个事件,对此没有“停止”,但您可以定义自己的......在这种情况下,“停止”定义为“未在250毫秒内滚动”,您可以根据自己的喜好调整计时器,但是这就是主意。

Also, if you're just doing one thing there's no need for an event, just put your code where I'm calling $(window).trigger("scrollStop") and it'll run n milliseconds after the scroll stops.

另外,如果你只做一件事就不需要事件,只需将你的代码放在我调用$(window).trigger(“scrollStop”)的地方,它就会在滚动停止后运行n毫秒。