当前位置:首页 > VUE

vue实现grid动态布局

2026-02-23 12:01:33VUE

Vue 实现动态 Grid 布局

使用 CSS Grid 和动态数据绑定

通过 Vue 的 v-for 指令结合 CSS Grid 实现动态布局。在模板中定义 grid 容器,动态渲染子元素。

<template>
  <div class="grid-container">
    <div 
      v-for="(item, index) in gridItems" 
      :key="index" 
      class="grid-item"
      :style="{ 'grid-area': item.area }"
    >
      {{ item.content }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      gridItems: [
        { area: 'header', content: 'Header' },
        { area: 'sidebar', content: 'Sidebar' },
        { area: 'main', content: 'Main Content' },
        { area: 'footer', content: 'Footer' }
      ]
    };
  }
};
</script>

<style>
.grid-container {
  display: grid;
  grid-template-areas:
    "header header header"
    "sidebar main main"
    "footer footer footer";
  gap: 10px;
  padding: 10px;
}

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

响应式动态调整布局

通过监听窗口大小或使用 Vue 的计算属性动态修改 grid-template-columnsgrid-template-rows

vue实现grid动态布局

<template>
  <div 
    class="responsive-grid" 
    :style="{ 'grid-template-columns': gridColumns }"
  >
    <div v-for="n in itemCount" :key="n" class="grid-item">
      Item {{ n }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      itemCount: 6,
      screenWidth: window.innerWidth
    };
  },
  mounted() {
    window.addEventListener('resize', this.handleResize);
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.handleResize);
  },
  methods: {
    handleResize() {
      this.screenWidth = window.innerWidth;
    }
  },
  computed: {
    gridColumns() {
      return this.screenWidth > 768 ? 'repeat(3, 1fr)' : 'repeat(2, 1fr)';
    }
  }
};
</script>

<style>
.responsive-grid {
  display: grid;
  gap: 10px;
  padding: 10px;
}

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

使用第三方库实现复杂交互

对于需要拖拽排序、动态调整大小等高级功能,可以使用 vue-grid-layout 这类专门库。

安装依赖:

vue实现grid动态布局

npm install vue-grid-layout

示例实现:

<template>
  <grid-layout
    :layout.sync="layout"
    :col-num="12"
    :row-height="30"
    :is-draggable="true"
    :is-resizable="true"
  >
    <grid-item
      v-for="item in layout"
      :key="item.i"
      :x="item.x"
      :y="item.y"
      :w="item.w"
      :h="item.h"
      :i="item.i"
    >
      {{ item.text }}
    </grid-item>
  </grid-layout>
</template>

<script>
import { GridLayout, GridItem } from 'vue-grid-layout';

export default {
  components: {
    GridLayout,
    GridItem
  },
  data() {
    return {
      layout: [
        { x: 0, y: 0, w: 2, h: 2, i: '0', text: 'Item 1' },
        { x: 2, y: 0, w: 2, h: 4, i: '1', text: 'Item 2' },
        { x: 4, y: 0, w: 2, h: 5, i: '2', text: 'Item 3' }
      ]
    };
  }
};
</script>

动态添加/删除网格项

结合 Vue 的响应式数据实现网格项的增删功能。

<template>
  <div>
    <button @click="addItem">Add Item</button>
    <button @click="removeItem">Remove Item</button>
    <div class="dynamic-grid">
      <div v-for="(item, index) in items" :key="index" class="grid-cell">
        {{ item }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: ['Item 1', 'Item 2', 'Item 3'],
      counter: 3
    };
  },
  methods: {
    addItem() {
      this.counter++;
      this.items.push(`Item ${this.counter}`);
    },
    removeItem() {
      if (this.items.length > 0) {
        this.items.pop();
        this.counter--;
      }
    }
  }
};
</script>

<style>
.dynamic-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
  gap: 10px;
  margin-top: 20px;
}

.grid-cell {
  background: #f0f0f0;
  padding: 15px;
  text-align: center;
}
</style>

这些方法覆盖了从基础到高级的 Vue 动态网格布局实现,可根据具体需求选择适合的方案。CSS Grid 提供原生支持,而第三方库能快速实现复杂交互功能。

标签: 布局动态
分享给朋友:

相关文章

vue实现动态

vue实现动态

Vue实现动态内容的几种方法 Vue提供了多种方式实现动态内容渲染,包括动态组件、动态样式、动态属性等。以下是常见的实现方法: 动态组件 使用<component :is="currentC…

vue实现上下布局

vue实现上下布局

实现上下布局的基本结构 在Vue中实现上下布局通常需要使用CSS的flexbox或grid布局方式。以下是一个基础的上下布局实现示例: <template> <div clas…

vue表格动态实现

vue表格动态实现

Vue 表格动态实现方法 动态表格的实现通常涉及数据绑定、列配置动态化以及交互功能的灵活处理。以下是几种常见场景的具体实现方式: 动态数据绑定 通过v-for指令循环渲染表格行数据,数据变化时表…

vue实现数据动态

vue实现数据动态

Vue 实现数据动态绑定的方法 Vue.js 的核心特性之一是数据绑定,通过响应式系统实现数据的动态更新。以下是几种常见的实现方式: 双向数据绑定(v-model) 使用 v-model 指令可以实…

vue页面布局实现

vue页面布局实现

Vue 页面布局实现方法 基础布局结构 使用 Vue 单文件组件(SFC)构建页面框架,结合 <template>、<script> 和 <style> 三个部分。…

vue实现动态按钮

vue实现动态按钮

实现动态按钮的基本思路 在Vue中实现动态按钮通常涉及根据数据状态动态改变按钮的样式、文本或行为。可以通过绑定动态类名、样式或事件来实现。 动态绑定按钮样式 使用v-bind:class或简写:cl…