当前位置:首页 > 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图片多选实现方法 基础实现方案 使用v-model绑定数组配合input[type="checkbox"]实现多选功能。创建图片列表数据时,每个图片对象应包含唯一标识符: data() {…

网页制作css图片切换

网页制作css图片切换

CSS图片切换的实现方法 使用CSS实现图片切换可以通过多种方式完成,以下是几种常见的方法: 纯CSS轮播图 通过CSS动画和关键帧实现自动轮播效果。定义一个容器,内部放置多个图片,利用ani…

css3手工制作图片

css3手工制作图片

使用CSS3手工制作图片 CSS3可以通过各种属性如box-shadow、border-radius、gradient等来手工绘制简单的图形或图片效果。以下是几种常见的方法: 绘制圆形 通过bor…

vue实现图片轮播

vue实现图片轮播

Vue 实现图片轮播的方法 使用第三方库(推荐) 安装 vue-awesome-swiper 库,这是基于 Swiper 的 Vue 封装: npm install swiper vue-awe…

vue实现图片打点

vue实现图片打点

实现图片打点的基本思路 图片打点功能通常指在图片上添加可交互的标记点,点击或悬停时显示相关信息。Vue实现该功能需要结合DOM操作和事件监听。 核心步骤 准备图片和容器 在Vue组件中设置一个相对…

vue实现图片

vue实现图片

Vue 实现图片展示的方法 在 Vue 中实现图片展示可以通过多种方式完成,以下是一些常见的方法: 使用 img 标签直接引入 通过 img 标签的 src 属性直接引入本地或远程图片: <…