监控页面的滚动事件(上滚/下滚)

注:以下方法是根据框架设计的,但也可以理解借鉴

公共接口

$(function() {    // js部分开始

    var scrolling =false;     //判断到底了

    functionisBottom() {

        var scrollTop =0;

        if ( document.documentElement  && document.documentElement.scrollTop ) {

         scrollTop = document.documentElement.scrollTop;

    }else if ( document.body ) {

        scrollTop =document.body.scrollTop;//滚动条高度

    }

     var clientHeight =document.documentElement.clientHeight;  //浏览器高度

    var scrollHeight = Math.max( document.body.scrollHeight , document.documentElement.scrollHeight );  //内容区域的高度

    return Math.abs(scrollTop + clientHeight - scrollHeight) <=50;   //高度差在50内可以判断到底了

}

    //判断到顶部了

    functionisTop(){

        var scrollTop =0;

        if ( document.documentElement && document.documentElement.scrollTop) {

            scrollTop =document.documentElement.scrollTop;

        }  else if ( document.body) {

              scrollTop =document.body.scrollTop;

         }

         return scrollTop==0;

  }

 functiononFinish() {

     scrolling =false;

   }

functiononScroll() {   //定义当前滚动的区域

    var scrollTop =me._param.config.scroller

        ?jQuery(me._param.config.scroller).scrollTop()    //自己设置的滚动区域

        : jQuery(document).scrollTop();    //浏览器的滚动区域

    if(scrollTop >0&& !scrolling &&isBottom()) {

        scrolling =true;

    try{

        //找到当前页面的onScroll方法

        var scrollEvent =me.control().onScroll;    //当前页面的onScroll方法,me.control() 是当前页面

        if( scrollEvent ) scrollEvent(onFinish,true);    //这里的true和下面的false分别是用来表示滚到底部(true)还是顶部(false)了

        else onFinish();

       }catch(e) {

            onFinish();

         }

  }

    if(scrollTop ==0&& !scrolling &&isTop()) {

    scrolling =true;

    try{

         //找到当前页面的onScroll方法

         var scrollEvent = me.control().onScroll;

         if( scrollEvent )  scrollEvent(onFinish,false);

        else onFinish();

     }catch(e) {

         onFinish();

        }

   }

}

window.addEventListener("scroll",onScroll,false);

});

某页面的js部分

onScroll:function(finished,isBottom) {   //这里初次调用可以直接赋值参数,比如 onScroll ( null , true) 设置首次下滚

     //如果上滚则不能进行下面的一系列操作

     if(!isBottom){  //这里对上滚刷新做了限制

         finished && finished();

        return;

    }

    if( that.$scope.count!=null&& that.$scope.userMsgList.length>= that.$scope.count) {   //判断

         finished && finished();

        return;

    }

   Util.ajax({    //ajax请求

       method:"POST",

      data:{ },

      url:Util.getApiUrl("")

      },function(data) {

      // 数据返回后的操作

     finished && finished();

},true);

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 以下是常用的代码收集,学习用。转自豪情博客园 1. PC - js 返回指定范围的随机数(m-n之间)的公式 re...
    自由加咖啡阅读 1,027评论 0 1
  • Window和document对象的区别 window对象window对象表示浏览器中打开的窗口window对象是...
    FConfidence阅读 2,259评论 0 5
  • 1.第一种方式:锚链接 优点:简单快速,没有兼容性问题 缺点: 视觉上不够直观,用户体验不太好 2.js的方式: ...
    love2013阅读 809评论 0 0
  • offset家族三大家族和一个事件对象三大家族(offset/scroll/client)事件对象/event ...
    Yuann阅读 984评论 0 5
  • 6:00起床写分享 一直写到上午10:30 上午感召海星非常感谢王剑从昨天晚上一直开导我到今天上午11:00,从昨...
    书恒被从名了阅读 343评论 0 0