WaterfallsFlow.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <template>
  2. <view :class="'wf-page wf-page' + type">
  3. <!-- left -->
  4. <view>
  5. <view id="left" v-if="leftList.length">
  6. <view v-for="(item, index) in leftList" :key="index" class="wf-item" @tap="itemTap(item)">
  7. <WaterfallsFlowItem :item="item" :isStore="isStore" :type="type" :recommend="recommend" :goDetail="goDetail" />
  8. </view>
  9. </view>
  10. </view>
  11. <!-- right -->
  12. <view>
  13. <view id="right" v-if="rightList.length">
  14. <view v-for="(item, index) in rightList" :key="index" class="wf-item" @tap="itemTap(item)">
  15. <WaterfallsFlowItem :item="item" :isStore="isStore" :type="type" :recommend="recommend" :goDetail="goDetail" />
  16. </view>
  17. </view>
  18. </view>
  19. </view>
  20. </template>
  21. <script>
  22. import WaterfallsFlowItem from './WaterfallsFlowItem.vue';
  23. export default {
  24. components: {
  25. WaterfallsFlowItem
  26. },
  27. props: {
  28. // 瀑布流列表
  29. wfList: {
  30. type: Array,
  31. require: true
  32. },
  33. updateNum: {
  34. type: Number,
  35. default: 10
  36. },
  37. type: {
  38. type: Number,
  39. default: 0
  40. },
  41. isStore: {
  42. type: [String, Number],
  43. default: '1'
  44. },
  45. recommend: {
  46. type: Boolean,
  47. default: false
  48. },
  49. goDetail: {
  50. type: String,
  51. default: ''
  52. }
  53. },
  54. data() {
  55. return {
  56. allList: [], // 全部列表
  57. leftList: [], // 左边列表
  58. rightList: [], // 右边列表
  59. mark: 0, // 列表标记
  60. boxHeight: [] // 下标0和1分别为左列和右列高度
  61. };
  62. },
  63. watch: {
  64. // 监听列表数据变化
  65. wfList: {
  66. handler(nVal, oVal) {
  67. // 如果数据为空或新的列表数据少于旧的列表数据(通常为下拉刷新或切换排序或使用筛选器),初始化变量
  68. if (!this.wfList.length || (this.wfList.length === this.updateNum && this.wfList.length <= this.allList.length)) {
  69. this.allList = [];
  70. this.leftList = [];
  71. this.rightList = [];
  72. this.boxHeight = [];
  73. this.mark = 0;
  74. }
  75. // 如果列表有值,调用waterfall方法
  76. if (this.wfList.length) {
  77. this.allList = this.wfList;
  78. this.leftList = [];
  79. this.rightList = [];
  80. this.boxHeight = [];
  81. this.allList.forEach((v, i) => {
  82. if (this.allList.length < 3 || (this.allList.length <= 7 && this.allList.length - i > 1) || (this.allList.length > 7 && this.allList.length - i > 2)) {
  83. if (i % 2) {
  84. this.rightList.push(v);
  85. } else {
  86. this.leftList.push(v);
  87. }
  88. }
  89. });
  90. if (this.allList.length < 3) {
  91. this.mark = this.allList.length + 1;
  92. } else if (this.allList.length <= 7) {
  93. this.mark = this.allList.length - 1;
  94. } else {
  95. this.mark = this.allList.length - 2;
  96. }
  97. if (this.mark < this.allList.length) {
  98. this.waterFall();
  99. }
  100. }
  101. },
  102. immediate: true,
  103. deep: true
  104. },
  105. mounted() {},
  106. // 监听标记,当标记发生变化,则执行下一个item排序
  107. mark() {
  108. const len = this.allList.length;
  109. if (this.mark < len && this.mark !== 0 && this.boxHeight.length) {
  110. this.waterFall();
  111. }
  112. }
  113. },
  114. methods: {
  115. // 瀑布流排序
  116. waterFall() {
  117. const i = this.mark;
  118. if (i == 0) {
  119. // 初始化,从左边开始插入
  120. this.leftList.push(this.allList[i]);
  121. // 更新左边列表高度
  122. this.getViewHeight(0);
  123. } else if (i == 1) {
  124. // 第二个item插入,默认为右边插入
  125. this.rightList.push(this.allList[i]);
  126. // 更新右边列表高度
  127. this.getViewHeight(1);
  128. } else {
  129. // 根据左右列表高度判断下一个item应该插入哪边
  130. if (!this.boxHeight.length) {
  131. this.rightList.length < this.leftList.length ? this.rightList.push(this.allList[i]) : this.leftList.push(this.allList[i]);
  132. } else {
  133. const leftOrRight = this.boxHeight[0] > this.boxHeight[1] ? 1 : 0;
  134. if (leftOrRight) {
  135. this.rightList.push(this.allList[i]);
  136. } else {
  137. this.leftList.push(this.allList[i]);
  138. }
  139. }
  140. // 更新插入列表高度
  141. this.getViewHeight();
  142. }
  143. },
  144. // 获取列表高度
  145. getViewHeight() {
  146. // 使用nextTick,确保页面更新结束后,再请求高度
  147. this.$nextTick(() => {
  148. setTimeout(() => {
  149. uni
  150. .createSelectorQuery()
  151. .in(this)
  152. .select('#right')
  153. .boundingClientRect((res) => {
  154. res ? (this.boxHeight[1] = res.height) : '';
  155. uni
  156. .createSelectorQuery()
  157. .in(this)
  158. .select('#left')
  159. .boundingClientRect((res) => {
  160. res ? (this.boxHeight[0] = res.height) : '';
  161. this.mark = this.mark + 1;
  162. })
  163. .exec();
  164. })
  165. .exec();
  166. }, 100);
  167. });
  168. },
  169. // item点击
  170. itemTap(item) {
  171. this.$emit('itemTap', item);
  172. },
  173. // item点击
  174. goShop(item) {
  175. this.$emit('goShop', item);
  176. }
  177. }
  178. };
  179. </script>
  180. <style lang="scss" scoped>
  181. $page-padding: 10px;
  182. $grid-gap: 10px;
  183. .wf-page {
  184. display: grid;
  185. grid-template-columns: 1fr 1fr;
  186. grid-gap: $grid-gap;
  187. }
  188. .wf-item {
  189. width: calc((100vw - 2 * #{$page-padding} - #{$grid-gap}) / 2);
  190. padding-bottom: $grid-gap;
  191. }
  192. .wf-page1 .wf-item {
  193. margin-top: 20rpx;
  194. background-color: #fff;
  195. border-radius: 20rpx;
  196. padding-bottom: 0;
  197. }
  198. </style>