当前位置:首页 > 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 评分组件实现方法 使用第三方库(如 Element UI) 安装 Element UI: npm install element-ui 引入并注册组件: import Vue from '…

vue实现返回

vue实现返回

返回按钮功能实现 在Vue中实现返回功能通常有两种方式:使用浏览器历史记录的history.back()或结合Vue Router进行编程式导航。以下是具体实现方法: 使用浏览器API metho…

vue 实现注册

vue 实现注册

Vue 实现注册功能 注册功能通常包括表单验证、数据提交和用户反馈。以下是实现注册功能的步骤: 1. 创建注册表单组件 在 Vue 项目中创建一个注册表单组件,包含用户名、邮箱、密码等输入字段。…

vue网页实现

vue网页实现

Vue 网页实现指南 环境准备 安装 Node.js 和 npm/yarn,确保环境配置正确。使用 Vue CLI 或 Vite 创建项目框架,快速搭建开发环境。 npm install -g @v…

vue实现回顶部

vue实现回顶部

实现方法一:使用 window.scrollTo 在Vue组件中,可以通过调用 window.scrollTo 方法平滑滚动到页面顶部。该方法接受一个配置对象,指定滚动行为为平滑(smooth)。…

vue实现饼图

vue实现饼图

使用 ECharts 实现 Vue 饼图 安装 ECharts 依赖 npm install echarts --save 在 Vue 组件中引入 ECharts import * as echa…