当前位置:   article > 正文

前端实现伪分页_前端假分页

前端假分页

有时候,做静态页面的时候,需要用到分页,当然绝绝绝大多数的情况下,分页这个事是后台来做的,用户请求时再给数据,至于两者利弊,稍后再讲。 因此,我们在这里称作它为伪分页。

此分页分为四部分,首先是页面布局,为了简洁布局,直接用了BootStrap中的分页,代码如下

  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.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>实现伪分页</title>
  8. </head>
  9. <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
  10. <body>
  11. <div id="list-icount">
  12. <!--存放数据的div-->
  13. </div>
  14. <ul class="pagination" id="paging">
  15. <!--存放页数列表ul-->
  16. </ul>
  17. </body>
  18. <script src="https://cdn.bootcss.com/jquery/2.1.1/jquery.min.js"></script>
  19. <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  20. </html>
第二我们暂且用本地数据,也可以请求后台,不过请求后台的话还是用后端做比较合适

  1. var data = {
  2. 1: "<p>俺家酒儿清</p>",
  3. 2: "<p>一贯买两瓶</p>",
  4. 3: "<p>灌得肚儿胀</p>",
  5. 4: "<p>自家店小二是也</p>",
  6. 5: "<p>在这岳阳楼下开着一个酒店</p>",
  7. 6: "<p>但是南来北往经商客旅</p>",
  8. 7: "<p>做买做卖</p>",
  9. 8: "<p>都来这楼上饮酒</p>",
  10. 9: "<p>今日早晨间</p>",
  11. 10: "<p>我将这镟锅儿烧的热了</p>",
  12. 11: "<p>将酒望子挑起来</p>",
  13. 12: "<p>招过客,招过客</p>",
  14. 13: "<p>贫道姓吕名岩字洞宾</p>",
  15. 14: "<p>道号纯阳子</p>",
  16. 15: "<p>先为唐朝儒士</p>",
  17. 16: "<p>后遇钟离师父点化</p>",
  18. 17: "<p>得成仙道</p>",
  19. 18: "<p>贫道在蟠桃会上饮宴</p>"
  20. }
第三我们绑定数据及加载数据
  1. function turnJson(oJsonArr, oJsonArrValue) {
  2. var iCount = 0;
  3. var turn_json_arr = [];
  4. if (arguments.length == 1) {
  5. for (key in oJsonArr) {
  6. turn_json_arr.push(key);
  7. //一个值时是将json的key转化成数组
  8. }
  9. } else {
  10. for (key in oJsonArr) {
  11. turn_json_arr.push(oJsonArr[key]);
  12. };
  13. }
  14. return turn_json_arr;
  15. }
  16. var iPageCount,
  17. iPageList = 6,
  18. sLi = '',
  19. k = 0,
  20. oPaging = document.getElementById('paging'), //获取ul ID
  21. oList_icount = document.getElementById('list-icount'); //获取数据列表ID
  22. var dataArr = turnJson(data, 'newsList'); //将对象中的键值转换为数组
  23. iPageCount = Math.ceil(dataArr.length / iPageList); //将页数进行向上取整
  24. for (var i = 0; i < iPageCount; i++) {
  25. sLi += '<li class="page-footer"><a href="javascript:;">' + (i + 1) + '</a></li>'; //动态创建页数
  26. }
  27. // 将前一页和后一页标签加上
  28. oPaging.innerHTML = '<li id="prePage"><a href="javascript:;">«</a></li>' + sLi +
  29. '<li id="nextPage"><a href="javascript:;">»</a></li>';
  30. // 对控制数据的显隐和绑定数据
  31. while (k < dataArr.length) {
  32. switch (true) {
  33. case k <= 5:
  34. if (k == 0) {
  35. var wraperP_1 = document.createElement('div')
  36. }
  37. wraperP_1.innerHTML += dataArr[k];
  38. if (k == 5) {
  39. wraperP_1.className = 'ShowPara';
  40. wraperP_1.style.display = 'block';
  41. oList_icount.appendChild(wraperP_1)
  42. }
  43. break;
  44. case k > 5 && k <= 11:
  45. if (k == 6) {
  46. var wraperP_2 = document.createElement('div')
  47. }
  48. wraperP_2.innerHTML += dataArr[k];
  49. if (k == 11) {
  50. wraperP_2.className = 'ShowPara';
  51. wraperP_2.style.display = 'none';
  52. oList_icount.appendChild(wraperP_2)
  53. }
  54. break;
  55. case k > 11 && k <= 17:
  56. if (k == 12) {
  57. var wraperP_3 = document.createElement('div')
  58. }
  59. wraperP_3.innerHTML += dataArr[k];
  60. if (k == 17) {
  61. wraperP_3.className = 'ShowPara';
  62. wraperP_3.style.display = 'none';
  63. oList_icount.appendChild(wraperP_3)
  64. }
  65. break;
  66. case k > 17 && k <= dataArr.length - 1:
  67. if (k == 18) {
  68. var wraperP_4 = document.createElement('div')
  69. }
  70. wraperP_4.innerHTML += dataArr[k];
  71. if (k == dataArr.length - 1) {
  72. wraperP_4.className = 'ShowPara';
  73. wraperP_4.style.display = 'none';
  74. oList_icount.appendChild(wraperP_4)
  75. }
  76. break;
  77. default:
  78. break
  79. }
  80. k++
  81. }
