WaterfallsFlow.vue 5.8 KB

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