当前位置:首页 > VUE

vue实现卡片

2026-03-07 16:19:38VUE

Vue 实现卡片组件

使用 Vue 实现卡片组件可以通过多种方式,包括使用原生 Vue 或结合 UI 框架(如 Element UI、Vuetify 等)。以下是几种常见的方法:

原生 Vue 实现卡片

通过 Vue 的模板和样式直接实现一个基础卡片组件。

vue实现卡片

<template>
  <div class="card">
    <div class="card-header">
      <slot name="header">默认标题</slot>
    </div>
    <div class="card-body">
      <slot>默认内容</slot>
    </div>
    <div class="card-footer">
      <slot name="footer">默认底部</slot>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Card'
}
</script>

<style scoped>
.card {
  border: 1px solid #ddd;
  border-radius: 4px;
  padding: 16px;
  margin: 16px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.card-header {
  font-weight: bold;
  margin-bottom: 8px;
}
.card-footer {
  margin-top: 8px;
  color: #666;
}
</style>

使用 Element UI 实现卡片

Element UI 提供了现成的卡片组件 el-card,可以直接使用。

<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <span>卡片标题</span>
    </div>
    <div v-for="item in items" :key="item.id" class="text item">
      {{ item.content }}
    </div>
  </el-card>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { id: 1, content: '内容1' },
        { id: 2, content: '内容2' }
      ]
    }
  }
}
</script>

<style>
.box-card {
  width: 480px;
}
</style>

使用 Vuetify 实现卡片

Vuetify 也提供了丰富的卡片组件 v-card

vue实现卡片

<template>
  <v-card class="mx-auto" max-width="400">
    <v-card-title>卡片标题</v-card-title>
    <v-card-text>
      卡片内容
    </v-card-text>
    <v-card-actions>
      <v-btn color="primary">操作按钮</v-btn>
    </v-card-actions>
  </v-card>
</template>

<script>
export default {
  name: 'VuetifyCard'
}
</script>

动态卡片列表

通过 v-for 动态渲染多个卡片。

<template>
  <div>
    <card v-for="item in cardList" :key="item.id" :title="item.title">
      {{ item.content }}
    </card>
  </div>
</template>

<script>
import Card from './Card.vue'

export default {
  components: { Card },
  data() {
    return {
      cardList: [
        { id: 1, title: '标题1', content: '内容1' },
        { id: 2, title: '标题2', content: '内容2' }
      ]
    }
  }
}
</script>

卡片交互效果

为卡片添加悬停效果或点击事件。

<template>
  <div 
    class="card" 
    @click="handleClick"
    @mouseover="hover = true"
    @mouseleave="hover = false"
    :class="{ 'hover-effect': hover }"
  >
    <div class="card-body">
      {{ content }}
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      hover: false,
      content: '卡片内容'
    }
  },
  methods: {
    handleClick() {
      alert('卡片被点击了!')
    }
  }
}
</script>

<style scoped>
.card {
  transition: all 0.3s ease;
}
.hover-effect {
  transform: scale(1.05);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}
</style>

通过以上方法,可以灵活实现不同风格的卡片组件,满足项目需求。

标签: 卡片vue
分享给朋友:

相关文章

vue实现前端注册

vue实现前端注册

Vue 实现前端注册功能 注册表单设计 使用 Vue 的模板语法创建注册表单,包含用户名、邮箱、密码和确认密码字段。表单需绑定 v-model 实现双向数据绑定。 <template>…

vue实现走势图

vue实现走势图

Vue 实现走势图的方法 使用 ECharts 库 ECharts 是一个强大的数据可视化库,支持多种图表类型,包括走势图(折线图)。在 Vue 项目中可以通过 vue-echarts 封装库或直接使…

vue实现密码框的包

vue实现密码框的包

Vue 密码框实现方案 在 Vue 中实现密码框功能,可以通过原生 HTML 结合 Vue 指令或使用第三方组件库完成。以下是几种常见方案: 原生 HTML + Vue 双向绑定 <t…

vue 实现权限

vue 实现权限

Vue 实现权限控制的方法 在 Vue 项目中实现权限控制通常涉及前端路由、组件和按钮级别的权限管理。以下是几种常见的实现方式: 路由权限控制 通过路由守卫实现权限验证,过滤用户无权访问的路由:…

vue实现动画

vue实现动画

Vue 实现动画的方法 Vue 提供了多种方式来实现动画效果,包括内置的过渡系统、第三方动画库以及直接操作 CSS 或 JavaScript 动画。 使用 Vue 内置过渡系统 Vue 的 <…

vue实现selectable

vue实现selectable

Vue 实现可选择的 Select 组件 在 Vue 中实现一个可选择的 Select 组件,可以通过以下几种方式: 使用原生 HTML select 元素 <template> &…