当前位置:首页 > VUE

vue实现grid

2026-01-07 18:14:07VUE

Vue 实现 Grid 布局的方法

使用 CSS Grid 布局

Vue 可以结合 CSS Grid 布局实现灵活的网格系统。CSS Grid 是现代浏览器原生支持的布局方案,无需额外依赖库。

<template>
  <div class="grid-container">
    <div v-for="(item, index) in items" :key="index" class="grid-item">
      {{ item }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [1, 2, 3, 4, 5, 6, 7, 8, 9]
    }
  }
}
</script>

<style>
.grid-container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px;
}

.grid-item {
  background: #eee;
  padding: 20px;
  text-align: center;
}
</style>

使用第三方组件库

多个流行的 Vue UI 组件库提供了现成的 Grid 组件,简化开发流程。

Element UI:

vue实现grid

<el-row :gutter="20">
  <el-col :span="6" v-for="(item, index) in items" :key="index">
    <div class="grid-item">{{ item }}</div>
  </el-col>
</el-row>

Vuetify:

<v-container>
  <v-row>
    <v-col cols="12" sm="6" md="4" v-for="(item, index) in items" :key="index">
      <v-card>{{ item }}</v-card>
    </v-col>
  </v-row>
</v-container>

自定义响应式 Grid 组件

可以创建自定义的响应式 Grid 组件,根据屏幕尺寸动态调整列数。

vue实现grid

<template>
  <div class="responsive-grid" :style="{ '--columns': columns }">
    <slot></slot>
  </div>
</template>

<script>
export default {
  props: {
    columns: {
      type: Number,
      default: 3
    }
  }
}
</script>

<style>
.responsive-grid {
  display: grid;
  grid-template-columns: repeat(var(--columns), 1fr);
  gap: 16px;
}

@media (max-width: 768px) {
  .responsive-grid {
    --columns: 2;
  }
}

@media (max-width: 480px) {
  .responsive-grid {
    --columns: 1;
  }
}
</style>

使用 CSS 变量动态控制

通过 CSS 变量实现更灵活的 Grid 控制,可在运行时动态调整布局参数。

<template>
  <div class="dynamic-grid" :style="gridStyle">
    <div v-for="(item, index) in items" :key="index">
      {{ item }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [1, 2, 3, 4, 5, 6],
      columns: 3,
      gap: '16px'
    }
  },
  computed: {
    gridStyle() {
      return {
        '--columns': this.columns,
        '--gap': this.gap
      }
    }
  }
}
</script>

<style>
.dynamic-grid {
  display: grid;
  grid-template-columns: repeat(var(--columns), 1fr);
  gap: var(--gap);
}
</style>

瀑布流 Grid 实现

对于图片墙等需要瀑布流布局的场景,可以使用 Masonry 风格的 Grid。

<template>
  <div class="masonry-grid">
    <div 
      v-for="(item, index) in items" 
      :key="index" 
      class="masonry-item"
      :style="{ height: item.height }"
    >
      {{ item.content }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { height: '200px', content: 'Item 1' },
        { height: '250px', content: 'Item 2' },
        { height: '180px', content: 'Item 3' }
      ]
    }
  }
}
</script>

<style>
.masonry-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  grid-auto-rows: 10px;
  gap: 16px;
}

.masonry-item {
  grid-row-end: span calc(var(--span) / 10);
  background: #eee;
}
</style>

标签: vuegrid
分享给朋友:

相关文章

vue如何实现到期提醒

vue如何实现到期提醒

实现 Vue 到期提醒功能 使用计算属性计算剩余时间 在 Vue 组件中定义一个计算属性,计算目标日期与当前日期的差值。通过 Date 对象获取时间戳并计算剩余天数或小时数。 computed: {…

vue 实现闪烁

vue 实现闪烁

Vue 实现闪烁效果的方法 方法一:使用 CSS 动画 在 Vue 组件中定义一个闪烁的 CSS 动画,通过绑定 class 或 style 来控制元素的闪烁效果。 <template>…

vue实现接口

vue实现接口

在 Vue 中实现接口请求 Vue 本身不直接提供 HTTP 请求功能,通常需要借助第三方库如 axios 或 fetch 来实现接口请求。以下是几种常见方法: 使用 axios 发送请求 安装 a…

vue实现通讯

vue实现通讯

Vue 组件通讯方法 父子组件通讯 父组件向子组件传递数据通过 props,子组件向父组件传递数据通过 $emit 事件。 父组件模板: <child-component :message=…

vue实现阻塞

vue实现阻塞

Vue 实现阻塞的方法 在 Vue 中实现阻塞操作通常涉及异步控制、状态管理或生命周期钩子的使用。以下是几种常见方法: 使用 async/await 处理异步阻塞 通过 async/await 可…

vue实现接口连接

vue实现接口连接

Vue 实现接口连接的常用方法 Vue 中实现接口连接通常需要借助 HTTP 客户端库,以下是几种常见实现方式: 使用 Axios Axios 是流行的 HTTP 客户端库,支持 Promise A…