waterfall.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <template>
  2. <view :style="mixinVariableStr" class="waterfall h-flex-x h-flex-2 h-flex-top">
  3. <view style="padding-right: 16rpx;">
  4. <view class="item" v-for="(item,index) in leftList" :key="index">
  5. <view class="img" :style="{
  6. 'height':item.height
  7. }"></view>
  8. <view class="name"></view>
  9. <view class="desc"></view>
  10. </view>
  11. </view>
  12. <view style="padding-left: 16rpx;">
  13. <view class="item" v-for="(item,index) in rightList" :key="index">
  14. <view class="img" :style="{
  15. 'height':item.height
  16. }"></view>
  17. <view class="name"></view>
  18. <view class="desc"></view>
  19. </view>
  20. </view>
  21. </view>
  22. </template>
  23. <script>
  24. import cssVariable from "../mixin/css-variable.js";
  25. export default {
  26. mixins:[cssVariable],
  27. props:{
  28. imgHeight:{
  29. type: String,
  30. default:"300rpx"
  31. },
  32. waterfall:{
  33. type: Boolean,
  34. default:true
  35. },
  36. length:{
  37. type: Number | String,
  38. default:"10"
  39. },
  40. },
  41. computed:{
  42. },
  43. data() {
  44. return {
  45. leftList:[],
  46. rightList:[],
  47. };
  48. },
  49. created() {
  50. this.createList();
  51. },
  52. methods:{
  53. createList(){
  54. let length = Number(this.$props.length) || 10;
  55. let height = parseInt(this.$props.imgHeight,10);
  56. // 校验是否为 rpx 单位
  57. let isRpx = /rpx/.test(this.$props.imgHeight);
  58. let [leftArr,rightArr] = [[],[]];
  59. let [leftHeight,rightHeight] = [0,0];
  60. for(let i=0;i<length;i++){
  61. let nowHeight = height;
  62. // 开启了瀑布流,则随机生成高度差
  63. if(this.$props.waterfall){
  64. // 运算符,用于决定 增加 或 减小 图片的高度
  65. let operator = Math.random();
  66. // 生成一个 0 ~ 30 的随机整数,rpx 单位则双倍
  67. let tempHeight = Math.floor(Math.random()*(isRpx ? 100 : 50));
  68. if(operator >= 0.5){
  69. nowHeight+=tempHeight;
  70. }else{
  71. nowHeight-=tempHeight;
  72. }
  73. }
  74. // nowHeight = isRpx ? `${nowHeight}rpx` : `${nowHeight}px`;
  75. if(rightHeight < leftHeight){
  76. rightArr.push({
  77. height:isRpx ? `${nowHeight}rpx` : `${nowHeight}px`
  78. });
  79. rightHeight+=nowHeight;
  80. }else{
  81. leftArr.push({
  82. height:isRpx ? `${nowHeight}rpx` : `${nowHeight}px`
  83. });
  84. leftHeight+=nowHeight;
  85. }
  86. }
  87. this.leftList = leftArr;
  88. this.rightList = rightArr;
  89. }
  90. }
  91. }
  92. </script>
  93. <style lang="scss" scoped>
  94. @import "../libs/global.scss";
  95. .waterfall{
  96. background-color: var(--background);
  97. .item{
  98. position: relative;
  99. & + .item{
  100. margin-top: 32rpx;
  101. }
  102. .img{
  103. background-color: var(--general);
  104. border-radius: 16rpx;
  105. }
  106. .name{
  107. background-color: var(--general);
  108. margin-top: 12rpx;
  109. width: 60%;
  110. height: 24rpx;
  111. border-radius: 0;
  112. margin-left: 12rpx;
  113. }
  114. .desc{
  115. background-color: var(--general);
  116. margin-top: 8rpx;
  117. width: 40%;
  118. height: 18rpx;
  119. border-radius: 0;
  120. margin-left: 12rpx;
  121. }
  122. }
  123. }
  124. </style>