Vue瀑布流

Vue瀑布流

data() {
    return {
      colCount: 0,
      colHeightArry: [],
      boxWidth: 0,
      mainWidth: 0
    }
  },
  watch: {
    mainWidth: {
      handler(val) {
        this.colHeightArry = []
        this.boxWidth = this.$refs.box[0].clientWidth
        if (val === 1180) {
          this.colCount = 3
        } else {
          this.colCount = 4
        }
        this.reset()
      }
    }
  },
  
  mounted() {
    window.onresize = () => {
      this.$nextTick(() => {
        this.mainWidth = this.$refs.main.clientWidth
      })
    }
   // 根据页面宽度不同显示不同的个数,如果在watch中加载读取不到this.$refs
    this.mainWidth = this.$refs.main.clientWidth
    this.boxWidth = this.$refs.box[0].clientWidth
    if (this.mainWidth === 1180) {
      this.colCount = 3
    } else {
      this.colCount = 4
    }
    this.reset()
  },
  methods: {
    reset() {
      const { colHeightArry, colCount, boxWidth } = this
      this.$refs.box.forEach((item, index) => {
        if (index < colCount) {
          item.style.top = 0
          item.style.left = `${boxWidth * index + index * 18}px`
          colHeightArry.push(item.offsetHeight)
        } else {
          let minHeight = colHeightArry[0]
          let minIndex = 0
          colHeightArry.forEach((p, pindex) => {
            if (minHeight > p) {
              minHeight = p
              minIndex = pindex
            }
          })
          item.style.top = `${colHeightArry[minIndex] + 20}px`
          item.style.left = `${boxWidth * minIndex + minIndex * 18}px`
          colHeightArry[minIndex] =
            colHeightArry[minIndex] + item.offsetHeight + 20
        }
      })
    }
  }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值