找回密码
 立即注册
搜索
热搜: 生活 中国

H5移动端网页上拉及下拉刷新数据

[复制链接]
admin 发表于 2020-11-24 10:50:28 | 显示全部楼层 |阅读模式
  1. 上拉刷新demo
  2. <!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5.         <meta charset="UTF-8">
  6.         <meta name="viewport" content="width=device-width, initial-scale=1">
  7.         <title>Document</title>
  8.         <style type="text/css">
  9.                 html,body,header,div,main,p,span,ul,li{ margin: 0; padding: 0; }
  10.                 #refreshContainer li{ background-color: #eee; margin-bottom: 1px; padding: 20px 10px; }
  11.                 .refreshText{ position: absolute; width: 100%; line-height: 50px; text-align: center; left: 0; top: 0; }
  12.         </style>
  13. </head>
  14. <body>
  15. <main>
  16.         <p class="refreshText"></p>
  17.         <ul id="refreshContainer">
  18.                 <li>111</li>
  19.                 <li>222</li>
  20.                 <li>333</li>
  21.                 <li>444</li>
  22.                 <li>555</li>
  23.                 <li>111</li>
  24.                 <li>222</li>
  25.                 <li>333</li>
  26.                 <li>444</li>
  27.                 <li>555</li>
  28.                 <li>111</li>
  29.                 <li>222</li>
  30.                 <li>333</li>
  31.                 <li>444</li>
  32.                 <li>555</li>
  33.         </ul>
  34. </main>

  35. <script type="text/javascript">
  36.         (function(window) {
  37.                 var _element = document.getElementById('refreshContainer'),
  38.                   _refreshText = document.querySelector('.refreshText'),
  39.                   _startPos = 0,
  40.                   _transitionHeight = 0;

  41.                 _element.addEventListener('touchstart', function(e) {
  42.                         console.log('初始位置:', e.touches[0].pageY);
  43.                         _startPos = e.touches[0].pageY;
  44.                         _element.style.position = 'relative';
  45.                         _element.style.transition = 'transform 0s';
  46.                 }, false);
  47.                
  48.                 _element.addEventListener('touchmove', function(e) {
  49.                         console.log('当前位置:', e.touches[0].pageY);
  50.                         _transitionHeight = e.touches[0].pageY - _startPos;
  51.                        
  52.                         if (_transitionHeight > 0 && _transitionHeight < 60) {
  53.                                 _refreshText.innerText = '下拉刷新';
  54.                                 _element.style.transform = 'translateY('+_transitionHeight+'px)';

  55.                                 if (_transitionHeight > 55) {
  56.                                   _refreshText.innerText = '释放更新';
  57.                                   _element.append("<li>100</li>");
  58.                                 }
  59.                         }                               
  60.                 }, false);

  61.                 _element.addEventListener('touchend', function(e) {
  62.                         _element.style.transition = 'transform 0.5s ease 1s';
  63.                         _element.style.transform = 'translateY(0px)';
  64.                         _refreshText.innerText = '更新中...';

  65.                         // todo...

  66.                 }, false);
  67.         })(window);
  68. </script>
  69. </body>
  70. </html>
