Skip to content

Card组件

基础用法

卡片包含标题,内容以及操作区域。 Card 组件由 header 和 body 组成。 header 是可选的,其内容取决于一个具名的 slot。

Card name
List item 1
List item 2
List item 3
List item 4
false
<template>
  <g-card class="box-card">
    <template #header>
      <div class="card-header">
        <span>Card name</span>
        <el-button class="button" text>Operation button</el-button>
      </div>
    </template>
    <div v-for="o in 4" :key="o" class="text item">{{ 'List item ' + o }}</div>
  </g-card>
  {{ isDark }}
</template>
<script setup>
import { useData } from 'vitepress'

const { isDark } = useData()
</script>
<style>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.box-card {
  width: 480px;
}
</style>
<template>
  <g-card class="box-card">
    <template #header>
      <div class="card-header">
        <span>Card name</span>
        <el-button class="button" text>Operation button</el-button>
      </div>
    </template>
    <div v-for="o in 4" :key="o" class="text item">{{ 'List item ' + o }}</div>
  </g-card>
  {{ isDark }}
</template>
<script setup>
import { useData } from 'vitepress'

const { isDark } = useData()
</script>
<style>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

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

简单卡片

卡片可以只有内容区域。

List item 1
List item 2
List item 3
List item 4
<template>
  <g-card class="box-card">
    <div v-for="o in 4" :key="o" class="text item">{{ 'List item ' + o }}</div>
  </g-card>
</template>
<style scoped>
.text {
  font-size: 14px;
}

.item {
  padding: 0px 0;
}

.box-card {
  display: inline-block;
  width: 280px;
}
</style>
<template>
  <g-card class="box-card">
    <div v-for="o in 4" :key="o" class="text item">{{ 'List item ' + o }}</div>
  </g-card>
</template>
<style scoped>
.text {
  font-size: 14px;
}

.item {
  padding: 0px 0;
}

.box-card {
  display: inline-block;
  width: 280px;
}
</style>

有图片内容的卡片

可配置定义更丰富的内容展示。

配置 body-style 属性来自定义 body 部分的样式。 在这个例子中我们还使用了 el-col 组件来布局。

<template>
  <el-row>
    <el-col
      v-for="(o, index) in 2"
      :key="o"
      :span="8"
      :offset="index > 0 ? 2 : 0"
    >
      <g-card :body-style="{ padding: '0px' }">
        <img
          src="https://shadow.elemecdn.com/app/element/hamburger.9cf7b091-55e9-11e9-a976-7f4d0b07eef6.png"
          class="image"
        />
        <div style="padding: 14px">
          <span>Yummy hamburger</span>
          <div class="bottom">
            <time class="time">{{ currentDate }}</time>
            <el-button text class="button">Operating</el-button>
          </div>
        </div>
      </g-card>
    </el-col>
  </el-row>
</template>

<script lang="ts" setup>
import { ref } from 'vue'

const currentDate = ref(new Date())
</script>

<style>
.time {
  font-size: 12px;
  color: #999;
}

.bottom {
  margin-top: 13px;
  line-height: 12px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.button {
  padding: 0;
  min-height: auto;
}

.image {
  width: 100%;
  display: block;
}
</style>
<template>
  <el-row>
    <el-col
      v-for="(o, index) in 2"
      :key="o"
      :span="8"
      :offset="index > 0 ? 2 : 0"
    >
      <g-card :body-style="{ padding: '0px' }">
        <img
          src="https://shadow.elemecdn.com/app/element/hamburger.9cf7b091-55e9-11e9-a976-7f4d0b07eef6.png"
          class="image"
        />
        <div style="padding: 14px">
          <span>Yummy hamburger</span>
          <div class="bottom">
            <time class="time">{{ currentDate }}</time>
            <el-button text class="button">Operating</el-button>
          </div>
        </div>
      </g-card>
    </el-col>
  </el-row>
</template>

<script lang="ts" setup>
import { ref } from 'vue'

const currentDate = ref(new Date())
</script>

<style>
.time {
  font-size: 12px;
  color: #999;
}

.bottom {
  margin-top: 13px;
  line-height: 12px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.button {
  padding: 0;
  min-height: auto;
}

.image {
  width: 100%;
  display: block;
}
</style>

带有阴影效果的卡片

你可以定义什么时候展示卡片的阴影效果。

通过 shadow 属性设置卡片阴影出现的时机。 该属性的值可以是:always、hover 或 never。

<template>
  <el-row :gutter="12">
    <el-col :span="8">
      <g-card shadow="always"> Always </g-card>
    </el-col>
    <el-col :span="8">
      <g-card shadow="hover"> Hover </g-card>
    </el-col>
    <el-col :span="8">
      <g-card shadow="never"> Never </g-card>
    </el-col>
  </el-row>
</template>
<template>
  <el-row :gutter="12">
    <el-col :span="8">
      <g-card shadow="always"> Always </g-card>
    </el-col>
    <el-col :span="8">
      <g-card shadow="hover"> Hover </g-card>
    </el-col>
    <el-col :span="8">
      <g-card shadow="never"> Never </g-card>
    </el-col>
  </el-row>
</template>

API

Attributes

名称类型默认值说明
headerstring-卡片标题
body-styleobject-body 的 CSS 样式
body-classstring-卡片的自定义类名
shadowenumalways卡片阴影显示时机

Slots

插槽名说明
default自定义默认内容
header卡片标题内容