index.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <template>
  2. <view>
  3. <view class="waterfall_left">
  4. <view class="waterfall_list" v-for="(item,index) in goodsLeftList" :key="index">
  5. <view class="waterfall_list_img">
  6. <u-image :src="item.goods_main_pic" mode="widthFix" @load="considerPush"></u-image>
  7. </view>
  8. <view class="msg-box">
  9. <view class="name single-omit">{{item.goods_name}}</view>
  10. <view class="price-box flex-align-center">
  11. <view class="unit"><text>¥</text>{{item.goods_sku.sku_sale_price}}</view>
  12. </view>
  13. </view>
  14. </view>
  15. </view>
  16. <view class="waterfall_right">
  17. <view class="waterfall_list" v-for="(item,index) in goodsRightList" :key="index">
  18. <view class="waterfall_list_img">
  19. <u-image :src="item.goods_main_pic" mode="widthFix" @load="considerPush"></u-image>
  20. </view>
  21. <view class="msg-box">
  22. <view class="name single-omit">{{item.goods_name}}</view>
  23. <view class="price-box flex-align-center">
  24. <view class="unit"><text>¥</text>{{item.goods_sku.sku_sale_price}}</view>
  25. </view>
  26. </view>
  27. </view>
  28. </view>
  29. </view>
  30. </template>
  31. <script>
  32. export default {
  33. props: {
  34. list: {
  35. type: Array,
  36. required: true
  37. },
  38. },
  39. data() {
  40. return {
  41. // 左侧商品列表
  42. goodsLeftList: [],
  43. // 右侧商品列表
  44. goodsRightList: [],
  45. // 组件数据备份
  46. newList: [],
  47. }
  48. },
  49. created() {
  50. this.touchOff(); // 触发排列
  51. },
  52. mounted() {},
  53. watch: {
  54. list(newValue, oldValue) {
  55. this.touchOff()
  56. },
  57. },
  58. computed: {},
  59. methods: {
  60. // 触发重新排列
  61. touchOff() {
  62. this.newList = [...this.list];
  63. this.goodsLeftList = [];
  64. this.goodsRightList = [];
  65. if (this.newList.length != 0) {
  66. this.goodsLeftList.push(this.newList.shift()); //触发排列
  67. }
  68. },
  69. // 计算排列
  70. considerPush() {
  71. if (this.newList.length == 0) return; //没有数据了
  72. let leftH = 0,
  73. rightH = 0; //左右高度
  74. var query = uni.createSelectorQuery().in(this);
  75. query.selectAll('.waterfall_left').boundingClientRect()
  76. query.selectAll('.waterfall_right').boundingClientRect()
  77. query.exec(res => {
  78. leftH = res[0].length != 0 ? res[0][0].height : 0; //防止查询不到做个处理
  79. rightH = res[1].length != 0 ? res[1][0].height : 0;
  80. if (leftH == rightH || leftH < rightH) {
  81. // 相等 || 左边小
  82. this.goodsLeftList.push(this.newList.shift());
  83. } else {
  84. // 右边小
  85. this.goodsRightList.push(this.newList.shift());
  86. }
  87. });
  88. },
  89. }
  90. }
  91. </script>