当前位置:首页 > VUE

vue实现卡片

2026-03-28 02:24:06VUE

Vue 实现卡片组件

在 Vue 中实现卡片组件可以通过自定义组件或使用现成的 UI 库(如 Element UI、Vuetify 等)。以下是两种常见方法:

自定义卡片组件

创建一个简单的卡片组件,包含标题、内容和操作按钮。

<template>
  <div class="card">
    <div class="card-header">
      <h3>{{ title }}</h3>
    </div>
    <div class="card-body">
      <p>{{ content }}</p>
    </div>
    <div class="card-footer">
      <button @click="handleClick">点击</button>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    title: {
      type: String,
      default: '默认标题'
    },
    content: {
      type: String,
      default: '默认内容'
    }
  },
  methods: {
    handleClick() {
      this.$emit('card-click');
    }
  }
};
</script>

<style scoped>
.card {
  border: 1px solid #ddd;
  border-radius: 4px;
  padding: 16px;
  margin: 10px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.card-header h3 {
  margin: 0;
}
.card-body {
  margin: 10px 0;
}
.card-footer button {
  padding: 5px 10px;
  background: #42b983;
  color: white;
  border: none;
  border-radius: 3px;
}
</style>

使用 UI 库(以 Element UI 为例)

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

<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <span>卡片名称</span>
      <el-button style="float: right; padding: 3px 0" type="text">操作按钮</el-button>
    </div>
    <div v-for="o in 4" :key="o" class="text item">
      {{ '列表内容 ' + o }}
    </div>
  </el-card>
</template>

<script>
export default {
  name: 'CardDemo'
};
</script>

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

动态渲染卡片列表

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

<template>
  <div>
    <Card 
      v-for="(item, index) in cards" 
      :key="index"
      :title="item.title"
      :content="item.content"
      @card-click="handleCardClick(index)"
    />
  </div>
</template>

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

export default {
  components: { Card },
  data() {
    return {
      cards: [
        { title: '卡片1', content: '内容1' },
        { title: '卡片2', content: '内容2' }
      ]
    };
  },
  methods: {
    handleCardClick(index) {
      console.log(`点击了卡片 ${index}`);
    }
  }
};
</script>

卡片动画效果

通过 Vue 的过渡(Transition)实现卡片动画。

vue实现卡片

<template>
  <transition name="fade">
    <div class="card" v-if="show">
      <h3>动画卡片</h3>
      <p>内容</p>
    </div>
  </transition>
</template>

<style scoped>
.fade-enter-active, .fade-leave-active {
  transition: opacity 0.5s;
}
.fade-enter, .fade-leave-to {
  opacity: 0;
}
</style>

通过以上方法,可以灵活实现 Vue 中的卡片组件,并根据需求扩展功能。

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

相关文章

vue实现app

vue实现app

Vue 实现 App 的常见方法 使用 Vue 开发移动端 App 主要有两种主流方案:混合开发(Hybrid)和原生渲染。以下是具体实现方式和工具链: 混合开发方案(WebView 嵌入) 通过…

vue 路由 实现

vue 路由 实现

Vue 路由实现方法 Vue Router 是 Vue.js 的官方路由管理器,用于构建单页面应用(SPA)。以下是实现 Vue 路由的详细步骤: 安装 Vue Router 通过 npm 或 ya…

vue实现slider

vue实现slider

Vue 实现 Slider 组件 使用 Vue 实现 Slider 组件可以通过原生开发或借助第三方库。以下是两种常见方法: 原生实现 Slider 创建一个基础的 Slider 组件,通过 v-m…

通过vue实现

通过vue实现

Vue 实现方法 安装 Vue 确保已安装 Node.js 和 npm,通过以下命令安装 Vue CLI: npm install -g @vue/cli 创建 Vue 项目 使用 Vue CLI…

vue实现ppt

vue实现ppt

Vue 实现 PPT 的功能 Vue 可以结合第三方库或自定义组件实现类似 PPT 的演示效果。以下是几种常见的方法: 使用第三方库 reveal.js reveal.js 是一个流行的演示框架,支…

vue实现权限

vue实现权限

Vue 实现权限管理的方法 在 Vue 项目中实现权限管理通常涉及路由控制、按钮权限和接口权限。以下是几种常见的实现方式: 路由权限控制 通过动态路由的方式,根据用户权限加载不同的路由配置。可以使用…