new_list.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. $(function () {
  2. // 图片懒加载
  3. $("img").lazyload();
  4. // 显示区高度
  5. let ch = document.documentElement.clientHeight || document.body.clientHeight;
  6. let lock = false;
  7. // 加载 数据
  8. let currentPage = 3, totalPage = 3;
  9. $(window).scroll(function () {
  10. if (lock) return false;
  11. // 滑动区高度
  12. let sh = document.documentElement.scrollHeight || document.body.scrollHeight;
  13. // 滑动位置
  14. let st = document.documentElement.scrollTop || document.body.scrollTop;
  15. let c = sh * 1 - ch * 1 - st * 1;
  16. if (c == 0 && currentPage <= totalPage) {
  17. lock = true;
  18. $(".my-loading").show();
  19. data({ currentPage }).then(res => { console.log(res.totalPage); totalPage = res.totalPage; currentPage++; lock = res.lock; $(".my-loading").hide(); })
  20. }
  21. })
  22. // 加载数据
  23. function data (data) {
  24. return new Promise(resolve => {
  25. $.ajax({
  26. url: "http://192.168.2.45:8033/member/news/",
  27. data: {
  28. ...data,
  29. pageSize: 10
  30. },
  31. type: "get",
  32. error: function () {
  33. $(".my-loading").html("已经没有可以加载的数据了")
  34. },
  35. success: function (res) {
  36. let { dataList, totalPage } = res.data;
  37. console.log(res.data)
  38. console.log(totalPage)
  39. let father = document.createDocumentFragment();
  40. Array.from(dataList).forEach(
  41. item => {
  42. let str = `<a target="_blank" href="http://www.gaokaoapp.net/h_exam/${item.id}.html" class="list-item">
  43. <div class="item-img">
  44. <img data-original="${item.thumbnail}"
  45. src="../imgs/app.png"
  46. alt="">
  47. </div>
  48. <div class="item-detail">
  49. <div class="title">${item.title}</div>
  50. <div class="time">${item.source}<span>${item.delay_time}</span></div>
  51. </div>
  52. </a>`
  53. $(father).append(str);
  54. }
  55. )
  56. $(".list").append(father);
  57. $("img").lazyload();
  58. resolve({ lock: false, totalPage })
  59. }
  60. })
  61. })
  62. }
  63. })