Square.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <template>
  2. <div class="square-container">
  3. <div class="title">全部任务</div>
  4. <div class="square-list-container">
  5. <task-item v-for="(item,index) in list" :key="index" :task="item" />
  6. <div v-if="loading" class="loading">
  7. <span></span>
  8. <span></span>
  9. <span></span>
  10. <span></span>
  11. <span></span>
  12. <span></span>
  13. <span></span>
  14. </div>
  15. <div v-if="nomore" class="nomore">没有更多</div>
  16. </div>
  17. </div>
  18. </template>
  19. <script>
  20. import TaskItem from "@/components/commons/TaskItem";
  21. import Http from '@/js/http.js';
  22. import Apis from '@/js/api.js';
  23. import {notify} from '@/constants/index'
  24. export default {
  25. name: "Square",
  26. components: { TaskItem },
  27. data() {
  28. return {
  29. loading: true,
  30. nomore:false,
  31. list: [],
  32. };
  33. },
  34. mounted() {
  35. window.addEventListener("scroll", this.throttle(this.setpage, 1000), false);
  36. },
  37. created() {
  38. this.loadData()
  39. },
  40. methods: {
  41. throttle(func, wait, options) {
  42. let context, args, result;
  43. let timeout = null;
  44. let previous = 0;
  45. if (!options) options = {};
  46. let later = function() {
  47. previous = options.leading === false ? 0 : Number(new Date());
  48. timeout = null;
  49. result = func.apply(context, args);
  50. if (!timeout) context = args = null;
  51. };
  52. return (..._args) => {
  53. let now = Number(new Date());
  54. if (!previous && options.leading === false) previous = now;
  55. let remaining = wait - (now - previous);
  56. context = this;
  57. args = _args;
  58. if (remaining <= 0 || remaining > wait) {
  59. clearTimeout(timeout);
  60. timeout = null;
  61. previous = now;
  62. result = func.apply(context, args);
  63. if (!timeout) context = args = null;
  64. } else if (!timeout && options.trailing !== false) {
  65. timeout = setTimeout(later, remaining);
  66. }
  67. return result;
  68. };
  69. },
  70. setpage() {
  71. if (this.nomore && !this.loading) return; //到达底部不再执行
  72. var scrollTop =
  73. document.documentElement.scrollTop || document.body.scrollTop;
  74. //变量windowHeight是可视区的高度
  75. var windowHeight =
  76. document.documentElement.clientHeight || document.body.clientHeight;
  77. //变量scrollHeight是滚动条的总高度
  78. var scrollHeight =
  79. document.documentElement.scrollHeight || document.body.scrollHeight;
  80. // console.log(scrollTop, windowHeight, scrollHeight);
  81. //滚动条到底部的条件
  82. if (scrollTop + windowHeight + 5 >= scrollHeight) {
  83. console.log("加载更多数据");
  84. // this.loading=true;
  85. //获取数据请求 请求结束后
  86. //成功设置 this.list =[...this.list,...this.list1]; this.loading=false;
  87. //失败设置 没有更多 this.nomore = true; this.loading = false;
  88. // window.setTimeout(()=>{this.loading=false;
  89. // console.log(this.list);},5000)
  90. }
  91. else{this.loading=false;}
  92. window.clearTimeout();
  93. },
  94. loadData() {
  95. console.log("loadData")
  96. Http.get(Apis.PAGE.SQUARE_PAGE).then((res) => {
  97. this.list = res.crowdTaskVOList
  98. })
  99. }
  100. }
  101. };
  102. </script>
  103. <style lang="less" scoped>
  104. .square-container {
  105. padding: 40px 80px;
  106. }
  107. .nomore{
  108. text-align: center;
  109. font-size: 1.4rem;
  110. color: #8a8a8a;
  111. padding: 5px;
  112. }
  113. </style>