js滑动到底部加载更多

版权声明:本文为博主原创文章,转载请附上原链接。 https://blog.csdn.net/weixin_39927850/article/details/80502315

上面动图是实现的效果。

全程无需重新加载整个页面,只需要上下拉即可刷新数据!

index.html

 

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>上拉加载</title>

<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/>

<script src="http://www.jq22.com/jquery/jquery-1.10.2.js"></script>

<style type="text/css">

* {

margin: 0;

padding: 0;

list-style: none;

box-sizing: border-box;

-webkit-box-sizing: border-box;

-moz-box-sizing: border-box;

}

html {font-size: 14px; }

header {

height: 40px;

width: 100%;

min-width: 320px;

background: #000;

text-align: center;

color: #fff;

font-size: 1.2rem;

line-height: 40px;

}

footer {

height: 52px;

width: 100%;

min-width: 320px;

background: #000;

text-align: center;

color: #fff;

font-size: 1.2rem;

line-height: 52px;

position: absolute;

bottom: 0;

}

#wrapper {

width: 100%;

min-width: 320px;

position: absolute;

left: 0;

top: 40px;

bottom: 52px;

overflow: hidden;

z-index: 1;

background-color: #eee;

/* 防止本机Windows上的触摸事件 */

-ms-touch-action: none;

/* 防止callout tap-hold和文本的选择 */

-webkit-touch-callout: none;

-webkit-user-select: none;

-moz-user-select: none;

-ms-user-select: none;

user-select: none;

/* 防止文本调整取向变化对web应用程序很有用 */

-webkit-text-size-adjust: none;

-moz-text-size-adjust: none;

-ms-text-size-adjust: none;

-o-text-size-adjust: none;

text-size-adjust: none;

}

.pull-loading {

text-align: center;

height: 40px;

line-height: 40px;

display: flex;

align-items: center;

justify-content: center;

}

#scroller ul li {

padding: 20px 10px;

border-bottom: solid 1px #ccc;

background: #fff;

}

</style>

</head>

<body>

<header>上拉加载,下拉刷新</header>

<div id="wrapper">

<div id="scroller">

<ul>

<li>这里显示部分数据</li>

<li>这里显示部分数据</li>

<li>这里显示部分数据</li>

<li>这里显示部分数据</li>

<li>这里显示部分数据</li>

</ul>

<div class="pull-loading">

上拉加载

</div>

</div>

</div>

<footer>底部</footer>

<script type="text/javascript" src="js/iscroll.js"></script>

<script type="text/javascript">

var myscroll = new iScroll("wrapper", {

onScrollMove: function () { //拉动时

//上拉加载

if (this.y < this.maxScrollY) {

$(".pull-loading").html("释放加载");

$(".pull-loading").addClass("loading");

} else {

$(".pull-loading").html("上拉加载");

$(".pull-loading").removeClass("loading");

}

},

onScrollEnd: function () { //拉动结束时

//上拉加载

if ($(".pull-loading").hasClass('loading')) {

$(".pull-loading").html("加载中...");

pullOnLoad();

}

}

});

//上拉加载函数,ajax

var num = 0;

var page = 4; //每次加载4条

function pullOnLoad() {

setTimeout(function () {

$.ajax({

url: "json/data.json",

type: "get",

dataType: 'json',

success: function (data) {

var data_length = data.length;//数据的总长度

var remainder = data_length % page;//余数

if ( data_length >= (num+page)){

for (var j = num; j < num + page; j++){

var text = data[j].text;

$("#scroller ul").append("<li>"+ text +"</li>");

}

num+=page;

}else if (remainder != 0 && data_length-num == remainder){

for (var j = num; j < num + remainder; j++){

var text = data[j].text;

$("#scroller ul").append("<li>"+ text +"</li>");

}

num+=page;

}else{

$('.pull-loading').html("没有了哟");

}

myscroll.refresh();

},

error: function () {

console.log("出错了");

}

});

myscroll.refresh();

}, 500);

}

</script>

</body>

</html>