第四,对元素对象进行事件操作

  1. iPage_footer = document.getElementsByClassName('page-footer').length,
  2. oShowPara = document.getElementsByClassName('ShowPara'),
  3. iShowPara = document.getElementsByClassName('ShowPara').length;
  4. document.getElementsByClassName('page-footer')[0].classList.add('active');
  5. // 为每一个页数增加点击事件,设置active类
  6. for (var i = 0; i < iPage_footer; i++) {
  7. oPage_footer[i].index = i;
  8. oPage_footer[i].onclick = function () {
  9. for (var j = 0; j < iPage_footer; j++) {
  10. oShowPara[j].style.display = 'none';
  11. oPage_footer[j].className = 'page-footer'
  12. }
  13. oShowPara[this.index].style.display = 'block';
  14. this.className = 'page-footer active'
  15. }
  16. };
  17. var oPre = document.getElementById('prePage');
  18. var oNext = document.getElementById('nextPage');
  19. // 控制列表的显隐
  20. function circleList() {
  21. for (var j = 0; j < iPage_footer; j++) {
  22. oShowPara[j].style.display = 'none';
  23. oPage_footer[j].className = 'page-footer'
  24. }
  25. }
  26. //进行下一页的切换
  27. function nextPage() {
  28. for (var i = 0; i < iPage_footer; i++) {
  29. if (oShowPara[i].style.display == 'block') {
  30. if (i == iPage_footer - 1) return;
  31. circleList();
  32. oShowPara[(i + 1)].style.display = 'block';
  33. oPage_footer[(i + 1)].className = 'page-footer active';
  34. return
  35. }
  36. }
  37. };
  38. //进行上一页切换
  39. function prePage() {
  40. for (var i = 0; i < iPage_footer; i++) {
  41. if (oShowPara[i].style.display == 'block') {
  42. if (i == 0) return;
  43. circleList();
  44. oShowPara[(i - 1)].style.display = 'block';
  45. oPage_footer[(i - 1)].className = 'page-footer active';
  46. return
  47. }
  48. }
  49. };
  50. oPre.onclick = prePage;
  51. oNext.onclick = nextPage;