复制代码
  1. 下拉加载更多demo
  2. <!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5.         <meta charset="UTF-8">
  6.         <meta name="viewport" content="width=device-width, initial-scale=1">
  7.         <title>Document</title>
  8.         <style type="text/css">
  9.                 html,body,header,div,main,p,span,ul,li{ margin: 0; padding: 0; }
  10.                 #refreshContainer li{ background-color: #eee; margin-bottom: 1px; padding: 20px 10px; }
  11.                 .refreshText{ line-height: 50px; text-align: center; }
  12.         </style>
  13. </head>
  14. <body>
  15. <main>
  16.         <ul id="refreshContainer">
  17.                 <li>111</li>
  18.                 <li>222</li>
  19.                 <li>333</li>
  20.                 <li>444</li>
  21.                 <li>555</li>
  22.                 <li>111</li>
  23.                 <li>222</li>
  24.                 <li>333</li>
  25.                 <li>444</li>
  26.                 <li>555</li>
  27.                 <li>111</li>
  28.                 <li>222</li>
  29.                 <li>333</li>
  30.                 <li>444</li>
  31.                 <li>555</li>
  32.         </ul>
  33.         <p class="refreshText"></p>
  34. </main>

  35. <script type="text/javascript">
  36. (function(window) {
  37.         // 获取当前滚动条的位置
  38.     function getScrollTop() {
  39.             var scrollTop = 0;
  40.             if (document.documentElement && document.documentElement.scrollTop) {
  41.                     scrollTop = document.documentElement.scrollTop;
  42.             } else if (document.body) {
  43.                     scrollTop = document.body.scrollTop;
  44.             }
  45.             return scrollTop;
  46.     }

  47.     // 获取当前可视范围的高度
  48.     function getClientHeight() {
  49.             var clientHeight = 0;
  50.             if (document.body.clientHeight && document.documentElement.clientHeight) {
  51.                     clientHeight = Math.min(document.body.clientHeight, document.documentElement.clientHeight);
  52.             }
  53.             else {
  54.                     clientHeight = Math.max(document.body.clientHeight, document.documentElement.clientHeight);
  55.             }
  56.             return clientHeight;
  57.     }

  58.     // 获取文档完整的高度
  59.     function getScrollHeight() {
  60.             return Math.max(document.body.scrollHeight, document.documentElement.scrollHeight);
  61.     }

  62.     var _text = document.querySelector('.refreshText'),
  63.     _container = document.getElementById('refreshContainer');

  64.     var throttle = function(method, context){
  65.                   clearTimeout(method.tId);
  66.                   method.tId = setTimeout(function(){
  67.                     method.call(context);
  68.                   }, 300);
  69.         }

  70.     function fetchData() {
  71.             setTimeout(function() {
  72.                     _container.insertAdjacentHTML('beforeend', '<li>new add...</li>');
  73.             }, 1000);
  74.     }

  75.     window.onscroll = function() {
  76.               if (getScrollTop() + getClientHeight() == getScrollHeight()) {
  77.                       _text.innerText = '加载中...';
  78.                       throttle(fetchData);
  79.               }
  80.     };

  81. })(window);
  82. </script>
  83. </body>
  84. </html>
