当前位置:   article > 正文

H5移动端网页上拉及下拉刷新数据_h5 上拉加载五条数据

h5 上拉加载五条数据

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

上拉刷新demo 

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <title>Document</title>
  7. <style type="text/css">
  8. html,body,header,div,main,p,span,ul,li{ margin: 0; padding: 0; }
  9. #refreshContainer li{ background-color: #eee; margin-bottom: 1px; padding: 20px 10px; }
  10. .refreshText{ position: absolute; width: 100%; line-height: 50px; text-align: center; left: 0; top: 0; }
  11. </style>
  12. </head>
  13. <body>
  14. <main>
  15. <p class="refreshText"></p>
  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. </main>
  34. <script type="text/javascript">
  35. (function(window) {
  36. var _element = document.getElementById('refreshContainer'),
  37. _refreshText = document.querySelector('.refreshText'),
  38. _startPos = 0,
  39. _transitionHeight = 0;
  40. _element.addEventListener('touchstart', function(e) {
  41. console.log('初始位置:', e.touches[0].pageY);
  42. _startPos = e.touches[0].pageY;
  43. _element.style.position = 'relative';
  44. _element.style.transition = 'transform 0s';
  45. }, false);
  46. _element.addEventListener('touchmove', function(e) {
  47. console.log('当前位置:', e.touches[0].pageY);
  48. _transitionHeight = e.touches[0].pageY - _startPos;
  49. if (_transitionHeight > 0 && _transitionHeight < 60) {
  50. _refreshText.innerText = '下拉刷新';
  51. _element.style.transform = 'translateY('+_transitionHeight+'px)';
  52. if (_transitionHeight > 55) {
  53. _refreshText.innerText = '释放更新';
  54. _element.append("<li>100</li>");
  55. }
  56. }
  57. }, false);
  58. _element.addEventListener('touchend', function(e) {
  59. _element.style.transition = 'transform 0.5s ease 1s';
  60. _element.style.transform = 'translateY(0px)';
  61. _refreshText.innerText = '更新中...';
  62. // todo...
  63. }, false);
  64. })(window);
  65. </script>
  66. </body>
  67. </html>

 

下拉加载更多demo

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <title>Document</title>
  7. <style type="text/css">
  8. html,body,header,div,main,p,span,ul,li{ margin: 0; padding: 0; }
  9. #refreshContainer li{ background-color: #eee; margin-bottom: 1px; padding: 20px 10px; }
  10. .refreshText{ line-height: 50px; text-align: center; }
  11. </style>
  12. </head>
  13. <body>
  14. <main>
  15. <ul id="refreshContainer">
  16. <li>111</li>
  17. <li>222</li>
  18. <li>333</li>
  19. <li>444</li>
  20. <li>555</li>
  21. <li>111</li>
  22. <li>222</li>
  23. <li>333</li>
  24. <li>444</li>
  25. <li>555</li>
  26. <li>111</li>
  27. <li>222</li>
  28. <li>333</li>
  29. <li>444</li>
  30. <li>555</li>
  31. </ul>
  32. <p class="refreshText"></p>
  33. </main>
  34. <script type="text/javascript">
  35. (function(window) {
  36. // 获取当前滚动条的位置
  37. function getScrollTop() {
  38. var scrollTop = 0;
  39. if (document.documentElement && document.documentElement.scrollTop) {
  40. scrollTop = document.documentElement.scrollTop;
  41. } else if (document.body) {
  42. scrollTop = document.body.scrollTop;
  43. }
  44. return scrollTop;
  45. }
  46. // 获取当前可视范围的高度
  47. function getClientHeight() {
  48. var clientHeight = 0;
  49. if (document.body.clientHeight && document.documentElement.clientHeight) {
  50. clientHeight = Math.min(document.body.clientHeight, document.documentElement.clientHeight);
  51. }
  52. else {
  53. clientHeight = Math.max(document.body.clientHeight, document.documentElement.clientHeight);
  54. }
  55. return clientHeight;
  56. }
  57. // 获取文档完整的高度
  58. function getScrollHeight() {
  59. return Math.max(document.body.scrollHeight, document.documentElement.scrollHeight);
  60. }
  61. var _text = document.querySelector('.refreshText'),
  62. _container = document.getElementById('refreshContainer');
  63. var throttle = function(method, context){
  64. clearTimeout(method.tId);
  65. method.tId = setTimeout(function(){
  66. method.call(context);
  67. }, 300);
  68. }
  69. function fetchData() {
  70. setTimeout(function() {
  71. _container.insertAdjacentHTML('beforeend', '<li>new add...</li>');
  72. }, 1000);
  73. }
  74. window.onscroll = function() {
  75. if (getScrollTop() + getClientHeight() == getScrollHeight()) {
  76. _text.innerText = '加载中...';
  77. throttle(fetchData);
  78. }
  79. };
  80. })(window);
  81. </script>
  82. </body>
  83. </html>

 

由于上面的demo针对是单页面且针对整个页面区,所以很多时候不大适合,下面这个例子是针对 div区域做的下拉刷新

滚动区域为div 的下拉刷新 demo

