mosowe-invented-list.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <!--
  2. 虚拟列表
  3. @Author: mosowe
  4. @Date:2023-03-07 09:14:40
  5. -->
  6. <template>
  7. <view class="mosowe-invented-list">
  8. <scroll-view
  9. class="container"
  10. scroll-y
  11. :style="{ height: boxHeight + 'px' }"
  12. @scroll="handleScroll">
  13. <div
  14. class="mosowe-invented-wrap"
  15. :style="{ height: itemHeight * list.length + 'px' }">
  16. <div
  17. class="mosowe-invented-content"
  18. :style="{ transform: 'translateY(' + offsetY + 'px)' }">
  19. <view
  20. class="mosowe-invented-item"
  21. v-for="(item, index) in showList"
  22. :key="index">
  23. <slot :item="item"></slot>
  24. </view>
  25. </div>
  26. </div>
  27. </scroll-view>
  28. </view>
  29. </template>
  30. <script>
  31. export default {
  32. props: {
  33. list: {
  34. type: Array,
  35. default: () => []
  36. },
  37. cacheNum: {
  38. // 前后缓存数目
  39. type: Number,
  40. default: 50
  41. }
  42. },
  43. data() {
  44. return {
  45. showList: [], // 展示的数据列表
  46. boxHeight: 0, // 组件可视区高度
  47. itemHeight: 0, // 每条数据高度,计算第一条数据的高度,以第一条数据高度为主
  48. offsetY: 0
  49. };
  50. },
  51. computed: {
  52. pageNum() {
  53. if (this.boxHeight && this.itemHeight) {
  54. return Math.ceil(this.boxHeight / this.itemHeight) + this.cacheNum;
  55. } else {
  56. return this.cacheNum;
  57. }
  58. }
  59. },
  60. watch: {
  61. pageNum() {
  62. this.setShowList(0);
  63. },
  64. list: {
  65. handler() {
  66. this.init();
  67. },
  68. deep: true,
  69. immediate: true
  70. }
  71. },
  72. mounted() {
  73. this.init();
  74. },
  75. methods: {
  76. init() {
  77. this.$nextTick(() => {
  78. this.setShowList(0);
  79. let t = setTimeout(() => {
  80. clearTimeout(t);
  81. t = null;
  82. const query = uni.createSelectorQuery().in(this);
  83. query
  84. .select('.mosowe-invented-list')
  85. .boundingClientRect((res) => {
  86. this.boxHeight = Math.floor(res?.height) || 0;
  87. })
  88. .select('.mosowe-invented-item')
  89. .boundingClientRect((res) => {
  90. this.itemHeight = Math.floor(res?.height) || 0;
  91. })
  92. .exec();
  93. }, 100);
  94. });
  95. },
  96. handleScroll(e) {
  97. const scrollTop = Math.floor(e.detail.scrollTop);
  98. this.offsetY = scrollTop - (scrollTop % this.itemHeight);
  99. let startIndex = Math.floor(scrollTop / this.itemHeight);
  100. if (startIndex > this.cacheNum) {
  101. this.offsetY -= this.cacheNum * this.itemHeight;
  102. startIndex = startIndex - this.cacheNum;
  103. }
  104. this.setShowList(startIndex);
  105. this.$emit('scroll', scrollTop);
  106. },
  107. setShowList(startIndex) {
  108. this.showList = this.list.slice(startIndex, startIndex + this.pageNum);
  109. }
  110. }
  111. };
  112. </script>
  113. <style lang="scss" scoped>
  114. .mosowe-invented-list {
  115. overflow: hidden;
  116. height: 100%;
  117. width: 100%;
  118. .container {
  119. width: 100%;
  120. height: 100%;
  121. }
  122. }
  123. </style>