仅在滚动时运行而不在页面加载上运行

时间:2014-04-01 21:10:42

标签: javascript jquery jquery-ui javascript-events

我有这段代码,

<script type="text/javascript">
    $(document).ready( function() {

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

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

            if(now - l > 400 && !scrolling ){
                $(this).trigger('scrollStart');
                l = now;
                console.log("scrolling ");
            }

            clearTimeout(t);
            t = setTimeout(function(){
                if (scrolling){
                    console.log("scrolling stop ");
                    $(window).trigger('scrollEnd');
                }

            }, 300);
        });

        $(window).bind('scrollStart', function(){
            scrolling = true;
            console.log("scrolling 1");

        });

        $(window).bind('scrollEnd', function(){
            scrolling = false;
            console.log("scrolling 2");


        });


    });
</script>

问题是当页面首次加载时,它会立即注册滚动,即使我还没有滚动

scrolling 1 
scrolling  
scrolling stop  
scrolling 2

我希望它在实际滚动上注册滚动而不是页面/文档加载/就绪

0 个答案:

没有答案
相关问题