当前位置:首页 > 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-waterfall

npm install vue-waterfall

示例代码:

<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)减少初始加载时间。

vue实现图片瀑布流

<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>

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

相关文章

H5图片实现地图

H5图片实现地图

实现H5图片地图的方法 在H5中实现图片地图功能,可以通过以下方式完成。图片地图允许用户在图片的特定区域点击,触发不同的交互效果。 使用HTML的<map>和<area>标签…

vue实现图片查看

vue实现图片查看

实现图片查看功能 在Vue中实现图片查看功能可以通过多种方式完成,常见的有使用原生HTML5的<dialog>元素、第三方组件库(如Element UI、Vant等)或专用插件(如view…

css制作图片

css制作图片

使用CSS创建图片效果 CSS可以通过多种方式实现图片的显示、处理和特效。以下是几种常见的方法: 使用background-image属性 在CSS中可以通过background-image属性将图…

vue实现图片闪烁

vue实现图片闪烁

实现图片闪烁效果的方法 在Vue中实现图片闪烁效果可以通过CSS动画或JavaScript动态控制样式。以下是几种常见方法: CSS动画实现 通过@keyframes定义闪烁动画,并应用到图片元素上…

js 实现图片 放大

js 实现图片 放大

使用 CSS transform 实现图片放大 通过 CSS 的 transform: scale() 属性可以实现图片的平滑放大效果。结合 JavaScript 监听鼠标事件控制放大状态: con…

jquery图片切换

jquery图片切换

jQuery图片切换的实现方法 通过jQuery实现图片切换功能有多种方式,以下是几种常见的实现方法: 基础淡入淡出效果 $(document).ready(function(){ let…