data.json

 
  1. [
  2. {

  3. "text":"十年编程两茫茫,工期短,需求长。",

  4. "date":"2018-02-02 14:00"

  5. },

  6. {

  7. "text":"千行代码,Bug何处藏。",

  8. "date":"2018-02-02 14:00"

  9. },

  10. {

  11. "text":"纵使上线又如何,新版本,继续忙。",

  12. "date":"2018-02-02 14:00"

  13. },

  14. {

  15. "text":"黑白颠倒没商量,睡地铺,吃食堂。",

  16. "date":"2018-02-02 14:00"

  17. },

  18. {

  19. "text":"夜半梦醒,无人在身旁。",

  20. "date":"2018-02-02 14:00"

  21. },

  22. {

  23. "text":"最怕灯火阑珊时,手机响,心里慌。",

  24. "date":"2018-02-02 14:00"

  25. },

  26. {

  27. "text":"是,程序员设计了程序",

  28. "date":"2018-02-02 14:00"

  29. },

  30. {

  31. "text":"还是,程序造就了程序员?",

  32. "date":"2018-02-02 14:00"

  33. },

  34. {

  35. "text":"程序,程序员——你的名字,我的姓氏",

  36. "date":"2018-02-02 14:00"

  37. },

  38. {

  39. "text":"日撸代码三千行,疯狂、疯狂,人未老,珠已黄。",

  40. "date":"2018-02-02 14:00"

  41. },

  42. {

  43. "text":"少年投身IT行,老来无伴又何妨。擦肩美女不屑看,三千码友在身旁。",

  44. "date":"2018-02-02 14:00"

  45. },

  46. {

  47. "text":"老夫聊发少年狂,不小心,选错行。误入IT,两眼泪茫茫",

  48. "date":"2018-02-02 14:00"

  49. },

  50. {

  51. "text":"夜半话凄凉,转眼泪千行,日日工期紧,亦为重构忙。久易无定论,命悬需求方,四顾前途路,一步三踉跄。",

  52. "date":"2018-02-02 14:00"

  53. },

  54. {

  55. "text":"IT放两旁,闲来把码敲,余音仍绕梁。码农压力大,愿君更健康!",

  56. "date":"2018-02-02 14:00"

  57. },

  58. {

  59. "text":"111111111111",

  60. "date":"2018-02-02 14:00"

  61. },

  62. {

  63. "text":"22222",

  64. "date":"2018-02-02 14:00"

  65. },

  66. {

  67. "text":"33333",

  68. "date":"2018-02-02 14:00"

  69. },

  70. {

  71. "text":"4444",

  72. "date":"2018-02-02 14:00"

  73. },

  74. {

  75. "text":"55555",

  76. "date":"2018-02-02 14:00"

  77. },

  78. {

  79. "text":"66666",

  80. "date":"2018-02-02 14:00"

  81. },

  82. {

  83. "text":"7777",

  84. "date":"2018-02-02 14:00"

  85. },

  86. {

  87. "text":"88888",

  88. "date":"2018-02-02 14:00"

  89. },

  90. {

  91. "text":"99999",

  92. "date":"2018-02-02 14:00"

  93. },

  94. {

  95. "text":"1010101010",

  96. "date":"2018-02-02 14:00"

  97. },

  98. {

  99. "text":"1.1.1.1.1.1.1.1.1.1.",

  100. "date":"2018-02-02 14:00"

  101. },

  102. {

  103. "text":"12121212121212",

  104. "date":"2018-02-02 14:00"

  105. },

  106. {

  107. "text":"1131313133131",

  108. "date":"2018-02-02 14:00"

  109. },

  110. {

  111. "text":"1414141414",

  112. "date":"2018-02-02 14:00"

  113. },

  114. {

  115. "text":"1515151515151",

  116. "date":"2018-02-02 14:00"

  117. },

  118. {

  119. "text":"111111111111",

  120. "date":"2018-02-02 14:00"

  121. },

  122. {

  123. "text":"111111111111",

  124. "date":"2018-02-02 14:00"

  125. },

  126. {

  127. "text":"111111111111",

  128. "date":"2018-02-02 14:00"

  129. },

  130. {

  131. "text":"111111111111",

  132. "date":"2018-02-02 14:00"

  133. },

  134. {

  135. "text":"111111111111",

  136. "date":"2018-02-02 14:00"

  137. },

  138. {

  139. "text":"111111111111",

  140. "date":"2018-02-02 14:00"

  141. },

  142. {

  143. "text":"111111111111",

  144. "date":"2018-02-02 14:00"

  145. }

  146.  
  147. ]

需要引入两个js文件
jquery-2.2.4.min.js
iscroll.js

这两个文件已经准备好了

https://pan.baidu.com/s/1dGSTBHPUtvBKi41IcxRogw

猜你喜欢

转载自blog.csdn.net/ZNYSYS520/article/details/82015398
今日推荐