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

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(如1.9)的话需要将handle改为dispatch
查看demo:http://www.shsc-valve.com

以下为实计应用代码

      $("document").ready(function() {
        $(".top").click(function() {
          var nierong = $(".top>a").text();
          //console.log(nierong);
          if (nierong == "返回底部") {
            $(".top").html('<a href="#top">返回顶部</a>');
          } else {
            $(".top").html('<a href="#bottom">返回底部</a>');
          }
        });

      });
      (function() {
        jQuery(window).bind("scrollstart", function() {
          //console.log("start");
        });

        jQuery(window).bind("scrollstop", function(e) {
          //console.log(e);
          if(e.type = 'scrollstop'){
            //监听滚轴停止
            var $scr = $(window).scrollTop();
            var $top_bot = $(".top>a").text(); 
            //console.log($top_bot);
            if ($scr < 300 && $top_bot !='返回底部') {
                $(".top").html('<a href="#bottom">返回底部</a>');
                console.log("底部");
              } else if ($scr >= 500 && $top_bot !='返回顶部') {
                $(".top").html('<a href="#top">返回顶部</a>');
                console.log("顶部");
              }

          }
        });
      })();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值