waterfall.vue 7.4 KB

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