当前位置:首页 > VUE

vue实现图片瀑布流

2026-01-22 16:06:25VUE

Vue 实现图片瀑布流的方法

使用 CSS 多列布局

CSS 的 column-countcolumn-gap 属性可以快速实现简单的瀑布流效果。这种方法适合图片高度差异不大的场景。

<template>
  <div class="waterfall-container">
    <div v-for="(img, index) in images" :key="index" class="waterfall-item">
      <img :src="img.src" :alt="img.alt">
    </div>
  </div>
</template>

<style>
.waterfall-container {
  column-count: 3;
  column-gap: 15px;
}
.waterfall-item {
  break-inside: avoid;
  margin-bottom: 15px;
}
</style>

使用 JavaScript 计算布局

通过 JavaScript 动态计算每张图片的位置,可以实现更灵活的瀑布流效果,适合图片高度差异较大的场景。

<template>
  <div class="waterfall-container" ref="container">
    <div 
      v-for="(img, index) in images" 
      :key="index" 
      class="waterfall-item" 
      :style="{ top: `${img.top}px`, left: `${img.left}px`, width: `${columnWidth}px` }"
    >
      <img :src="img.src" :alt="img.alt" @load="handleImageLoad">
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      images: [],
      columnWidth: 200,
      columnHeights: [],
    };
  },
  mounted() {
    this.initWaterfall();
    window.addEventListener('resize', this.initWaterfall);
  },
  methods: {
    initWaterfall() {
      const containerWidth = this.$refs.container.offsetWidth;
      const columnCount = Math.floor(containerWidth / this.columnWidth);
      this.columnHeights = new Array(columnCount).fill(0);
    },
    handleImageLoad(event) {
      const img = event.target;
      const minHeight = Math.min(...this.columnHeights);
      const columnIndex = this.columnHeights.indexOf(minHeight);

      this.images.push({
        src: img.src,
        alt: img.alt,
        top: minHeight,
        left: columnIndex * this.columnWidth,
      });

      this.columnHeights[columnIndex] += img.height + 15;
    },
  },
};
</script>

使用第三方库

如果需要更复杂的功能(如懒加载、无限滚动等),可以使用第三方库如 vue-waterfallmasonry-layout

vue实现图片瀑布流

安装 vue-waterfall

npm install vue-waterfall

示例代码:

vue实现图片瀑布流

<template>
  <vue-waterfall :list="images" :gutter="15" :width="240">
    <template v-slot:item="{ item }">
      <img :src="item.src" :alt="item.alt">
    </template>
  </vue-waterfall>
</template>

<script>
import VueWaterfall from 'vue-waterfall';

export default {
  components: { VueWaterfall },
  data() {
    return {
      images: [],
    };
  },
};
</script>

响应式设计

为了适应不同屏幕尺寸,可以通过监听窗口大小动态调整列数和图片宽度。

methods: {
  handleResize() {
    const containerWidth = this.$refs.container.offsetWidth;
    this.columnWidth = Math.floor(containerWidth / 3);
    this.initWaterfall();
  },
},
mounted() {
  window.addEventListener('resize', this.handleResize);
},
beforeDestroy() {
  window.removeEventListener('resize', this.handleResize);
},

性能优化

对于大量图片的场景,可以使用懒加载技术(如 vue-lazyload)减少初始加载时间。

<template>
  <div class="waterfall-container">
    <div v-for="(img, index) in images" :key="index" class="waterfall-item">
      <img v-lazy="img.src" :alt="img.alt">
    </div>
  </div>
</template>

<script>
import VueLazyload from 'vue-lazyload';

Vue.use(VueLazyload, {
  preLoad: 1.3,
  attempt: 3,
});
</script>

标签: 瀑布图片
分享给朋友:

相关文章

vue实现图片预览

vue实现图片预览

实现图片预览功能 在Vue中实现图片预览功能可以通过多种方式完成,常见的有使用原生HTML5的<dialog>元素、第三方库如viewer.js或vue-photo-preview,以及自…

react如何渲染图片

react如何渲染图片

在React中渲染图片的方法 使用<img>标签直接引入本地图片 将图片文件放入项目目录(如src/assets),通过相对路径引入。需注意Webpack可能需要配置file-loader…

react如何引用图片

react如何引用图片

在React中引用图片的方法 使用import直接引入图片 在React组件中,可以通过import语句将图片作为模块引入。这种方式适合在组件内部使用图片,Webpack会处理图片的路径和打包。 i…

js实现图片加载

js实现图片加载

使用Image对象加载图片 通过JavaScript的Image对象可以动态加载图片,适用于需要预加载或动态插入图片的场景。 const img = new Image(); img.src =…

vue实现图片列表

vue实现图片列表

实现图片列表的基本方法 在Vue中实现图片列表可以通过v-for指令遍历图片数据数组,结合<img>标签动态绑定src属性。以下是基本实现步骤: <template> &…

vue实现瀑布流

vue实现瀑布流

实现瀑布流布局的方法 在Vue中实现瀑布流布局可以通过多种方式完成,以下是几种常见的方法: 使用CSS Grid或Flexbox 通过CSS Grid或Flexbox可以实现简单的瀑布流布局。这种方…