当前位置:首页 > VUE

vue实现groupbox

2026-03-27 16:41:04VUE

Vue 实现 GroupBox 组件

在 Vue 中实现类似 WinForms/WPF 的 GroupBox 效果,可以通过自定义组件实现分组内容的视觉隔离和标题展示。以下是两种常见实现方式:

基于插槽和 CSS 的实现

创建一个可复用的 GroupBox 组件,利用插槽传递内容:

<!-- GroupBox.vue -->
<template>
  <div class="group-box">
    <fieldset>
      <legend v-if="title" class="group-box-title">{{ title }}</legend>
      <slot></slot>
    </fieldset>
  </div>
</template>

<script>
export default {
  props: {
    title: String
  }
}
</script>

<style scoped>
.group-box {
  margin: 1em 0;
}
fieldset {
  border: 1px solid #ddd;
  border-radius: 4px;
  padding: 0.5em 1em 1em;
}
.group-box-title {
  padding: 0 0.5em;
  color: #666;
  font-size: 0.9em;
}
</style>

使用方式:

<GroupBox title="用户信息">
  <input type="text" placeholder="用户名">
  <input type="password" placeholder="密码">
</GroupBox>

基于 Card 组件的变体

若使用 UI 框架(如 Element UI/Vuetify),可基于现有组件扩展:

<template>
  <el-card class="group-box" :header="title">
    <slot></slot>
  </el-card>
</template>

<script>
import { ElCard } from 'element-plus'
export default {
  components: { ElCard },
  props: {
    title: String
  }
}
</script>

<style>
.group-box {
  margin-bottom: 20px;
}
</style>

动态折叠功能扩展

增加折叠/展开功能(需 Vue 3 的 v-model 支持):

<template>
  <div class="group-box">
    <div class="header" @click="toggle">
      {{ title }}
      <span class="toggle-icon">{{ isOpen ? '−' : '+' }}</span>
    </div>
    <div v-show="isOpen" class="content">
      <slot></slot>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    title: String,
    modelValue: { type: Boolean, default: true }
  },
  emits: ['update:modelValue'],
  computed: {
    isOpen: {
      get() { return this.modelValue },
      set(val) { this.$emit('update:modelValue', val) }
    }
  },
  methods: {
    toggle() { this.isOpen = !this.isOpen }
  }
}
</script>

<style scoped>
.header {
  cursor: pointer;
  padding: 8px;
  background: #f5f5f5;
  border: 1px solid #ddd;
}
.content {
  padding: 12px;
  border: 1px solid #ddd;
  border-top: none;
}
.toggle-icon {
  float: right;
}
</style>

使用方式:

vue实现groupbox

<GroupBox v-model="isOpen" title="高级选项">
  <!-- 内容 -->
</GroupBox>

注意事项

  1. 边框样式可通过 CSS 的 border 属性自定义
  2. 折叠功能需处理动画效果时可添加 <transition> 包裹
  3. 在移动端建议增加点击区域的 padding 以提高可用性
  4. 若需要深色模式支持,可通过 CSS 变量动态切换边框颜色

标签: vuegroupbox
分享给朋友:

相关文章

vue实现发表

vue实现发表

Vue 实现发表功能 在 Vue 中实现发表功能通常涉及表单处理、数据绑定和网络请求。以下是实现步骤和代码示例: 表单设计与数据绑定 创建一个表单用于输入发表内容,使用 v-model 进行数据双向…

vue实现iframe

vue实现iframe

Vue 中实现 iframe 的方法 在 Vue 项目中,可以通过多种方式实现 iframe 的嵌入。以下是几种常见的方法: 使用原生 HTML iframe 标签 直接在 Vue 组件的模板中使用…

vue 实现拖拽

vue 实现拖拽

Vue 实现拖拽功能 使用 HTML5 原生拖拽 API HTML5 提供了原生的拖拽 API,可以通过 draggable 属性和相关事件实现拖拽功能。 <template> &l…

vue 实现轮播

vue 实现轮播

Vue 实现轮播的方法 使用第三方库(如 Swiper) 安装 Swiper 库: npm install swiper 在 Vue 组件中引入 Swiper: <template>…

vue 实现上传

vue 实现上传

Vue 实现文件上传 使用 Vue 实现文件上传可以通过原生 HTML 的 <input type="file"> 或第三方库(如 axios、vue-upload-component)完…

react实现vue

react实现vue

React 实现 Vue 功能 React 和 Vue 是两种不同的前端框架,但可以通过一些方法在 React 中实现 Vue 的特性。以下是几种常见 Vue 功能在 React 中的实现方式: 双…