当前位置:首页 > 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)减少初始加载时间。

<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实现图片

Vue 实现图片功能的方法 静态图片引入 在 Vue 项目中,可以通过 src 属性直接引入静态图片。图片通常放在 public 或 assets 目录下。 <template>…

vue 实现图片

vue 实现图片

Vue 实现图片加载与展示的方法 在 Vue 中实现图片加载和展示可以通过多种方式完成,以下是一些常见的方法: 使用 v-bind 绑定图片路径 通过 v-bind(或简写为 :)动态绑定图片的…

vue实现图片单选

vue实现图片单选

实现图片单选功能 在Vue中实现图片单选功能可以通过v-model绑定选中状态,结合CSS样式控制选中效果。以下是具体实现方式: 基础实现方案 <template> <div…

css网页制作漂浮图片

css网页制作漂浮图片

使用CSS实现漂浮图片效果 通过CSS的animation属性和@keyframes规则可以轻松实现图片漂浮效果。以下是几种常见实现方式: 基础漂浮动画 通过关键帧定义图片的垂直移动,模拟漂浮感:…

js实现图片轮播

js实现图片轮播

实现基础图片轮播 使用HTML、CSS和JavaScript创建一个简单的图片轮播。HTML部分定义轮播容器和图片,CSS设置样式和动画效果,JavaScript处理轮播逻辑。 <div cl…

vue实现图片

vue实现图片

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