复制代码
  1. 由于上面的demo针对是单页面且针对整个页面区,所以很多时候不大适合,下面这个例子是针对 div区域做的下拉刷新

  2. 滚动区域为div 的下拉刷新 demo
  3. 注:该demo相关的几个文件未给出,但关键代码已贴

  4. <!DOCTYPE html>
  5. <html lang="en">
  6. <head>
  7.     <meta charset="UTF-8">
  8.     <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  9.     <title>下拉刷新-移动端</title>

  10.     <script src="jscript/jquery-1.11.0.min.js" type="text/javascript"></script>
  11.     <script src="jscript/util.js" type="text/javascript"></script>
  12.     <script src="jscript/publish.js" type="text/javascript"></script>

  13.     <link rel="stylesheet" href="css/mbase.css">
  14.     <link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
  15.     <style>
  16.         .refreshText {
  17.             line-height: 50px;
  18.             text-align: center;
  19.         }
  20.     </style>
  21. </head>
  22. <body>
  23.     <div class="header">
  24.         <div style="width:10%;float:left; text-align:center; vertical-align:central;"><img src="mimages/nconlogo.png" style="width:75px;height:75px;"></div>
  25.         <div style="width:75%;float:right; margin-left:10px;">
  26.             <div class="nav">
  27.                 <a href="#" onclick="sel(this,2);">平面口罩</a>
  28.             </div>
  29.         </div>

  30.     </div>
  31.     <div class="content"  id="div2">
  32.         <p style="text-align:center;font-size:18px;margin-bottom:5px;margin-top:5px;">平面口罩</p>
  33.         <div id="wraper1" style="overflow-y:auto; height:75vh;">
  34.                 <div style="margin-left:5px; " id="infoData2">
  35.                     暂无信息
  36.                 </div>
  37.                 <p class="refreshText" id="refreshText1"></p>
  38.         </div>
  39.     </div>
  40.     <div class="footer">
  41.         <p>下拉刷新测试</p>
  42.     </div>

  43.     <script type="text/javascript">

  44.         var content = document.getElementById('wraper1');    //滑动容器
  45.         var _text = document.getElementById('refreshText1');

  46.         (function (window) {
  47.             $("#btnOk").click(function () {
  48.                 publish();
  49.             });
  50.             
  51.             // 获取当前滚动条的位置
  52.             function getScrollTop() {
  53.                 var scrollTop = 0;
  54.                 if (document.documentElement && content.scrollTop) {
  55.                     scrollTop = content.scrollTop;
  56.                 } else if (document.body) {
  57.                     scrollTop = document.body.scrollTop;
  58.                 }
  59.                 return scrollTop;
  60.             }

  61.             // 获取当前可视范围的高度
  62.             function getClientHeight() {
  63.                 var clientHeight = 0;
  64.                 if (document.body.clientHeight && content.clientHeight) {
  65.                     clientHeight = Math.min(document.body.clientHeight, content.clientHeight);
  66.                 }
  67.                 else {
  68.                     clientHeight = Math.max(document.body.clientHeight, content.clientHeight);
  69.                 }
  70.                 return clientHeight;
  71.             }

  72.             // 获取文档完整的高度
  73.             function getScrollHeight() {
  74.                 return Math.max(document.body.scrollHeight, content.scrollHeight);
  75.             }

  76.             var throttle = function (method, context) {
  77.                 clearTimeout(method.tId);
  78.                 method.tId = setTimeout(function () {
  79.                     method.call(context);
  80.                 }, 500);
  81.             }

  82.             function fetchData() {
  83.                 setTimeout(function () {
  84.                     //_container.insertAdjacentHTML('beforeend', '<li>new add...</li>');
  85.                     pageIndex++;
  86.                     getconinfoData(pageIndex, SmallpageSize);
  87.                     //alert("loading " + pageIndex+" 页 typeid="+CurrentTypeId);
  88.                 }, 1000);
  89.             }
  90.             
  91.             

  92.             // 触摸开始
  93.             function boxTouchStart(e) {
  94.                 var touch = e.touches[0];
  95.                 startY = touch.pageY;
  96.             }

  97.             // 触摸移动
  98.             function boxTouchMove(e) {
  99.                 var touch = e.touches[0];
  100.                 moveY = touch.pageY - startY;
  101.                 index = Number(e.target.id.split('page')[1])

  102.                 var htop = getScrollTop();
  103.                 var hch = getClientHeight();
  104.                 var sch = getScrollHeight();
  105.                 if (htop + hch >= sch) {
  106.                     _text.innerText = '加载中...';
  107.                     throttle(fetchData);
  108.                 }
  109.             }

  110.             content.addEventListener('touchstart', boxTouchStart, false)
  111.             content.addEventListener('touchmove', boxTouchMove, false)

  112.         })(window);
  113.     </script>
  114.     <script>

  115.         function sel(fromobj, val) {
  116.             //alert(val);
  117.             CurrentTypeId = 2;
  118.             $(".nav a").removeClass("active");
  119.             $(fromobj).addClass("active");
  120.             getconinfoData(pageIndex, SmallpageSize);
  121.         }
  122.     </script>
  123. </body>
  124. </html>

  125. getconinfoData为加载数据的js

  126. function getconinfoData(idx, size) {
  127.     var val = CurrentTypeId;
  128.     var typeId = parseInt(val) * 100;
  129.    
  130.     if (idx == 1) {
  131.         //先清空
  132.         $($("#infoData" + val)).html("");
  133.     }

  134.     var data = {};
  135.     data.call = ReqDefaultInfo;
  136.     var url = ReqDefaultDomain + "cov/list?typeId=" + typeId + "&pageIndex=" + idx + "&pageSize=" + size
  137.     ajaxProcessForApi(url, data.call, function callSuccess(oRet) {
  138.         var result = oRet.Data;
  139.         var html = "";
  140.         var tr = "<div class='listdata'><ul><li>产品名称:{PName}</li><li>注册证号:{RegisterNo}</li><li class='publisher'>注册人:{RegisterName}</li></ul></div>";
  141.         
  142.         $(result.Rows).each(function () {
  143.             html += tr.format(this);
  144.         });
  145.         if (html.length < 10) {
  146.             html = "暂无信息";
  147.             $($("#refreshText" + val)).innerText = "已经到底了...";
  148.         }
  149.         $($("#infoData" + val)).append(html);
  150.     }, function callError(e) {
  151.         //alert(e);
  152.     });
  153. }
  154. 页面绑定事件时加入了节流函数,其作用就是忽略滚动条300毫秒内的连续多次触发。

  155. 小结
  156. 上拉刷新的实现主要依靠的是touch事件的三个阶段,以及借助CSS3动画效果;下拉加载主要依靠页面的boxTouchMove事件,需要注意的是页面滚动时可能要考虑函数节流。

复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|生活导航|生活导航 ( 新ICP备12003026-1号 )

GMT+8, 2024-4-26 00:41 , Processed in 0.099336 second(s), 14 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表