maramlee-waterfalls-flow.vue 6.1 KB

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