maramlee-waterfalls-flow.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <template>
  2. <view class="waterfalls-box" :style="{ height: height + 'px' }">
  3. <!-- #ifdef MP-WEIXIN -->
  4. <view
  5. v-for="(item, index) of list"
  6. class="waterfalls-list"
  7. :key="item[idKey]"
  8. :id="'waterfalls-list-id-' + item[idKey]"
  9. :ref="'waterfalls-list-id-' + item[idKey]"
  10. :style="{
  11. '--offset': offset + 'px',
  12. '--cols': cols,
  13. top: allPositionArr[index].top || 0,
  14. left: allPositionArr[index].left || 0,
  15. }"
  16. @click="$emit('wapper-lick', item)"
  17. >
  18. <view class="pictrue">
  19. <image
  20. class="waterfalls-list-image"
  21. mode="widthFix"
  22. :class="{ single }"
  23. :style="imageStyle"
  24. :src="item[imageSrcKey] || ' '"
  25. @load="imageLoadHandle(index)"
  26. @error="imageLoadHandle(index)"
  27. @click="$emit('image-click', item)"
  28. />
  29. <view class="masks acea-row row-center-wrapper" v-if="item.stock<=0">
  30. <view class="bg">
  31. <view>暂时</view>
  32. <view>售罄</view>
  33. </view>
  34. </view>
  35. </view>
  36. <slot name="slot{{index}}" />
  37. </view>
  38. <!-- #endif -->
  39. <!-- #ifndef MP-WEIXIN -->
  40. <view
  41. v-for="(item, index) of list"
  42. class="waterfalls-list"
  43. :key="item[idKey]"
  44. :id="'waterfalls-list-id-' + item[idKey]"
  45. :ref="'waterfalls-list-id-' + item[idKey]"
  46. :style="{
  47. '--offset': offset + 'px',
  48. '--cols': cols,
  49. ...listStyle,
  50. ...(allPositionArr[index] || {}),
  51. }"
  52. @click="$emit('wapper-lick', item)"
  53. >
  54. <view class="pictrue">
  55. <image
  56. class="waterfalls-list-image"
  57. :class="{ single }"
  58. mode="widthFix"
  59. :style="imageStyle"
  60. :src="item[imageSrcKey] || ' '"
  61. @load="imageLoadHandle(index)"
  62. @error="imageLoadHandle(index)"
  63. @click="$emit('image-click', item)"
  64. />
  65. <view class="masks acea-row row-center-wrapper" v-if="item.stock<=0">
  66. <view class="bg">
  67. <view>暂时</view>
  68. <view>售罄</view>
  69. </view>
  70. </view>
  71. </view>
  72. <slot v-bind="item" />
  73. </view>
  74. <!-- #endif -->
  75. </view>
  76. </template>
  77. <script>
  78. export default {
  79. props: {
  80. list: { type: Array, required: true },
  81. // offset 间距,单位为 px
  82. offset: { type: Number, default: 10 },
  83. // 列表渲染的 key 的键名,值必须唯一,默认为 id
  84. idKey: { type: String, default: "id" },
  85. // 图片 src 的键名
  86. imageSrcKey: { type: String, default: "image" },
  87. // 列数
  88. cols: { type: Number, default: 2, validator: (num) => num >= 2 },
  89. imageStyle: { type: Object },
  90. // 是否是单独的渲染图片的样子,只控制图片圆角而已
  91. single: { type: Boolean, default: false },
  92. // #ifndef MP-WEIXIN
  93. listStyle: { type: Object },
  94. // #endif
  95. },
  96. data() {
  97. return {
  98. topArr: [], // left, right 多个时依次表示第几列的数据
  99. allPositionArr: [], // 保存所有的位置信息
  100. allHeightArr: [], // 保存所有的 height 信息
  101. height: 0, // 外层包裹高度
  102. oldNum: 0,
  103. num: 0,
  104. };
  105. },
  106. created() {
  107. this.refresh();
  108. },
  109. methods: {
  110. imageLoadHandle(index) {
  111. if(!this.list.length){
  112. return
  113. }
  114. const id = "waterfalls-list-id-" + this.list[index][this.idKey],
  115. query = uni.createSelectorQuery().in(this);
  116. query
  117. .select("#" + id)
  118. .fields({ size: true }, (data) => {
  119. this.num++;
  120. this.$set(this.allHeightArr, index, data.height);
  121. if (this.num === this.list.length) {
  122. for (let i = this.oldNum; i < this.num; i++) {
  123. const getTopArrMsg = () => {
  124. let arrtmp = [...this.topArr].sort((a, b) => a - b);
  125. return {
  126. shorterIndex: this.topArr.indexOf(arrtmp[0]),
  127. shorterValue: arrtmp[0],
  128. longerIndex: this.topArr.indexOf(arrtmp[this.cols - 1]),
  129. longerValue: arrtmp[this.cols - 1],
  130. };
  131. };
  132. const { shorterIndex, shorterValue } = getTopArrMsg();
  133. const position = {
  134. top: shorterValue + "px",
  135. left: (data.width + this.offset) * shorterIndex + "px",
  136. };
  137. this.$set(this.allPositionArr, i, position);
  138. this.topArr[shorterIndex] =
  139. shorterValue + this.allHeightArr[i] + this.offset;
  140. this.height = getTopArrMsg().longerValue - this.offset;
  141. }
  142. this.oldNum = this.num;
  143. // 完成渲染 emit `image-load` 事件
  144. this.$emit("image-load");
  145. }
  146. })
  147. .exec();
  148. },
  149. refresh() {
  150. let arr = [];
  151. for (let i = 0; i < this.cols; i++) {
  152. arr.push(0);
  153. }
  154. this.topArr = arr;
  155. this.num = 0;
  156. this.oldNum = 0;
  157. this.height = 0;
  158. },
  159. },
  160. };
  161. </script>
  162. <style lang="scss" scoped>
  163. // 这里可以自行配置
  164. $border-radius: 10px;
  165. .waterfalls-box {
  166. position: relative;
  167. width: 100%;
  168. overflow: hidden;
  169. .waterfalls-list {
  170. width: calc((100% - var(--offset) * (var(--cols) - 1)) / var(--cols));
  171. position: absolute;
  172. background-color: #fff;
  173. border-radius: $border-radius;
  174. // 防止刚开始渲染时堆叠在第一幅图的地方
  175. left: calc(-50% - var(--offset));
  176. .pictrue{
  177. position: relative;
  178. .masks{
  179. position: absolute;
  180. top: 0;
  181. left: 0;
  182. right: 0;
  183. bottom: 0;
  184. background: rgba(0, 0, 0, 0.2);
  185. border-radius: 20rpx 20rpx 0 0;
  186. .bg{
  187. width: 152rpx;
  188. height: 152rpx;
  189. background: #000000;
  190. opacity: 0.6;
  191. color: #fff;
  192. font-size: 32rpx;
  193. border-radius: 50%;
  194. padding: 34rpx 0;
  195. text-align: center;
  196. }
  197. }
  198. }
  199. .waterfalls-list-image {
  200. width: 100%;
  201. height: 382rpx!important;
  202. will-change: transform;
  203. border-radius: $border-radius $border-radius 0 0;
  204. display: block;
  205. &.single {
  206. border-radius: $border-radius;
  207. }
  208. }
  209. }
  210. }
  211. </style>