基于vue的固定宽度图片瀑布流组件

基于vue的固定宽度图片瀑布流组件

<!-- 
 @Description: pictureList 图库列表组件
 @Author: sunyongwei
 @Date: 2022-10-12
 @Version: V1.0
 -->
<template>
  <div style="margin: 20px;">
   <div style="width: 100%;">
      <div>
        <div style="font-weight: bold;font-size: 16px;">
          <span v-for="(item,index) in fullTitle">{{item}}
             <span v-if="index<fullTitle.length-1">/</span>
          </span>
        </div>
        <div style="font-size: 10px;color: #767676;">{{list.length}}个文件,共{{(sizeTotle / 1024 / 1024).toFixed(3)}}GB</div>
      </div>
   </div>
   <div id="img-content" class="img-content">
      <img
          class="img"
          :style="'width:' + imgWidth + 'px;'"
          v-for="(item, index) in list"
          :key="'img' + index"
          :src="item.url"
      />
    </div>
  </div>
</template>

<script>
  export default {
    name: 'pictureList',
    props:{
      list:{ //图片列表
        type:Array,
        default() {
          return []
        }
      },
      fullTitle:{ //全节点名称
        type:Array,
        default() {
          return ''
        }
      },
      imgWidth: {
        type: Number,
        default: 100
      },
      imgMargin: {
        type: Number,
        default: 3
      },
      
    },
    data () {
      return {
        pictureList:[],
        screenWidth:1000,
        sizeTotle:0
      }
    },
    watch:{
      list(){
        this.sizeTotle = 0
        this.list.forEach(e=>{
          this.sizeTotle += e.size
        })
      },
    },
    mounted() {
      this.screenWidth = document.body.clientWidth;
      window.onresize = () => {
        return (() => {
          this.screenWidth = document.body.clientWidth;
          this.waterfallHandler()
        })();
      };
    },
    created() {
      let that = this
      setTimeout(function(){
        that.waterfallHandler()
      },200)
      this.list.forEach(e=>{
        this.sizeTotle += e.size
      })
    },
    methods: {
       waterfallHandler() {
          const imgWidth = this.imgWidth + this.imgMargin * 2;
          const contentW = this.screenWidth - 500;
          // 获取图片的列数
          const column = parseInt(contentW / imgWidth);
          // 高度数组
          const heightArr = new Array(column).fill(0);
          const imgList = document.getElementsByClassName("img");
          for (let i = 0; i < imgList.length; i++) {
              const item = imgList[i];
              // 当前元素的高度
              const itemHeight = item.offsetHeight;
              // 高度数组最小的高度
              const minHeight = Math.min(...heightArr);
              // 高度数组最小的高度的索引
              const minIndex = heightArr.indexOf(minHeight);
              item.style.top = minHeight+i+this.imgMargin + "px";
              item.style.left = minIndex * imgWidth + "px";
              heightArr[minIndex] += itemHeight;
          }
      }
    }
  }
</script>

<style scoped >
   .img-content {
       height: 1200px;
       position: relative;
       margin: 8px 0px;
   }
   .img {
       position: absolute;
       vertical-align: top;
       margin: 3px;
   }
  
</style>

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
实现瀑布流布局的方法有很多种,这里介绍一种使用Vue2封装瀑布流组件的方法。 首先,我们需要引入一个瀑布流插件。这里推荐使用Masonry.js(https://masonry.desandro.com/)。通过npm安装: ``` npm install masonry-layout ``` 然后在Vue组件中引入并初始化Masonry: ```javascript import Masonry from 'masonry-layout'; export default { name: 'WaterfallFlow', mounted () { this.masonry = new Masonry('.waterfall-flow', { itemSelector: '.waterfall-flow-item', columnWidth: 200, // 每列的宽度 gutter: 10, // 列与列之间的间距 fitWidth: true // 是否自适应容器宽度 }); }, methods: { reload () { this.masonry.reloadItems(); this.masonry.layout(); } } } ``` 接下来,我们需要在模板中渲染出瀑布流的布局。这里我们可以使用`v-for`指令来循环渲染出每个瀑布流项,并使用`ref`指令来获取瀑布流容器的DOM对象: ```html <template> <div class="waterfall-flow" ref="waterfallFlow"> <div class="waterfall-flow-item" v-for="(item, index) in items" :key="index"> <!-- 瀑布流项的内容 --> </div> </div> </template> ``` 最后,我们需要在组件中监听窗口大小的变化,并在变化时重新布局瀑布流。这里我们可以使用`window.resize`事件来监听窗口大小的变化,并在事件回调中调用`reload`方法重新布局瀑布流: ```javascript export default { name: 'WaterfallFlow', mounted () { // 初始化瀑布流 this.masonry = new Masonry('.waterfall-flow', { itemSelector: '.waterfall-flow-item', columnWidth: 200, gutter: 10, fitWidth: true }); // 监听窗口大小变化 window.addEventListener('resize', this.reload); }, beforeDestroy () { // 销毁瀑布流 this.masonry.destroy(); // 移除窗口大小变化监听 window.removeEventListener('resize', this.reload); }, methods: { reload () { // 重新布局瀑布流 this.masonry.reloadItems(); this.masonry.layout(); } } } ``` 这样就完成了一个简单的Vue2瀑布流组件的封装。完整代码如下: ```html <template> <div class="waterfall-flow" ref="waterfallFlow"> <div class="waterfall-flow-item" v-for="(item, index) in items" :key="index"> <!-- 瀑布流项的内容 --> </div> </div> </template> <script> import Masonry from 'masonry-layout'; export default { name: 'WaterfallFlow', props: { items: { type: Array, default: () => [] } }, mounted () { // 初始化瀑布流 this.masonry = new Masonry('.waterfall-flow', { itemSelector: '.waterfall-flow-item', columnWidth: 200, gutter: 10, fitWidth: true }); // 监听窗口大小变化 window.addEventListener('resize', this.reload); }, beforeDestroy () { // 销毁瀑布流 this.masonry.destroy(); // 移除窗口大小变化监听 window.removeEventListener('resize', this.reload); }, methods: { reload () { // 重新布局瀑布流 this.masonry.reloadItems(); this.masonry.layout(); } } } </script> <style scoped> .waterfall-flow { width: 100%; } .waterfall-flow-item { width: 100%; margin-bottom: 10px; } </style> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

巧克力味。

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值