码迷,mamicode.com
首页 > Web开发 > 详细

扩展jquery scroll事件,支持 scroll start 和 scroll stop

时间:2017-07-10 20:15:41      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:插件   nbsp   拖动   滚动事件   滚动条   data   rip   for   class   

参考地址:

http://www.ghugo.com/special-scroll-events-for-jquery/

javascript里有一个事件是滚动事件,只要拖动滚动条,就会触发事件。

用jquery的话,这个事件scroll 可以查看jquery api :http://api.jquery.com/scroll/

但scroll 事件有一个缺陷,就是只能判断滚动条滚动,而不能监控滚动条停止滚动时的事件。

现用jquery扩展一下scroll 事件,新增

不多说,直接上代码实在点。

(function(){
 
    var special = jQuery.event.special,
        uid1 = ‘D‘ + (+new Date()),
        uid2 = ‘D‘ + (+new Date() + 1);
 
    special.scrollstart = {
        setup: function() {
 
            var timer,
                handler =  function(evt) {
 
                    var _self = this,
                        _args = arguments;
 
                    if (timer) {
                        clearTimeout(timer);
                    } else {
                        evt.type = ‘scrollstart‘;
                        jQuery.event.handle.apply(_self, _args);
                    }
 
                    timer = setTimeout( function(){
                        timer = null;
                    }, special.scrollstop.latency);
 
                };
 
            jQuery(this).bind(‘scroll‘, handler).data(uid1, handler);
 
        },
        teardown: function(){
            jQuery(this).unbind( ‘scroll‘, jQuery(this).data(uid1) );
        }
    };
 
    special.scrollstop = {
        latency: 300,
        setup: function() {
 
            var timer,
                    handler = function(evt) {
 
                    var _self = this,
                        _args = arguments;
 
                    if (timer) {
                        clearTimeout(timer);
                    }
 
                    timer = setTimeout( function(){
 
                        timer = null;
                        evt.type = ‘scrollstop‘;
                        jQuery.event.handle.apply(_self, _args);
 
                    }, special.scrollstop.latency);
 
                };
 
            jQuery(this).bind(‘scroll‘, handler).data(uid2, handler);
 
        },
        teardown: function() {
            jQuery(this).unbind( ‘scroll‘, jQuery(this).data(uid2) );
        }
    };
 
})();

可以将上面代码保存到一个文件,这相当于一个插件,呵呵。调用方法如下:

(function(){
    jQuery(window).bind(‘scrollstart‘, function(){
        console.log("start");
    });
 
    jQuery(window).bind(‘scrollstop‘, function(e){
        console.log("end");
    });
 
})();

 

扩展jquery scroll事件,支持 scroll start 和 scroll stop

标签:插件   nbsp   拖动   滚动事件   滚动条   data   rip   for   class   

原文地址:http://www.cnblogs.com/wang715100018066/p/7147208.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!