好吧..

$(window).scroll(function()
{
    $('.slides_layover').removeClass('showing_layover');
    $('#slides_effect').show();
});

I can tell when someone is scrolling from what I understand. So with that I am trying to figure out how to catch when someone has stopped. From the above example you can see I am removing a class from a set of elements while the scrolling is occurring. However, I want to put that class back on when the user stops scrolling.

The reason for this is I am intent on having a layover show while the page is scrolling to give the page a special effect I am attempting to work on. But the one class I am trying to remove while scrolling conflicts with that effect as its a transparency effect to some nature.

推荐答案

$(window).scroll(function() {
    clearTimeout($.data(this, 'scrollTimer'));
    $.data(this, 'scrollTimer', setTimeout(function() {
        // do something
        console.log("Haven't scrolled in 250ms!");
    }, 250));
});

Update

I wrote an extension to enhance jQuery's default on-event-handler. It attaches an event handler function for one or more events to the selected elements and calls the handler function if the event was not triggered for a given interval. This is useful if you want to fire a callback only after a delay, like the resize event, or such.

It is important to check the github-repo for updates!

https://github.com/yckart/jquery.unevent.js

;(function ($) {
    var on = $.fn.on, timer;
    $.fn.on = function () {
        var args = Array.apply(null, arguments);
        var last = args[args.length - 1];

        if (isNaN(last) || (last === 1 && args.pop())) return on.apply(this, args);

        var delay = args.pop();
        var fn = args.pop();

        args.push(function () {
            var self = this, params = arguments;
            clearTimeout(timer);
            timer = setTimeout(function () {
                fn.apply(self, params);
            }, delay);
        });

        return on.apply(this, args);
    };
}(this.jQuery || this.Zepto));

Use it like any other on or bind-event handler, except that you can pass an extra parameter as a last:

$(window).on('scroll', function(e) {
    console.log(e.type + '-event was 250ms not triggered');
}, 250);

http://yckart.github.com/jquery.unevent.js/

(这个演示使用的是resize,而不是scroll,但谁在乎呢?!)

Jquery相关问答推荐

在正则表达式字符括号中包含连字符?

如何在jQuery中移动表格行?

如何判断是否有任何 JavaScript 事件侦听器/处理程序附加到元素/文档?

删除所有子元素的 CLASS

当ID包含方括号时按ID查找DOM元素?

调用 e.preventDefault() 后提交表单

如何使用 jquery 更改 onclick 事件?

在 jquery 中启用/禁用下拉框

如何将参数传递给 JavaScript 中的匿名函数?

所有选中复选框的 jQuery 数组(按类)

为不同的 node 类型配置jstree右键上下文菜单

jQuery:如何创建一个简单的叠加层?

如何在纯 JavaScript 中模拟鼠标悬停以激活 CSS:悬停?

jQuery: Select 不为空的数据属性?

通过 AJAX MVC 下载 Excel 文件

请求的资源错误中不存在Access-Control-Allow-Origin标头

使用 jQuery Select 空文本输入

一起使用 JQuery-Mobile/Phonegap 的正确方法?

在 Javascript/jQuery 中,(e) 是什么意思?

如何使用 jQuery 的 form.serialize 但排除空字段