当前位置:Gxlcms > JavaScript > iscroll动态加载数据完美解决方法

iscroll动态加载数据完美解决方法

时间:2021-07-01 10:21:17 帮助过:15人阅读

本文实例为大家分享了iscroll动态加载数据的具体代码,供大家参考,具体内容如下

  1. <div id="wrapper" class="margin-b90">
  2. <div id="scroller">
  3. <div id="pullDown">
  4. <span class="pullDownLabel" style="text-align: center;">加载中...</span>
  5. </div>
  6. <div class="sps_itemBox ">
  7. <div class="list_show">
  8. <ul id="ulList"></ul>
  9. </div>
  10. </div>
  11. <div id="pullUp">
  12. <span class="pullUpLabel" style="text-align: center;">上拉加载...</span>
  13. </div>
  14. </div>
  15. </div>

js.

  1. // iScroll 滚动条/上拉刷新/下拉加载
  2. var myScroll,
  3. pullDownEl,
  4. pullDownOffset,
  5. pullUpEl,
  6. pullUpOffset;
  7. function loaded() {
  8. pullDownEl = document.getElementById('pullDown');
  9. pullDownOffset = pullDownEl.offsetHeight;
  10. pullUpEl = document.getElementById('pullUp');
  11. pullUpOffset = pullUpEl.offsetHeight;
  12. myScroll = new iScroll('wrapper', {
  13. useTransition: false,
  14. topOffset: pullDownOffset,
  15. btnOffset: pullUpOffset,
  16. hideScrollbar: true,
  17. fadeScrollbar: true,
  18. onRefresh: function () {
  19. if (pullDownEl.className.match('loading')) {
  20. pullDownEl.className = '';
  21. //pullDownEl.querySelector('.pullDownLabel').innerHTML = '下拉刷新...';
  22. pullDownEl.querySelector('.pullDownLabel').innerHTML = '数据更新时间:' + updateDatetime;
  23. } else if (pullUpEl.className.match('loading')) {
  24. pullUpEl.className = '';
  25. pullUpEl.querySelector('.pullUpLabel').innerHTML = '上拉加载...';
  26. }
  27. },
  28. onScrollMove: function () {
  29. if (this.y > 5 && !pullDownEl.className.match('flip')) {
  30. pullDownEl.className = 'flip';
  31. //pullDownEl.querySelector('.pullDownLabel').innerHTML = '释放刷新...';
  32. pullDownEl.querySelector('.pullDownLabel').innerHTML = '数据更新时间:' + updateDatetime;
  33. this.minScrollY = 0;
  34. } else if (this.y < 5 && pullDownEl.className.match('flip')) {
  35. pullDownEl.className = '';
  36. //pullDownEl.querySelector('.pullDownLabel').innerHTML = '下拉刷新...';
  37. pullDownEl.querySelector('.pullDownLabel').innerHTML = '数据更新时间:' + updateDatetime;
  38. this.minScrollY = -pullDownOffset;
  39. } else if (this.y < (this.maxScrollY - pullUpOffset - 40) && !pullUpEl.className.match('flip')) {
  40. pullUpEl.className = 'flip';
  41. pullUpEl.querySelector('.pullUpLabel').innerHTML = '释放加载...';
  42. this.maxScrollY = this.maxScrollY - pullUpOffset;
  43. }
  44. //else if (this.y > (this.maxScrollY - pullUpOffset) && pullUpEl.className.match('flip')) {
  45. // pullUpEl.className = '';
  46. // pullUpEl.querySelector('.pullUpLabel').innerHTML = '上拉加载...';
  47. // //this.maxScrollY = pullUpOffset;
  48. //}
  49. },
  50. onScrollEnd: function () {
  51. if (pullDownEl.className.match('flip')) {
  52. pullDownEl.className = 'loading';
  53. //pullDownEl.querySelector('.pullDownLabel').innerHTML = '数据刷新中...';
  54. pullDownEl.querySelector('.pullDownLabel').innerHTML = '数据更新时间:' + updateDatetime;
  55. myScroll.refresh();
  56. } else if (pullUpEl.className.match('flip')) {
  57. pullUpEl.className = 'loading';
  58. pullUpEl.querySelector('.pullUpLabel').innerHTML = '数据加载中...';
  59. setTimeout(function () { myScroll.refresh(); }, 3000);
  60. }
  61. }
  62. });
  63. }
  64. document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);
  65. document.addEventListener('DOMContentLoaded', function () { setTimeout(loaded, 200); }, false);

css

  1. /* iScroll */
  2. #wrapper{width:100%; position:absolute; top:0; bottom:0; z-index:1; overflow:hidden;}
  3. #scroller{
  4. width:100%; position:absolute; z-index:1;
  5. -webkit-touch-callout:none; -webkit-tap-highlight-color:rgba(0,0,0,0);
  6. }
  7. #pullDown,
  8. #pullUp{padding:15px 0 15px 60px; font-size:14px; line-height:27px; color:#303030;}
  9. #pullDown{background:url(../images/loadBottom.png) no-repeat 30px center; background-size:27px 27px;}
  10. #pullUp{background:url(../images/loadTop.png) no-repeat 30px center; background-size:27px 27px;}
  11. #pullDown.flip{background:url(../images/loadTop.png) no-repeat 30px center; background-size:27px 27px;}
  12. #pullUp.flip{background:url(../images/loadBottom.png) no-repeat 30px center; background-size:27px 27px;}
  13. #pullDown.loading,
  14. #pullUp.loading{background:url(../images/loading.gif) no-repeat 30px center; background-size:25px 27px;}
  15. /* iScroll end */

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

人气教程排行