jquery mousewheel:检测滚轮何时停止? [英] jquery mousewheel: detecting when the wheel stops?

查看:47
本文介绍了jquery mousewheel:检测滚轮何时停止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Jquery mousewheel 插件,我希望能够检测何时用户已完成使用滚轮.与 stop: 可拖动内容中的事件类似的功能.有人能指出我正确的方向吗?

I'm using Jquery mousewheel plugin and I would like like to be able to detect when the user has finished using the wheel. Similar functionality as the stop: event in the draggable stuff. Can somebody point me to the right direction?

推荐答案

这里真的没有停止"事件——当你滚动时你会得到一个事件,所以每次鼠标滚轮事件发生时事件被触发...当没有任何事件时,您将不会收到任何事件,并且您的处理程序不会被触发.

There's no "stop" event here really - you get an event when you do scroll, so every time a mousewheel event happens the event is triggered...when there's nothing you'll get no events and your handler won't be firing.

但是,您可以检测到用户在 250 毫秒内没有使用它的时间,如下所示:

You ca however detect when the user hasn't used it in say 250ms, like this:

$("#myElem").mousewheel(function() {
  clearTimeout($.data(this, 'timer'));
  $.data(this, 'timer', setTimeout(function() {
     alert("Haven't scrolled in 250ms!");
     //do something
  }, 250));
});

您可以在这里尝试一下,我们所做的只是将超时存储在每个使用 $.data(),如果你使用它再次在该时间用完之前,它会被清除...如果不是,那么您想要运行的任何代码都会触发,用户在您测试的任何时间段内都使用鼠标滚轮完成"了.

You can give it a try here, all we're doing is storing the timeout on each use in using $.data(), if you use it again before that time runs out, it gets cleared...if not then whatever code you wanted to run fires, the user has "finished" using their mousewheel for whatever period of time you're testing for.

这篇关于jquery mousewheel:检测滚轮何时停止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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