WaterfallsFlow.vue 6.4 KB

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