custom-waterfalls-flow.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. <template>
  2. <view class="waterfalls-flow">
  3. <view v-for="(item,index) in data.column" :key="index" class="waterfalls-flow-column" :id="`waterfalls_flow_column_${index+1}`" :msg="msg" :style="{'width':w,'margin-left':index==0?0:m}">
  4. <view :class="['column-value',{'column-value-show':item2.o}]" v-for="(item2,index2) in columnValue(index)" :key="index2" :style="[s1]" @click.stop="wapperClick(item2)">
  5. <view class="inner" v-if="data.seat==1">
  6. <!-- #ifdef MP-WEIXIN -->
  7. <!-- #ifdef VUE2 -->
  8. <slot name="slot{{item2.index}}"></slot>
  9. <!-- #endif -->
  10. <!-- #ifdef VUE3 -->
  11. <slot :name="`slot${item2.index}`"></slot>
  12. <!-- #endif -->
  13. <!-- #endif -->
  14. <!-- #ifndef MP-WEIXIN -->
  15. <slot v-bind="item2"></slot>
  16. <!-- #endif -->
  17. </view>
  18. <image :class="['img',{'img-hide':item2[hideImageKey]==true||item2[hideImageKey]==1},{'img-error':!item2[data.imageKey]}]" :src="item2[data.imageKey]" mode="widthFix" @load="imgLoad(item2,index+1)" @error="imgError(item2,index+1)" @click.stop="imageClick(item2)"></image>
  19. <view class="inner" v-if="data.seat==2">
  20. <!-- #ifdef MP-WEIXIN -->
  21. <!-- #ifdef VUE2 -->
  22. <slot name="slot{{item2.index}}"></slot>
  23. <!-- #endif -->
  24. <!-- #ifdef VUE3 -->
  25. <slot :name="`slot${item2.index}`"></slot>
  26. <!-- #endif -->
  27. <!-- #endif -->
  28. <!-- #ifndef MP-WEIXIN -->
  29. <slot v-bind="item2"></slot>
  30. <!-- #endif -->
  31. </view>
  32. </view>
  33. </view>
  34. </view>
  35. </template>
  36. <script>
  37. export default {
  38. props: {
  39. value: Array,
  40. column: { // 列的数量
  41. type: [String, Number],
  42. default: 2
  43. },
  44. maxColumn: { // 最大列数
  45. type: [String, Number],
  46. default: 5
  47. },
  48. columnSpace: { // 列之间的间距 百分比
  49. type: [String, Number],
  50. default: 2
  51. },
  52. imageKey: { // 图片key
  53. type: [String],
  54. default: 'image'
  55. },
  56. hideImageKey: { // 隐藏图片key
  57. type: [String],
  58. default: 'hide'
  59. },
  60. seat: { // 文本的位置,1图片之上 2图片之下
  61. type: [String, Number],
  62. default: 2
  63. },
  64. listStyle: { // 单个展示项的样式:eg:{'background':'red'}
  65. type: Object
  66. }
  67. },
  68. data() {
  69. return {
  70. data: {
  71. list: this.value ? this.value : [],
  72. column: this.column < 2 ? 2 : this.column,
  73. columnSpace: this.columnSpace <= 5 ? this.columnSpace : 5,
  74. imageKey: this.imageKey,
  75. seat: this.seat
  76. },
  77. msg: 0,
  78. listInitStyle: {
  79. 'border-radius': '12rpx',
  80. 'margin-bottom': '20rpx',
  81. 'background-color': '#fff'
  82. },
  83. adds: [], //预置数据
  84. isLoaded: true,
  85. curIndex: 0,
  86. isRefresh: true,
  87. flag: false,
  88. refreshDatas: []
  89. }
  90. },
  91. computed: {
  92. // 计算列宽
  93. w() {
  94. const column_rate = `${100 / this.data.column - (+this.data.columnSpace)}%`;
  95. return column_rate;
  96. },
  97. // 计算margin
  98. m() {
  99. const column_margin = `${(100-(100 / this.data.column - (+this.data.columnSpace)).toFixed(5)*this.data.column)/(this.data.column-1)}%`;
  100. return column_margin;
  101. },
  102. // list样式
  103. s1() {
  104. return { ...this.listInitStyle, ...this.listStyle };
  105. }
  106. },
  107. created() {
  108. // 初始化
  109. this.refresh();
  110. },
  111. methods: {
  112. // 预加载图片
  113. loadImages(idx = 0) {
  114. let count = 0;
  115. const newList = this.data.list.filter((item, index) => index >= idx);
  116. for (let i = 0; i < newList.length; i++) {
  117. // #ifndef APP-PLUS
  118. uni.getImageInfo({
  119. src: `${newList[i][this.imageKey]}.jpg`,
  120. complete: res => {
  121. count++;
  122. if (count == newList.length) this.initValue(idx);
  123. }
  124. })
  125. // #endif
  126. // #ifdef APP-PLUS
  127. plus.io.getImageInfo({
  128. src: `${newList[i][this.imageKey]}.jpg`,
  129. complete: res => {
  130. count++;
  131. if (count == newList.length) this.initValue(idx);
  132. }
  133. })
  134. // #endif
  135. }
  136. },
  137. // 刷新
  138. refresh() {
  139. if (!this.isLoaded) {
  140. this.refreshDatas = this.value;
  141. return false;
  142. };
  143. setTimeout(() => {
  144. this.refreshDatas = [];
  145. this.isRefresh = true;
  146. this.adds = [];
  147. this.data.list = this.value ? this.value : [];
  148. this.data.column = this.column < 2 ? 2 : this.column >= this.maxColumn ? this.maxColumn : this.column;
  149. this.data.columnSpace = this.columnSpace <= 5 ? this.columnSpace : 5;
  150. this.data.imageKey = this.imageKey;
  151. this.data.seat = this.seat;
  152. this.curIndex = 0;
  153. // 每列的数据初始化
  154. for (let i = 1; i <= this.data.column; i++) {
  155. this.data[`column_${i}_values`] = [];
  156. this.msg++;
  157. }
  158. this.$nextTick(() => {
  159. this.initValue(this.curIndex, 'refresh==>');
  160. })
  161. }, 1)
  162. },
  163. columnValue(index) {
  164. return this.data[`column_${index+1}_values`];
  165. },
  166. change(newValue) {
  167. for (let i = 0; i < this.data.list.length; i++) {
  168. const cv = this.data[`column_${this.data.list[i].column}_values`];
  169. for (let j = 0; j < cv.length; j++) {
  170. if (newValue[i] && i === cv[j].index) {
  171. this.data[`column_${this.data.list[i].column}_values`][j] = Object.assign(cv[j], newValue[i]);
  172. this.msg++;
  173. break;
  174. }
  175. }
  176. }
  177. },
  178. getMin(a, s) {
  179. let m = a[0][s];
  180. let mo = a[0];
  181. for (var i = a.length - 1; i >= 0; i--) {
  182. if (a[i][s] < m) {
  183. m = a[i][s];
  184. }
  185. }
  186. mo = a.filter(i => i[s] == m);
  187. return mo[0];
  188. },
  189. // 计算每列的高度
  190. getMinColumnHeight() {
  191. return new Promise(resolve => {
  192. const heightArr = [];
  193. for (let i = 1; i <= this.data.column; i++) {
  194. const query = uni.createSelectorQuery().in(this);
  195. query.select(`#waterfalls_flow_column_${i}`).boundingClientRect(data => {
  196. heightArr.push({ column: i, height: data.height });
  197. }).exec(() => {
  198. if (this.data.column <= heightArr.length) {
  199. resolve(this.getMin(heightArr, 'height'));
  200. }
  201. });
  202. }
  203. })
  204. },
  205. async initValue(i, from) {
  206. this.isLoaded = false;
  207. if (i >= this.data.list.length || this.refreshDatas.length) {
  208. this.msg++;
  209. this.loaded();
  210. return false;
  211. }
  212. const minHeightRes = await this.getMinColumnHeight();
  213. const c = this.data[`column_${minHeightRes.column}_values`];
  214. this.data.list[i].column = minHeightRes.column;
  215. c.push({ ...this.data.list[i], cIndex: c.length, index: i, o: 0 });
  216. this.msg++;
  217. },
  218. // 图片加载完成
  219. imgLoad(item, c) {
  220. const i = item.index;
  221. item.o = 1;
  222. this.$set(this.data[`column_${c}_values`], item.cIndex, JSON.parse(JSON.stringify(item)));
  223. this.initValue(i + 1);
  224. },
  225. // 图片加载失败
  226. imgError(item, c) {
  227. const i = item.index;
  228. item.o = 1;
  229. item[this.data.imageKey] = null;
  230. this.$set(this.data[`column_${c}_values`], item.cIndex, JSON.parse(JSON.stringify(item)));
  231. this.initValue(i + 1);
  232. },
  233. // 渲染结束
  234. loaded() {
  235. if (this.refreshDatas.length) {
  236. this.isLoaded = true;
  237. this.refresh();
  238. return false;
  239. }
  240. this.curIndex = this.data.list.length;
  241. if (this.adds.length) {
  242. this.data.list = this.adds[0];
  243. this.adds.splice(0, 1);
  244. this.initValue(this.curIndex);
  245. } else {
  246. if (this.data.list.length) this.$emit('loaded');
  247. this.isLoaded = true;
  248. this.isRefresh = false;
  249. }
  250. },
  251. // 单项点击事件
  252. wapperClick(item) {
  253. this.$emit('wapperClick', item);
  254. },
  255. // 图片点击事件
  256. imageClick(item) {
  257. this.$emit('imageClick', item);
  258. }
  259. },
  260. watch: {
  261. value: {
  262. deep: true,
  263. handler(newValue, oldValue) {
  264. setTimeout(() => {
  265. this.$nextTick(() => {
  266. if (this.isRefresh) return false;
  267. if (this.isLoaded) {
  268. // if (newValue.length <= this.curIndex) return this.refresh();
  269. if (newValue.length <= this.curIndex) return this.change(newValue);
  270. this.data.list = newValue;
  271. this.$nextTick(() => {
  272. this.initValue(this.curIndex, 'watch==>');
  273. })
  274. } else {
  275. this.adds.push(newValue);
  276. }
  277. })
  278. }, 10)
  279. }
  280. },
  281. column(newValue) {
  282. this.refresh();
  283. }
  284. }
  285. }
  286. </script>
  287. <style lang="scss" scoped>
  288. .waterfalls-flow {
  289. overflow: hidden;
  290. &-column {
  291. float: left;
  292. }
  293. }
  294. .column-value {
  295. width: 100%;
  296. font-size: 0;
  297. overflow: hidden;
  298. transition: opacity .4s;
  299. opacity: 0;
  300. &-show {
  301. opacity: 1;
  302. }
  303. .inner {
  304. font-size: 30rpx;
  305. }
  306. .img {
  307. width: 100%;
  308. &-hide {
  309. display: none;
  310. }
  311. &-error {
  312. background: #f2f2f2 url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAC4AAAAiAQMAAAAatXkPAAAABlBMVEUAAADMzMzIT8AyAAAAAXRSTlMAQObYZgAAAIZJREFUCNdlzjEKwkAUBNAfEGyCuYBkLyLuxRYW2SKlV1JSeA2tUiZg4YrLjv9PGsHqNTPMSAQuyAJgRDHSyvBPwtZoSJXakeJI9iuRLGDygdl6V0yKDtyMAeMPZySj8yfD+UapvRPj2JOwkyAooSV5IwdDjPdCPspe8LyTl9IKJvDETKKRv6vnlUasgg0fAAAAAElFTkSuQmCC) no-repeat center center;
  313. }
  314. }
  315. }
  316. </style>