注:该demo相关的几个文件未给出,但关键代码已贴

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  6. <title>下拉刷新-移动端</title>
  7. <script src="jscript/jquery-1.11.0.min.js" type="text/javascript"></script>
  8. <script src="jscript/util.js" type="text/javascript"></script>
  9. <script src="jscript/publish.js" type="text/javascript"></script>
  10. <link rel="stylesheet" href="css/mbase.css">
  11. <link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
  12. <style>
  13. .refreshText {
  14. line-height: 50px;
  15. text-align: center;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <div class="header">
  21. <div style="width:10%;float:left; text-align:center; vertical-align:central;"><img src="mimages/nconlogo.png" style="width:75px;height:75px;"></div>
  22. <div style="width:75%;float:right; margin-left:10px;">
  23. <div class="nav">
  24. <a href="#" onclick="sel(this,2);">平面口罩</a>
  25. </div>
  26. </div>
  27. </div>
  28. <div class="content" id="div2">
  29. <p style="text-align:center;font-size:18px;margin-bottom:5px;margin-top:5px;">平面口罩</p>
  30. <div id="wraper1" style="overflow-y:auto; height:75vh;">
  31. <div style="margin-left:5px; " id="infoData2">
  32. 暂无信息
  33. </div>
  34. <p class="refreshText" id="refreshText1"></p>
  35. </div>
  36. </div>
  37. <div class="footer">
  38. <p>下拉刷新测试</p>
  39. </div>
  40. <script type="text/javascript">
  41. var content = document.getElementById('wraper1'); //滑动容器
  42. var _text = document.getElementById('refreshText1');
  43. (function (window) {
  44. $("#btnOk").click(function () {
  45. publish();
  46. });
  47. // 获取当前滚动条的位置
  48. function getScrollTop() {
  49. var scrollTop = 0;
  50. if (document.documentElement && content.scrollTop) {
  51. scrollTop = content.scrollTop;
  52. } else if (document.body) {
  53. scrollTop = document.body.scrollTop;
  54. }
  55. return scrollTop;
  56. }
  57. // 获取当前可视范围的高度
  58. function getClientHeight() {
  59. var clientHeight = 0;
  60. if (document.body.clientHeight && content.clientHeight) {
  61. clientHeight = Math.min(document.body.clientHeight, content.clientHeight);
  62. }
  63. else {
  64. clientHeight = Math.max(document.body.clientHeight, content.clientHeight);
  65. }
  66. return clientHeight;
  67. }
  68. // 获取文档完整的高度
  69. function getScrollHeight() {
  70. return Math.max(document.body.scrollHeight, content.scrollHeight);
  71. }
  72. var throttle = function (method, context) {
  73. clearTimeout(method.tId);
  74. method.tId = setTimeout(function () {
  75. method.call(context);
  76. }, 500);
  77. }
  78. function fetchData() {
  79. setTimeout(function () {
  80. //_container.insertAdjacentHTML('beforeend', '<li>new add...</li>');
  81. pageIndex++;
  82. getconinfoData(pageIndex, SmallpageSize);
  83. //alert("loading " + pageIndex+" 页 typeid="+CurrentTypeId);
  84. }, 1000);
  85. }
  86. // 触摸开始
  87. function boxTouchStart(e) {
  88. var touch = e.touches[0];
  89. startY = touch.pageY;
  90. }
  91. // 触摸移动
  92. function boxTouchMove(e) {
  93. var touch = e.touches[0];
  94. moveY = touch.pageY - startY;
  95. index = Number(e.target.id.split('page')[1])
  96. var htop = getScrollTop();
  97. var hch = getClientHeight();
  98. var sch = getScrollHeight();
  99. if (htop + hch >= sch) {
  100. _text.innerText = '加载中...';
  101. throttle(fetchData);
  102. }
  103. }
  104. content.addEventListener('touchstart', boxTouchStart, false)
  105. content.addEventListener('touchmove', boxTouchMove, false)
  106. })(window);
  107. </script>
  108. <script>
  109. function sel(fromobj, val) {
  110. //alert(val);
  111. CurrentTypeId = 2;
  112. $(".nav a").removeClass("active");
  113. $(fromobj).addClass("active");
  114. getconinfoData(pageIndex, SmallpageSize);
  115. }
  116. </script>
  117. </body>
  118. </html>
  119. getconinfoData为加载数据的js
  120. function getconinfoData(idx, size) {
  121. var val = CurrentTypeId;
  122. var typeId = parseInt(val) * 100;
  123. if (idx == 1) {
  124. //先清空
  125. $($("#infoData" + val)).html("");
  126. }
  127. var data = {};
  128. data.call = ReqDefaultInfo;
  129. var url = ReqDefaultDomain + "cov/list?typeId=" + typeId + "&pageIndex=" + idx + "&pageSize=" + size
  130. ajaxProcessForApi(url, data.call, function callSuccess(oRet) {
  131. var result = oRet.Data;
  132. var html = "";
  133. var tr = "<div class='listdata'><ul><li>产品名称:{PName}</li><li>注册证号:{RegisterNo}</li><li class='publisher'>注册人:{RegisterName}</li></ul></div>";
  134. $(result.Rows).each(function () {
  135. html += tr.format(this);
  136. });
  137. if (html.length < 10) {
  138. html = "暂无信息";
  139. $($("#refreshText" + val)).innerText = "已经到底了...";
  140. }
  141. $($("#infoData" + val)).append(html);
  142. }, function callError(e) {
  143. //alert(e);
  144. });
  145. }

页面绑定事件时加入了节流函数,其作用就是忽略滚动条300毫秒内的连续多次触发。

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

 

--- end ---

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/97419
推荐阅读
相关标签