tm-flowLayout-custom.vue 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <template>
  2. <view class="tm-flowLayout relative">
  3. <view class="tm-flowLayout-hidden absolute fulled">
  4. <view v-for="(item, index) in list2" :key="index" style="width: 350rpx;" :class="'tm-flowLayout-hidden-'+index">
  5. <tm-images @error="loadimg($event,false,index)" @load="loadimg($event,true,index)" :src="item.spec.url"></tm-images>
  6. </view>
  7. </view>
  8. <view class="tm-flowLayout-cm flex-between">
  9. <view class="tm-flowLayout-left" style="width: 48.6%;">
  10. <block v-for="(item, index) in dataList[0]" :key="index">
  11. <view @click.stop="onclick(index,0)" class="tm-flowLayout-item mb-20 fulled" :class="['tm-flowLayout-item_'+index]">
  12. <tm-translate animation-name="fadeUp">
  13. <view class="card round-3 overflow white">
  14. <tm-images :previmage="false" :src="item.spec.url"></tm-images>
  15. </view>
  16. </tm-translate>
  17. </view>
  18. </block>
  19. </view>
  20. <view class="tm-flowLayout-right" style="width: 48.6%;">
  21. <block v-for="(item, index) in dataList[1]" :key="index">
  22. <view @click.stop="onclick(index,1)" class="tm-flowLayout-item mb-20 fulled" :class="['tm-flowLayout-item_'+index]">
  23. <tm-translate animation-name="fadeUp">
  24. <view class="card round-3 overflow white">
  25. <tm-images :previmage="false" :src="item.spec.url"></tm-images>
  26. </view>
  27. </tm-translate>
  28. </view>
  29. </block>
  30. </view>
  31. </view>
  32. <view v-if="isLoading" class="flex-shrink fulled">
  33. <tm-loadding></tm-loadding>
  34. </view>
  35. </view>
  36. </template>
  37. <script>
  38. /**
  39. * 瀑布流组件
  40. * @property {Function} click 点击项目时触发
  41. * @property {Function} load 当前列表加载完成发出的事件。
  42. * @property {String} model = [rank|desc] desc下正时排序,所有加载完成再进行显示,rank随机,哪个先加载哪个先排前面。
  43. */
  44. import tmImages from "@/tm-vuetify/components/tm-images/tm-images.vue"
  45. import tmLoadding from "@/tm-vuetify/components/tm-loadding/tm-loadding.vue"
  46. import tmIcons from "@/tm-vuetify/components/tm-icons/tm-icons.vue"
  47. import tmTranslate from "@/tm-vuetify/components/tm-translate/tm-translate.vue"
  48. export default {
  49. components: {tmImages, tmLoadding, tmIcons, tmTranslate},
  50. name: 'tm-flowLayout',
  51. props: {
  52. model: {
  53. type: String,
  54. default: 'rank' //desc下正时排序,所有加载完成再进行显示,rank随机,哪个先加载哪个先排前面。
  55. }
  56. },
  57. data() {
  58. return {
  59. list2: [],
  60. dataList: [
  61. [], []
  62. ],
  63. colHeight: [0, 0],
  64. minWidth: 0,
  65. isLoading: true,
  66. };
  67. },
  68. async mounted() {
  69. let p = await this.$Querey('.tm-flowLayout-left').catch(e => {
  70. });
  71. this.minWidth = p[0].width;
  72. },
  73. methods: {
  74. onclick(index, dirIndex) {
  75. this.$emit('click',
  76. {
  77. childrenIndex: index,
  78. dirIndex: dirIndex,
  79. item: this.dataList[dirIndex][index]
  80. });
  81. },
  82. //修改或者替换列表数据
  83. changeItemData(dirIndex, childrenIndex, item) {
  84. this.dataList[dirIndex].splice(childrenIndex, 1, item);
  85. },
  86. //删除一项列表数据
  87. delItemData(dirIndex, childrenIndex) {
  88. this.colHeight.splice(dirIndex, 1, this.colHeight[dirIndex] - this.dataList[dirIndex][childrenIndex].height)
  89. this.dataList[dirIndex].splice(childrenIndex, 1);
  90. this.list2.splice(this.dataList[dirIndex][childrenIndex].index, 1);
  91. this.$nextTick(function () {
  92. this.sucessRank();
  93. })
  94. },
  95. //向列表添加数据
  96. pushData(list) {
  97. console.log('添加图片数据----------',list)
  98. let prIdx_i = this.list2.length;
  99. if (!Array.isArray(list) || typeof list == 'undefined') {
  100. return false;
  101. }
  102. for (let i = 0; i < list.length; i++) {
  103. this.list2.push({
  104. index: i + prIdx_i,
  105. isLoad: false,
  106. image: "",
  107. width: 0,
  108. height: 0,
  109. ...list[i]
  110. })
  111. }
  112. },
  113. //获取内部列表数据
  114. getDataList() {
  115. return this.dataList;
  116. },
  117. async loadimg(event, isLoad, index) {
  118. console.log('图片加载事件----------',event)
  119. this.isLoading = true;
  120. let ps = this.list2[index];
  121. ps.isLoad = true;
  122. if (isLoad == false) {
  123. ps.width = this.minWidth;
  124. ps.height = this.minWidth;
  125. } else {
  126. ps.width = this.minWidth;
  127. ps.height = ps.height + event.height;
  128. }
  129. if (this.list2.length == 0 && this.dataList[0].length == 0 && this.dataList[1].length == 0) {
  130. this.isLoading = false;
  131. return;
  132. }
  133. this.list2.splice(index, 1, ps);
  134. if (this.model == 'desc') {
  135. this.sucessRank();
  136. } else if (this.model == 'rank') {
  137. let indexCol = this.colHeight[0] <= this.colHeight[1] ? 0 : 1;
  138. this.dataList[indexCol].push(this.list2[index]);
  139. this.colHeight.splice(indexCol, 1, this.colHeight[indexCol] + this.list2[index].height);
  140. if (this.isAllLoading() === false) return;
  141. this.isLoading = false;
  142. }
  143. },
  144. isAllLoading() {
  145. let isAllload = true;
  146. for (let i = 0; i < this.list2.length; i++) {
  147. if (this.list2[i].isLoad === false) {
  148. isAllload = false;
  149. break;
  150. }
  151. }
  152. if (isAllload === true) {
  153. // 当前列表加载完成发出的事件。
  154. this.$emit('load', this.dataList)
  155. }
  156. return isAllload;
  157. },
  158. sucessRank() {
  159. if (this.isAllLoading() === false) return;
  160. this.isLoading = false;
  161. for (let i = 0; i < this.list2.length; i++) {
  162. // let p = await this.getLeftRightHeight();
  163. let index = this.colHeight[0] <= this.colHeight[1] ? 0 : 1;
  164. this.dataList[index].push(this.list2[i]);
  165. this.colHeight.splice(index, 1, this.colHeight[index] + this.list2[i].height)
  166. }
  167. },
  168. //清空瀑布流数据 。
  169. clear() {
  170. this.list2 = [];
  171. this.dataList = [[], []];
  172. this.isLoading = false;
  173. },
  174. getLeftRightHeight(index) {
  175. let t = this;
  176. return new Promise((resolve, rejecvt) => {
  177. let q = uni.createSelectorQuery().in(t);
  178. q.select(".tm-flowLayout-left").boundingClientRect();
  179. q.select(".tm-flowLayout-right").boundingClientRect();
  180. q.exec((res) => {
  181. resolve([res[0].height, res[1].height])
  182. })
  183. })
  184. }
  185. },
  186. };
  187. </script>
  188. <style lang="scss">
  189. .tm-flowLayout {
  190. .tm-flowLayout-hidden {
  191. width: 100%;
  192. height: 100px;
  193. overflow-y: auto;
  194. left: -300%;
  195. top: 0;
  196. }
  197. .tm-flowLayout-cm {
  198. align-items: flex-start;
  199. }
  200. .card {
  201. box-shadow: 0rpx 4rpx 24rpx rgba(0, 0, 0, 0.03);
  202. }
  203. }
  204. </style>