最后整个代码如下

  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.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>实现伪分页</title>
  8. </head>
  9. <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
  10. <body>
  11. <div id="list-icount">
  12. <!--存放数据的div-->
  13. </div>
  14. <ul class="pagination" id="paging">
  15. <!--存放页数列表ul-->
  16. </ul>
  17. </body>
  18. <script src="https://cdn.bootcss.com/jquery/2.1.1/jquery.min.js"></script>
  19. <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  20. <script>
  21. var data = {
  22. 1: "<p>俺家酒儿清</p>",
  23. 2: "<p>一贯买两瓶</p>",
  24. 3: "<p>灌得肚儿胀</p>",
  25. 4: "<p>自家店小二是也</p>",
  26. 5: "<p>在这岳阳楼下开着一个酒店</p>",
  27. 6: "<p>但是南来北往经商客旅</p>",
  28. 7: "<p>做买做卖</p>",
  29. 8: "<p>都来这楼上饮酒</p>",
  30. 9: "<p>今日早晨间</p>",
  31. 10: "<p>我将这镟锅儿烧的热了</p>",
  32. 11: "<p>将酒望子挑起来</p>",
  33. 12: "<p>招过客,招过客</p>",
  34. 13: "<p>贫道姓吕名岩字洞宾</p>",
  35. 14: "<p>道号纯阳子</p>",
  36. 15: "<p>先为唐朝儒士</p>",
  37. 16: "<p>后遇钟离师父点化</p>",
  38. 17: "<p>得成仙道</p>",
  39. 18: "<p>贫道在蟠桃会上饮宴</p>"
  40. }
  41. //将json中的key或键值转化为数组
  42. function turnJson(oJsonArr, oJsonArrValue) {
  43. var iCount = 0;
  44. var turn_json_arr = [];
  45. if (arguments.length == 1) {
  46. for (key in oJsonArr) {
  47. turn_json_arr.push(key);
  48. //一个值时是将json的key转化成数组
  49. }
  50. } else {
  51. for (key in oJsonArr) {
  52. turn_json_arr.push(oJsonArr[key]);
  53. };
  54. }
  55. return turn_json_arr;
  56. }
  57. var iPageCount,
  58. iPageList = 6,
  59. sLi = '',
  60. k = 0,
  61. oPaging = document.getElementById('paging'), //获取ul ID
  62. oList_icount = document.getElementById('list-icount'); //获取数据列表ID
  63. var dataArr = turnJson(data, 'newsList'); //将对象中的键值转换为数组
  64. iPageCount = Math.ceil(dataArr.length / iPageList); //将页数进行向上取整
  65. for (var i = 0; i < iPageCount; i++) {
  66. sLi += '<li class="page-footer"><a href="javascript:;">' + (i + 1) + '</a></li>'; //动态创建页数
  67. }
  68. // 将前一页和后一页标签加上
  69. oPaging.innerHTML = '<li id="prePage"><a href="javascript:;">«</a></li>' + sLi +
  70. '<li id="nextPage"><a href="javascript:;">»</a></li>';
  71. // 对控制数据的显隐和绑定数据
  72. while (k < dataArr.length) {
  73. switch (true) {
  74. case k <= 5:
  75. if (k == 0) {
  76. var wraperP_1 = document.createElement('div')
  77. }
  78. wraperP_1.innerHTML += dataArr[k];
  79. if (k == 5) {
  80. wraperP_1.className = 'ShowPara';
  81. wraperP_1.style.display = 'block';
  82. oList_icount.appendChild(wraperP_1)
  83. }
  84. break;
  85. case k > 5 && k <= 11:
  86. if (k == 6) {
  87. var wraperP_2 = document.createElement('div')
  88. }
  89. wraperP_2.innerHTML += dataArr[k];
  90. if (k == 11) {
  91. wraperP_2.className = 'ShowPara';
  92. wraperP_2.style.display = 'none';
  93. oList_icount.appendChild(wraperP_2)
  94. }
  95. break;
  96. case k > 11 && k <= 17:
  97. if (k == 12) {
  98. var wraperP_3 = document.createElement('div')
  99. }
  100. wraperP_3.innerHTML += dataArr[k];
  101. if (k == 17) {
  102. wraperP_3.className = 'ShowPara';
  103. wraperP_3.style.display = 'none';
  104. oList_icount.appendChild(wraperP_3)
  105. }
  106. break;
  107. case k > 17 && k <= dataArr.length - 1:
  108. if (k == 18) {
  109. var wraperP_4 = document.createElement('div')
  110. }
  111. wraperP_4.innerHTML += dataArr[k];
  112. if (k == dataArr.length - 1) {
  113. wraperP_4.className = 'ShowPara';
  114. wraperP_4.style.display = 'none';
  115. oList_icount.appendChild(wraperP_4)
  116. }
  117. break;
  118. default:
  119. break
  120. }
  121. k++
  122. }
  123. var oPage_footer = document.getElementsByClassName('page-footer'),
  124. iPage_footer = document.getElementsByClassName('page-footer').length,
  125. oShowPara = document.getElementsByClassName('ShowPara'),
  126. iShowPara = document.getElementsByClassName('ShowPara').length;
  127. document.getElementsByClassName('page-footer')[0].classList.add('active');
  128. // 为每一个页数增加点击事件,设置active类
  129. for (var i = 0; i < iPage_footer; i++) {
  130. oPage_footer[i].index = i;
  131. oPage_footer[i].onclick = function () {
  132. for (var j = 0; j < iPage_footer; j++) {
  133. oShowPara[j].style.display = 'none';
  134. oPage_footer[j].className = 'page-footer'
  135. }
  136. oShowPara[this.index].style.display = 'block';
  137. this.className = 'page-footer active'
  138. }
  139. };
  140. var oPre = document.getElementById('prePage');
  141. var oNext = document.getElementById('nextPage');
  142. // 控制列表的显隐
  143. function circleList() {
  144. for (var j = 0; j < iPage_footer; j++) {
  145. oShowPara[j].style.display = 'none';
  146. oPage_footer[j].className = 'page-footer'
  147. }
  148. }
  149. //进行下一页的切换
  150. function nextPage() {
  151. for (var i = 0; i < iPage_footer; i++) {
  152. if (oShowPara[i].style.display == 'block') {
  153. if (i == iPage_footer - 1) return;
  154. circleList();
  155. oShowPara[(i + 1)].style.display = 'block';
  156. oPage_footer[(i + 1)].className = 'page-footer active';
  157. return
  158. }
  159. }
  160. };
  161. //进行上一页切换
  162. function prePage() {
  163. for (var i = 0; i < iPage_footer; i++) {
  164. if (oShowPara[i].style.display == 'block') {
  165. if (i == 0) return;
  166. circleList();
  167. oShowPara[(i - 1)].style.display = 'block';
  168. oPage_footer[(i - 1)].className = 'page-footer active';
  169. return
  170. }
  171. }
  172. };
  173. oPre.onclick = prePage;
  174. oNext.onclick = nextPage;
  175. </script>
  176. </html>
前台分页的特点为:下载量大,显示慢,加载时用户体验不好。服务器压力请求少,换页时用户体验好。如果有即时性内容,就不能翻回来的时候更新了。

后台分页的特点为:和前台分页特点相反。

总的来说,个人认为,前台分页更适合那些请求数据较少的,静态页面的数据。

另外,感觉自己写的代码好臃肿,请大佬们不吝赐教,溜了溜了~~




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

闽ICP备14008679号