图片瀑布流

效果图:
在这里插入图片描述
代码如下:

<template>
  <div class="wrap">
    <div class="column">
      <img :src="item" alt="" v-for="(item, index) in leftColumn" :key="index">
    </div>
    <div class="column">
      <img :src="item" alt="" v-for="(item, index) in rightColumn" :key="index">
    </div>
  </div>
</template>
<script>
// 图片列表
const list = [
  'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=2306490913,2115460294&fm=26&gp=0.jpg',
  'https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=2850261322,1135324421&fm=26&gp=0.jpg',
  'https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3185577376,2639757095&fm=26&gp=0.jpg',
  'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=2045072227,4071612352&fm=26&gp=0.jpg',
  'https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=4250510097,1033008477&fm=26&gp=0.jpg',
  'https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=4009862980,2222696633&fm=15&gp=0.jpg',
  'https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3612860753,3233719888&fm=26&gp=0.jpg',
  'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=2822955656,271745579&fm=26&gp=0.jpg',
  'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=2731422450,3042484163&fm=26&gp=0.jpg',
]
export default {
  data() {
    return {
      leftColumn: [],
      rightColumn: [],
    }
  },
  mounted() {
    this.getColumn();
  },
  methods: {
    // 加载图片
    imgLoad() {
      const imgArr = list.map(item => {
        return new Promise(resolve => {
          const img = new Image()
          img.src = item;
          if (img.complete) {
            resolve({ url: item, height: img.height })
          } else {
            img.onload = img.onerror = () => {
              resolve({ url: item, height: img.height })
            }
          }
        })
      })
      return imgArr;
    },
    // 遍历加载成功的图片 渲染左右两列
    getColumn() {
      const arr = this.imgLoad()
      let leftHeight = 0
      let rightHeight = 0
      Promise.all(arr).then(res => {
        res.forEach(({ height }, index) => {
          if (index === 0) {
            this.leftColumn.push(list[index])
            leftHeight += height
          } else if (index === 1) {
            this.rightColumn.push(list[index])
            rightHeight += height
          } else {
            if (leftHeight <= rightHeight) {
              this.leftColumn.push(list[index])
              leftHeight += height
            } else {
              this.rightColumn.push(list[index])
              rightHeight += height
            }
          }
        })
      })
    }
  }
}
</script>
<style>
.wrap {
  width: 100%;
  display: flex;
}
.column {
  width: 50%;
}
img {
  width: 100%;
}
</style>




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值