WaterfallsFlow.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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" :recommend="recommend"/>
  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" :recommend="recommend"/>
  18. </view>
  19. </view>
  20. </view>
  21. </view>
  22. </template>
  23. <script>
  24. import WaterfallsFlowItem from './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. recommend:{
  48. type: Boolean,
  49. default: false
  50. }
  51. },
  52. data() {
  53. return {
  54. allList: [], // 全部列表
  55. leftList: [], // 左边列表
  56. rightList: [], // 右边列表
  57. mark: 0, // 列表标记
  58. boxHeight: [], // 下标0和1分别为左列和右列高度
  59. };
  60. },
  61. watch: {
  62. // 监听列表数据变化
  63. wfList: {
  64. handler(nVal,oVal){
  65. // 如果数据为空或新的列表数据少于旧的列表数据(通常为下拉刷新或切换排序或使用筛选器),初始化变量
  66. if (!this.wfList.length ||
  67. (this.wfList.length === this.updateNum && this.wfList.length <= this.allList.length)) {
  68. this.allList = [];
  69. this.leftList = [];
  70. this.rightList = [];
  71. this.boxHeight = [];
  72. this.mark = 0;
  73. }
  74. // 如果列表有值,调用waterfall方法
  75. if (this.wfList.length) {
  76. this.allList = this.wfList;
  77. this.leftList = [];
  78. this.rightList = [];
  79. this.boxHeight = [];
  80. this.allList.forEach((v, i) => {
  81. if(this.allList.length < 3 || (this.allList.length <= 7 && this.allList.length - i > 1) || (this.allList.length > 7 && this.allList.length - i > 2)) {
  82. if(i % 2){
  83. this.rightList.push(v);
  84. }else{
  85. this.leftList.push(v);
  86. }
  87. }
  88. });
  89. if(this.allList.length < 3){
  90. this.mark = this.allList.length+1;
  91. }else if(this.allList.length <= 7){
  92. this.mark = this.allList.length - 1;
  93. }else{
  94. this.mark = this.allList.length - 2;
  95. }
  96. if(this.mark < this.allList.length){
  97. this.waterFall()
  98. }
  99. }
  100. },
  101. immediate: true,
  102. deep:true
  103. },
  104. mounted(){
  105. },
  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
  132. ? this.rightList.push(this.allList[i])
  133. : this.leftList.push(this.allList[i]);
  134. } else {
  135. const leftOrRight = this.boxHeight[0] > this.boxHeight[1] ? 1 : 0;
  136. if (leftOrRight) {
  137. this.rightList.push(this.allList[i])
  138. } else {
  139. this.leftList.push(this.allList[i])
  140. }
  141. }
  142. // 更新插入列表高度
  143. this.getViewHeight();
  144. }
  145. },
  146. // 获取列表高度
  147. getViewHeight() {
  148. // 使用nextTick,确保页面更新结束后,再请求高度
  149. this.$nextTick(() => {
  150. setTimeout(()=>{
  151. uni.createSelectorQuery().in(this).select('#right').boundingClientRect(res => {
  152. res ? this.boxHeight[1] = res.height : '';
  153. uni.createSelectorQuery().in(this).select('#left').boundingClientRect(res => {
  154. res ? this.boxHeight[0] = res.height : '';
  155. this.mark = this.mark + 1;
  156. }).exec();
  157. }).exec();
  158. },100)
  159. })
  160. },
  161. // item点击
  162. itemTap(item) {
  163. this.$emit('itemTap', item)
  164. },
  165. // item点击
  166. goShop(item) {
  167. this.$emit('goShop', item)
  168. }
  169. }
  170. }
  171. </script>
  172. <style lang="scss" scoped>
  173. $page-padding: 10px;
  174. $grid-gap: 10px;
  175. .wf-page {
  176. display: grid;
  177. grid-template-columns: 1fr 1fr;
  178. grid-gap: $grid-gap;
  179. }
  180. .wf-item {
  181. width: calc((100vw - 2 * #{$page-padding} - #{$grid-gap}) / 2);
  182. padding-bottom: $grid-gap;
  183. }
  184. .wf-page1 .wf-item{
  185. margin-top: 20rpx;
  186. background-color: #fff;
  187. border-radius: 20rpx;
  188. padding-bottom: 0;
  189. }
  190. </style>