基于iscroll.js的上滑加载更多下拉刷新

有两种可以选择

1.如果需要下拉上拉有提示的可以下载查看以下案例:https://github.com/fongdaBoy/pullupPulldownDemohttps://github.com/fongdaBoy/pullupPulldownDemo

2.不需要提示 或者自己写 但是样式更多 包括所有滑动案例等:

https://github.com/cubiq/iscrollhttps://github.com/cubiq/iscroll以下是我根据1改进后的代码----------------------------------------------------------------------------------

1.下载引用jquery.js和iscroll.js文件

<script type="application/javascript" src="../js/iscroll.js"></script>  <!-- 第三方的插件 可以实现上拉加载下拉刷-->
        <script type="application/javascript" src="../js/jquery-3.4.1.min.js"></script><!-- 第三方插件 jquery -->

2.js中:

    // -------------------------------------------------滑动方法------------------------------------------------
            var myScroll, pullDownEl, pullDownOffset, pullUpEl, pullUpOffset, generatedCount = 0;
            
            // 下拉重新加载页面
            function pullDownAction () {
                window.location.reload();//刷新
                setTimeout(function () {
                    
                    myScroll.refresh();  //数据加载完成后,调用界面更新方法 Remember to refresh when contents are loaded (ie: on ajax completion)
                }, 100); 
            }
            
            // 上拉加载更多方法
            function pullUpAction () {
                if(haveMoreDataflag) {
                    ajaxpage1(start);
                }
                
                setTimeout(function () {
        
                    myScroll.refresh();  // 数据加载完成后,调用界面更新方法 Remember to refresh when contents are loaded (ie: on ajax completion)
                  
                }, 100); 
            }
            
             // 初始化iScroll控件
            function loaded() {
             pullDownEl = document.getElementById('pullDown');
             pullDownOffset = pullDownEl.offsetHeight;
             pullUpEl = document.getElementById('pullUp'); 
             pullUpOffset = pullUpEl.offsetHeight;
            
             myScroll = new iScroll('wrapper', {
              scrollbarClass: 'myScrollbar', /* 重要样式 */
              useTransition: false, /* 此属性不知用意,本人从true改为false */
              topOffset: pullDownOffset,
              // 拖动幅度比较小时候出发的方法
              onRefresh: function () {
               if (pullDownEl.className.match('loadingPart')) {
                pullDownEl.className = '';
                pullDownEl.querySelector('.pullDownLabel').innerHTML = '下拉重新加载...';
               } else if (pullUpEl.className.match('loadingPart')) {
                pullUpEl.className = '';
                pullUpEl.querySelector('.pullUpLabel').innerHTML = '';
               }
              },
              // 拖动距离比较大的出发的方法
              onScrollMove: function () {
               if (this.y > 5 && !pullDownEl.className.match('flip')) {
                pullDownEl.className = 'flip';
                pullDownEl.querySelector('.pullDownLabel').innerHTML = '松手开始更新...';
                this.minScrollY = 0;
               } else if (this.y < 5 && pullDownEl.className.match('flip')) {
                pullDownEl.className = '';
                pullDownEl.querySelector('.pullDownLabel').innerHTML = '下拉重新加载...';
                this.minScrollY = -pullDownOffset;
               } else if (this.y < (this.maxScrollY - 5) && !pullUpEl.className.match('flip')) {
                pullUpEl.className = 'flip';
                pullUpEl.querySelector('.pullUpLabel').innerHTML = '';
                this.maxScrollY = this.maxScrollY;
               } else if (this.y > (this.maxScrollY + 5) && pullUpEl.className.match('flip')) {
                pullUpEl.className = '';
                pullUpEl.querySelector('.pullUpLabel').innerHTML = '';
                this.maxScrollY = pullUpOffset;
               }
              },
              // 放手后的方法
              onScrollEnd: function () {
                  
               if (pullDownEl.className.match('flip')) {
                pullDownEl.className = 'loadingPart';
                pullDownEl.querySelector('.pullDownLabel').innerHTML = '加载中...';    
                pullDownAction(); // Execute custom function (ajax call?)
               } else if (pullUpEl.className.match('flip')) {
                    pullUpEl.className = 'loadingPart';
                    if(haveMoreDataflag){  
                        pullUpEl.querySelector('.pullUpLabel').innerHTML = '';    
                        pullUpAction(); // Execute custom function (ajax call?)
                    }else{
                        pullUpEl.querySelector('.pullUpLabel').innerHTML = '数据已全部加载完毕';   
                    }
                
               }
              }
             });
            
             setTimeout(function () { document.getElementById('wrapper').style.left = '0'; }, 800);
            }
            
            // -------------------------------------------------页面初始化------------------------------------------------
            var start = 1
            var pagesize = 8;
            var haveMoreDataflag = true;
            $(function() {
                ajaxpage1(start);
                setTimeout(()=>{
                    //初始化绑定iScroll控件
                    document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);
                    document.addEventListener('DOMContentLoaded', loaded, false);
                    myScroll.refresh();//很重要
                },100)
            });

注:在1案例中发现可能会有滑动不到底部的情况,思考了很久,后来发现需要数据加载完之后再进行scroll刷新(高度会刷新)就会正常. 我采用了定时器触发刷新方法.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iScroll 可以很方便地实现上拉加载下拉刷新,下面是一个简单的示例代码: ```html <div id="wrapper"> <div id="scroller"> <ul> <li>1</li> <li>2</li> <li>3</li> ... </ul> </div> <div id="pullDown"> <span>下拉刷新</span> </div> <div id="pullUp"> <span>上拉加载更多</span> </div> </div> ``` ```javascript var myScroll = new IScroll('#wrapper', { probeType: 3, mouseWheel: true, scrollbars: true, fadeScrollbars: true, interactiveScrollbars: true, shrinkScrollbars: 'scale', click: true, pullDownRefresh: true, pullUpLoad: true }); // 下拉刷新 myScroll.on('scroll', function() { if (this.y > 30) { $('#pullDown span').html('松开刷新'); } else { $('#pullDown span').html('下拉刷新'); } }); myScroll.on('scrollEnd', function() { if (this.y > 30) { // 执行下拉刷新操作 setTimeout(function() { myScroll.refresh(); }, 1000); } }); // 上拉加载 myScroll.on('scroll', function() { if (this.y < (this.maxScrollY - 30)) { $('#pullUp span').html('松开加载'); } else { $('#pullUp span').html('上拉加载更多'); } }); myScroll.on('scrollEnd', function() { if (this.y < (this.maxScrollY - 30)) { // 执行上拉加载操作 setTimeout(function() { myScroll.refresh(); }, 1000); } }); ``` 上面的代码中,我们在 iScroll 的配置中开启了 `pullDownRefresh` 和 `pullUpLoad` 两个选项,然后监听 `scroll` 和 `scrollEnd` 事件,根据滚动的位置来判断是否需要触发下拉刷新上拉加载操作。在触发操作后,我们可以通过 setTimeout 来模拟异步加载数据的过程,然后调用 `myScroll.refresh()` 来更新 iScroll 的状态。这样就可以实现简单的上拉加载下拉刷新功能了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值