当前位置:首页 > VUE

vue实现瀑布流布局

2026-02-22 01:03:48VUE

Vue 实现瀑布流布局

使用 CSS 多列布局

CSS 的 column-countcolumn-gap 属性可以快速实现瀑布流效果。适用于内容高度不一致的场景。

<template>
  <div class="waterfall-container">
    <div v-for="(item, index) in items" :key="index" class="waterfall-item">
      <!-- 内容 -->
      <img :src="item.image" :alt="item.title">
      <h3>{{ item.title }}</h3>
    </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" ref="waterfall">
    <div 
      v-for="(item, index) in items" 
      :key="index" 
      class="item" 
      :style="{ top: `${item.top}px`, left: `${item.left}px`, width: `${itemWidth}px` }"
    >
      <!-- 内容 -->
      <img :src="item.image" :alt="item.title">
      <h3>{{ item.title }}</h3>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [],
      itemWidth: 200,
      columns: 3,
      columnHeights: []
    }
  },
  mounted() {
    this.initWaterfall();
    window.addEventListener('resize', this.initWaterfall);
  },
  methods: {
    initWaterfall() {
      const containerWidth = this.$refs.waterfall.offsetWidth;
      this.itemWidth = (containerWidth - 20 * (this.columns - 1)) / this.columns;
      this.columnHeights = new Array(this.columns).fill(0);

      this.items = this.items.map(item => {
        const minHeight = Math.min(...this.columnHeights);
        const columnIndex = this.columnHeights.indexOf(minHeight);

        item.top = minHeight;
        item.left = columnIndex * (this.itemWidth + 20);

        this.columnHeights[columnIndex] += item.height + 10;
        return item;
      });
    }
  }
}
</script>

<style>
.waterfall {
  position: relative;
}
.item {
  position: absolute;
  transition: all 0.3s;
}
</style>

使用第三方库

现有的 Vue 瀑布流组件可以快速实现功能,如 vue-waterfallvue-masonry

安装 vue-waterfall:

npm install vue-waterfall --save

使用示例:

<template>
  <waterfall :col="3" :data="items">
    <template>
      <div class="cell-item" v-for="(item, index) in items" :key="index">
        <img :src="item.image" :alt="item.title">
        <h3>{{ item.title }}</h3>
      </div>
    </template>
  </waterfall>
</template>

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

export default {
  components: { Waterfall },
  data() {
    return {
      items: [] // 你的数据
    }
  }
}
</script>

响应式处理

为适应不同屏幕尺寸,可以动态调整列数:

computed: {
  columnCount() {
    const width = window.innerWidth;
    if (width < 600) return 1;
    if (width < 900) return 2;
    return 3;
  }
}

然后在布局方法中使用这个计算属性。

图片懒加载

结合 IntersectionObserver 实现图片懒加载,优化性能:

methods: {
  lazyLoadImages() {
    const observer = new IntersectionObserver((entries) => {
      entries.forEach(entry => {
        if (entry.isIntersecting) {
          const img = entry.target;
          img.src = img.dataset.src;
          observer.unobserve(img);
        }
      });
    });

    document.querySelectorAll('img[data-src]').forEach(img => {
      observer.observe(img);
    });
  }
}

在模板中使用:

vue实现瀑布流布局

<img :data-src="item.image" alt="...">

标签: 瀑布vue
分享给朋友:

相关文章

vue实现旋转

vue实现旋转

Vue 实现旋转效果的方法 在 Vue 中实现旋转效果可以通过多种方式完成,以下是几种常见的方法: 使用 CSS 动画 通过 Vue 的 :class 或 :style 绑定 CSS 动画实现旋转效…

vue实现订单

vue实现订单

Vue 实现订单功能 订单数据结构设计 订单数据通常包含以下字段: order: { id: String, // 订单ID userId: String,…

vue mvvm实现

vue mvvm实现

Vue MVVM 实现原理 Vue.js 是一个基于 MVVM(Model-View-ViewModel)模式的框架,其核心是通过数据绑定和响应式系统实现视图与数据的自动同步。以下是 Vue 实现 M…

vue实现slot

vue实现slot

Vue 中的 Slot 实现方法 Vue 的 slot 是一种内容分发机制,允许父组件向子组件插入内容。以下是几种常见的 Slot 实现方式: 默认 Slot 子组件通过 <slot>…

实现vue框架

实现vue框架

Vue框架的基本实现 Vue是一个渐进式JavaScript框架,用于构建用户界面。以下是实现Vue框架的基本步骤: 安装Vue 通过CDN引入Vue是最简单的方式: <script src…

vue mvvm 实现

vue mvvm 实现

Vue MVVM 实现原理 Vue.js 的 MVVM(Model-View-ViewModel)架构通过数据绑定和响应式系统实现视图与数据的自动同步。以下是其核心实现机制: 数据劫持与响应式系统…