tui-waterfall.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <template>
  2. <view class="tui-waterfall__box"
  3. :style="{ paddingLeft: leftGap, paddingRight: rightGap, background: backgroundColor, borderRadius: radius }">
  4. <view class="tui-waterfall__list" id="tui-waterfall__left" :style="{ marginRight: columnGap }">
  5. <view v-for="(item, index) in leftList" :key="index">
  6. <slot name="left" :entity="item" :index="index" :isList="columnCount==1"></slot>
  7. </view>
  8. </view>
  9. <view v-if="columnCount > 1" class="tui-waterfall__list" id="tui-waterfall__right">
  10. <view v-for="(item, index) in rightList" :key="index">
  11. <slot name="right" :entity="item" :index="index" :isList="columnCount==1"></slot>
  12. </view>
  13. </view>
  14. </view>
  15. </template>
  16. <script>
  17. /**
  18. * 列表中图片可以使用懒加载或者其他方式
  19. * 优先计算出图片高度,以达到最佳效果
  20. * App端建议使用weex的waterfall组件,使用案例详见:
  21. * [ThorUI组件库->pages->index->productNvue]
  22. */
  23. export default {
  24. name: 'tuiWaterfall',
  25. // #ifdef MP-WEIXIN
  26. options: {
  27. multipleSlots: true
  28. },
  29. // #endif
  30. props: {
  31. //列表数据,不建议一次性加载过多数据
  32. listData: {
  33. type: Array,
  34. default () {
  35. return [];
  36. }
  37. },
  38. //每页数据条数(固定条数),当总数据长度小于等于该数时表示第一页数据,刷新重置
  39. pageSize: {
  40. type: Number,
  41. default: 10
  42. },
  43. //数据分组类型:1-简单左右分组,按顺序排列,伪瀑布流 2-计算左右容器高度进行分组
  44. type: {
  45. type: Number,
  46. default: 1
  47. },
  48. //瀑布流列数,目前支持最大值为2
  49. columnCount: {
  50. type: Number,
  51. default: 2
  52. },
  53. //列与列的间隙
  54. columnGap: {
  55. type: String,
  56. default: '10rpx'
  57. },
  58. //左侧和列表的间隙
  59. leftGap: {
  60. type: String,
  61. default: '0'
  62. },
  63. //右侧和列表的间隙
  64. rightGap: {
  65. type: String,
  66. default: '0'
  67. },
  68. //列表背景色,可使用渐变色
  69. backgroundColor: {
  70. type: String,
  71. default: 'transparent'
  72. },
  73. //列表外层容器圆角值
  74. radius: {
  75. type: String,
  76. default: '0'
  77. }
  78. },
  79. data() {
  80. return {
  81. leftListConst: [],
  82. leftList: [],
  83. rightList: [],
  84. init: true
  85. };
  86. },
  87. watch: {
  88. listData(val) {
  89. if (!this.init) {
  90. this.columnChange();
  91. }
  92. },
  93. columnCount(val) {
  94. this.columnChange(val);
  95. }
  96. },
  97. mounted() {
  98. this.init = false;
  99. this.columnChange();
  100. },
  101. methods: {
  102. columnChange(val) {
  103. if (this.columnCount < 2) {
  104. this.leftList = [...this.listData];
  105. } else {
  106. if (val && val == 2) {
  107. this.leftList = [...this.leftListConst]
  108. }
  109. this.initData()
  110. }
  111. },
  112. initData() {
  113. if (this.type == 1) {
  114. this.getSubGroup();
  115. } else {
  116. this.getArrayByHeight();
  117. }
  118. },
  119. getDiffList() {
  120. let diffList = [];
  121. let total = this.listData.length;
  122. if (total <= this.pageSize) {
  123. this.leftListConst = [];
  124. this.leftList = [];
  125. this.rightList = [];
  126. }
  127. let sum = this.leftListConst.length + this.rightList.length;
  128. let diff = total - sum;
  129. if (diff > 0) {
  130. diffList = [...this.listData].filter((item, index) => {
  131. return index >= sum;
  132. });
  133. }
  134. return diffList;
  135. },
  136. getSubGroup() {
  137. //type=1时执行简单数据分组
  138. if (!this.listData && this.listData.length === 0) return;
  139. let leftList = [];
  140. let rightList = [];
  141. let data = this.getDiffList();
  142. data.forEach((item, index) => {
  143. if (index % 2 === 0) {
  144. leftList.push(item);
  145. } else {
  146. rightList.push(item);
  147. }
  148. });
  149. this.leftList = this.leftList.concat(leftList);
  150. this.leftListConst = this.leftListConst.concat(leftList);
  151. this.rightList = this.rightList.concat(rightList);
  152. },
  153. async getArrayByHeight() {
  154. if (!this.listData && this.listData.length === 0) return;
  155. let data = this.getDiffList();
  156. for (let item of data) {
  157. await this.render(item);
  158. }
  159. },
  160. sleep(millisecond) {
  161. let now = new Date();
  162. let exitTime = now.getTime() + millisecond;
  163. while (true) {
  164. now = new Date();
  165. if (now.getTime() > exitTime) return;
  166. }
  167. },
  168. async render(item) {
  169. this.sleep(50)
  170. let obj = await this.getContainerHeight();
  171. return await new Promise((resolve, reject) => {
  172. if (obj && typeof obj.leftHeight === 'number') {
  173. if (obj.leftHeight <= obj.rightHeight) {
  174. this.leftList.push(item);
  175. this.leftListConst.push(item);
  176. } else {
  177. this.rightList.push(item);
  178. }
  179. resolve(true);
  180. } else {
  181. reject(false);
  182. }
  183. });
  184. },
  185. async getContainerHeight() {
  186. //type=2
  187. return await new Promise((resolve, reject) => {
  188. const query = uni.createSelectorQuery().in(this);
  189. let nodes = query.selectAll('#tui-waterfall__left, #tui-waterfall__right');
  190. nodes.boundingClientRect().exec(res => {
  191. if (res && res[0]) {
  192. const rects = res[0];
  193. const leftHeight = rects[0].height;
  194. const rightHeight = rects[1].height;
  195. resolve({
  196. leftHeight: leftHeight,
  197. rightHeight: rightHeight
  198. });
  199. } else {
  200. reject(res);
  201. }
  202. });
  203. });
  204. }
  205. }
  206. };
  207. </script>
  208. <style scoped>
  209. .tui-waterfall__box {
  210. width: 100%;
  211. display: inline-flex;
  212. justify-content: space-between;
  213. flex-direction: row;
  214. flex-wrap: wrap;
  215. box-sizing: border-box;
  216. align-items: flex-start;
  217. }
  218. .tui-waterfall__list {
  219. flex: 1;
  220. }
  221. </style>