我可以在jQuery上声明一个滚动事件的开始和结束的逻辑吗? [英] Can I declare logic on jQuery for the start and end of a scrolling event?

查看:136
本文介绍了我可以在jQuery上声明一个滚动事件的开始和结束的逻辑吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置逻辑,当用户开始滚动页面时,滚动结束后,我该如何实现?

I would like to setup logic for when the user begins scrolling around the page and after the scrolling is finished, how can I accomplish this?

我想避免这意味着我的逻辑将被不必要地被激发

I want to avoid the below as it means my logic will be fired repeatedly unnecessarily

$(window).scroll(function(){
    console.log("scrolling");
});


推荐答案

您可以尝试定义自己的 debounced events 。一个(非常粗糙)的实现将如下所示:

You can try defining you're own debounced events. A (very crude) implementation would look something like this:

var t, l = (new Date()).getTime();

$(window).scroll(function(){
    var now = (new Date()).getTime();

    if(now - l > 400){
        $(this).trigger('scrollStart');
        l = now;
    }

    clearTimeout(t);
    t = setTimeout(function(){
        $(window).trigger('scrollEnd');
    }, 300);
});

请参阅: http://www.jsfiddle.net/yijiang/fGmbe/ 进行现场演示

See: http://www.jsfiddle.net/yijiang/fGmbe/ for a live demo

这篇关于我可以在jQuery上声明一个滚动事件的开始和结束的逻辑吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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