waterfall.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <template>
  2. <view class="water-flow-box" >
  3. <view class="water-flow-column" :style="{ 'margin-right': columnGap + 'px' }" v-for="(col, c) in colunmList" :key="c">
  4. <view class="item" :id="col.id" style="width: 100%;">
  5. <view
  6. v-for="(item, index) in col.list"
  7. :key="index"
  8. class="item_content"
  9. :style="{ 'margin-bottom': columnGap + 'px', background: item.background }"
  10. @click="goTodetails(item)"
  11. >
  12. <image :src="item.cover" style="width: 100%;"></image>
  13. <view class="waterfall">
  14. <view class="waterfallitem">
  15. {{item.title}}
  16. </view>
  17. <view class="image">
  18. <view class="nickImg">
  19. <image :src="item.head_photo" class="tuxiang" mode=""></image>
  20. <view class="contentname">
  21. {{item.nickname?item.nickname:item.mobile}}
  22. </view>
  23. </view>
  24. <view class="fabulous">
  25. <image class="good" src="../../static/delImg/good.png" mode=""></image>
  26. <view class="contentnumber">
  27. {{item.like_num}}
  28. </view>
  29. </view>
  30. </view>
  31. </view>
  32. </view>
  33. </view>
  34. </view>
  35. </view>
  36. </template>
  37. <script>
  38. export default {
  39. name: 'grass-water-flow',
  40. props: {
  41. fieldKey: {
  42. //关键比对key
  43. type: String,
  44. default: 'id'
  45. },
  46. idPrefix: {
  47. //前缀
  48. type: String,
  49. default: 'water-flow-'
  50. },
  51. colunmNumber: {
  52. //瀑布流列数
  53. type: Number,
  54. default: 2
  55. },
  56. columnGap: {
  57. //列间隔
  58. type: Number,
  59. default: 10
  60. },
  61. flowList: {
  62. // 瀑布流数据
  63. type: Array,
  64. required: true,
  65. default: function() {
  66. return [];
  67. }
  68. }
  69. },
  70. data() {
  71. return {
  72. colunmList: [], //列
  73. internalDataList: [], //内部操作数据
  74. refrenshColunmDataList: [] //记录加载的数据
  75. };
  76. },
  77. watch: {
  78. colunmNumber: function(v) {
  79. this.internalDataList = Object.assign([], this.refrenshColunmDataList);
  80. this.calculateColumn(v, false);
  81. },
  82. flowList: function(v) {
  83. this.internalDataList = Object.assign(this.internalDataList, v);
  84. if (this.internalDataList.length > 0) {
  85. this.getPushContainer();
  86. }
  87. },
  88. colunmList: {
  89. handler() {
  90. this.$nextTick(function() {
  91. this.getPushContainer();
  92. });
  93. },
  94. deep: true
  95. }
  96. },
  97. created() {
  98. this.internalDataList = Object.assign([], this.flowList);
  99. this.calculateColumn(this.colunmNumber, true);
  100. },
  101. mounted() {
  102. if (this.internalDataList.length > 0) {
  103. this.colunmList[0].list.push(this.internalDataList[0]);
  104. let shiftObj = this.internalDataList.shift();
  105. this.pushLoadData(shiftObj);
  106. }
  107. },
  108. methods: {
  109. /**
  110. * 计算列
  111. * @param {Object} size 列数
  112. * @param {Object} isCreate 是否初始化创建(created生命周期)
  113. */
  114. calculateColumn: function(size, isCreate) {
  115. this.colunmList = [];
  116. for (let i = 1; i <= size; i++) {
  117. let obj = {};
  118. obj.id = this.idPrefix + i;
  119. obj.list = [];
  120. this.colunmList.push(obj);
  121. }
  122. if (!isCreate) {
  123. if (this.internalDataList.length > 0) {
  124. this.colunmList[0].list.push(this.internalDataList[0]);
  125. let shiftObj = this.internalDataList.shift();
  126. this.pushLoadData(shiftObj);
  127. }
  128. }
  129. },
  130. /**
  131. * 获取节点信息
  132. */
  133. getPushContainer() {
  134. let sortList = [];
  135. const query = uni.createSelectorQuery().in(this);
  136. query
  137. .selectAll('.item')
  138. .boundingClientRect()
  139. .exec(res => {
  140. if (res) {
  141. sortList = res[0];
  142. sortList.sort(function(a, b) {
  143. return a.height - b.height;
  144. });
  145. this.pushShiftData(sortList[0]);
  146. }
  147. });
  148. },
  149. //商品跳详情
  150. goTodetails(item){
  151. uni.navigateTo({
  152. url:"./details?id="+item.id
  153. })
  154. },
  155. /**
  156. * 处理数据
  157. * @param {Object} pushObj
  158. */
  159. pushShiftData(pushObj) {
  160. if (this.internalDataList.length > 0) {
  161. for (let i = 0; i < this.colunmList.length; i++) {
  162. if (this.colunmList[i].id == pushObj.id) {
  163. this.colunmList[i].list.push(this.internalDataList[0]);
  164. let shiftObj = this.internalDataList.shift();
  165. this.pushLoadData(shiftObj);
  166. }
  167. }
  168. }
  169. },
  170. /**
  171. * 记录加载的数据
  172. * @param {Object} obj
  173. */
  174. pushLoadData(obj) {
  175. if (this.refrenshColunmDataList.length > 0) {
  176. let result = this.refrenshColunmDataList.some(item => {
  177. if (item[this.fieldKey] == obj[this.fieldKey]) {
  178. return true;
  179. }
  180. });
  181. if (!result) {
  182. this.refrenshColunmDataList.push(obj);
  183. }
  184. } else {
  185. this.refrenshColunmDataList.push(obj);
  186. }
  187. },
  188. /**
  189. * 外部刷新数据时,调用此方法,清理掉原有加载数据
  190. */
  191. externalRefrensh() {
  192. this.refrenshColunmDataList = [];
  193. for (let i = 0; i < this.colunmList.length; i++) {
  194. this.colunmList[i].list = [];
  195. }
  196. }
  197. }
  198. };
  199. </script>
  200. <style scoped>
  201. uni-view.water-flow-column{
  202. background-color: transparent;
  203. margin: transparent;
  204. }
  205. .item_content{
  206. border-radius: 18rpx;
  207. margin-bottom: 42rpx;
  208. overflow: hidden;
  209. background-color: #fff;
  210. }
  211. .black{
  212. height: 30rpx;
  213. background-color: #f5f5f5;
  214. }
  215. .contentnumber{
  216. border-radius: 16rpx;
  217. font-size: 24rpx;
  218. font-family: PingFang SC, PingFang SC-Regular;
  219. font-weight: 400;
  220. color: #999999;
  221. margin-left: 10rpx;
  222. }
  223. .good{
  224. width: 29rpx;
  225. height: 31rpx;
  226. margin-left: 62rpx;
  227. }
  228. .contentname{
  229. width: 90rpx;
  230. flex: 1;
  231. color: #1a1a1a;
  232. font-size: 24rpx;
  233. font-family: PingFang SC, PingFang SC-Regular;
  234. font-weight: 400;
  235. white-space: nowrap;
  236. text-overflow: ellipsis;
  237. overflow: hidden;
  238. margin-left: 13rpx;
  239. }
  240. .tuxiang{
  241. width: 50rpx;
  242. height: 50rpx;
  243. }
  244. .image{
  245. display: flex;
  246. padding: 9rpx 18rpx 22rpx 22rpx ;
  247. justify-content: space-between;
  248. align-items: center;
  249. }
  250. .image .nickImg,.image .fabulous{
  251. display: flex;
  252. justify-content: space-between;
  253. align-items: center;
  254. }
  255. .waterfallitem{
  256. padding: 15rpx 20rpx;
  257. font-size: 24rpx;
  258. line-height: 40rpx;
  259. color: #333333;
  260. overflow: hidden;
  261. text-overflow: ellipsis;
  262. display: -webkit-box;
  263. -webkit-line-clamp: 2;
  264. -webkit-box-orient: vertical;
  265. word-wrap: break-word;
  266. word-break: break-all;
  267. white-space: normal !important;
  268. }
  269. /* 瀑布流最外层 */
  270. .water-flow-box {
  271. /* margin: 10px; */
  272. display: flex;
  273. flex-direction: row;
  274. height: auto;
  275. border-radius: 18rpx;
  276. padding: 50rpx 20rpx 42rpx 29rpx;
  277. }
  278. .water-flow-column {
  279. /* margin-right: 10px; */
  280. display: flex;
  281. flex-flow: column wrap;
  282. width: 100%;
  283. border-radius: 18rpx;
  284. background-color: white;
  285. /* margin-bottom: 42rpx; */
  286. }
  287. .water-flow-box .water-flow-column:last-child {
  288. margin-right: 0px !important;
  289. }
  290. .water-flow-column > .item > .item_content {
  291. /* margin-bottom: 10px; */
  292. color: #ffffff;
  293. /* text-align: center; */
  294. width: 100%;
  295. }
  296. </style>