Card 卡片
17:06说明
把头部、主体与底部组织成一张风格统一的卡片:头部由 title / sub-title 与可选 thumb 缩略图构成,主体与底部由默认插槽及 body / foot 插槽填充,传入 head 插槽则整体替换默认头部。支持圆角、外围阴影与边框。
兼容性
| 平台 | 支持情况 |
|---|---|
| App | ✅ 支持 |
| H5 | ✅ 支持 |
| 微信小程序 | ✅ 支持 |
示例
基础用法
通过 title / sub-title 配置头部标题与副标题,默认插槽填充主体内容。
查看源码
vue
<template>
<ux-card title="卡片标题" sub-title="副标题" @click="onClick">
<text class="body-text">卡片内容区域,支持自定义主体插槽,头部可配置标题与副标题。</text>
</ux-card>
</template>
<script setup lang="uts">
const onClick = (index: string | number) => {
console.log('card click:', index);
};
</script>
<style scoped>
.body-text {
font-size: 13px;
color: #909399;
line-height: 20px;
}
</style>缩略图 thumb
通过 thumb 在标题左侧显示缩略图,thumb-circle 控制是否为圆形。
查看源码
vue
<template>
<ux-card
title="带缩略图"
sub-title="圆角头像"
thumb="https://ik.imagekit.io/anyup/uview-pro/common/logo-new.png"
:thumb-circle="true"
>
<text class="body-text">通过 thumb 属性在标题左侧显示缩略图。</text>
</ux-card>
</template>
<style scoped>
.body-text {
font-size: 13px;
color: #909399;
line-height: 20px;
}
</style>自定义插槽
通过 head / foot 插槽自定义头部与底部,默认插槽填充主体,组合出图文信息流卡片。
查看源码
vue
<template>
<ux-card :border-radius="14" box-shadow="0 2px 12px rgba(0, 0, 0, 0.06)">
<template #head>
<view class="feed-head">
<text class="feed-title">设计分享圈</text>
<text class="feed-follow">+ 关注</text>
</view>
</template>
<view class="feed-author">
<image class="feed-avatar" src="https://picsum.photos/seed/card-avatar/72/72" mode="aspectFill" />
<view class="feed-meta">
<text class="feed-name">uView 设计</text>
<text class="feed-time">3分钟前</text>
</view>
</view>
<text class="feed-content">今晚分享一套新的配色方案,主打暖橙渐变,适合电商促销场景……</text>
<image class="feed-image" src="https://picsum.photos/seed/card-post/600/360" mode="widthFix" />
<template #foot>
<view class="feed-actions">
<text class="feed-action">点赞 128</text>
<text class="feed-action">评论 32</text>
<text class="feed-action">转发 12</text>
</view>
</template>
</ux-card>
</template>
<style scoped>
.feed-head {
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-between;
width: 100%;
}
.feed-title {
font-size: 15px;
font-weight: bold;
color: #303133;
}
.feed-follow {
font-size: 13px;
color: #2979ff;
}
.feed-author {
display: flex;
flex-direction: row;
align-items: center;
margin-bottom: 10px;
}
.feed-avatar {
width: 36px;
height: 36px;
border-radius: 50%;
margin-right: 10px;
}
.feed-meta {
display: flex;
flex-direction: column;
}
.feed-name {
font-size: 14px;
color: #303133;
font-weight: bold;
}
.feed-time {
font-size: 12px;
color: #909399;
margin-top: 2px;
}
.feed-content {
font-size: 14px;
color: #606266;
line-height: 21px;
margin-bottom: 12px;
}
.feed-image {
width: 100%;
border-radius: 8px;
}
.feed-actions {
display: flex;
flex-direction: row;
align-items: center;
}
.feed-action {
font-size: 13px;
color: #909399;
margin-right: 28px;
}
</style>阴影 box-shadow
通过 box-shadow 为卡片添加外围阴影,字符串形式。
查看源码
vue
<template>
<ux-card title="数据概览" box-shadow="0 6px 18px rgba(0, 0, 0, 0.08)" :border-radius="12">
<view class="stats">
<view class="stat">
<text class="stat-num">128</text>
<text class="stat-label">今日访问</text>
</view>
<view class="stat-divider" />
<view class="stat">
<text class="stat-num">3.2w</text>
<text class="stat-label">累计订单</text>
</view>
<view class="stat-divider" />
<view class="stat">
<text class="stat-num">209</text>
<text class="stat-label">会员数</text>
</view>
</view>
</ux-card>
</template>
<style scoped>
.stats {
display: flex;
flex-direction: row;
align-items: center;
padding: 4px 0 2px;
}
.stat {
display: flex;
flex-direction: column;
align-items: center;
flex: 1;
}
.stat-num {
font-size: 20px;
font-weight: bold;
color: #303133;
}
.stat-label {
font-size: 12px;
color: #909399;
margin-top: 4px;
}
.stat-divider {
width: 1px;
height: 30px;
background-color: #e4e7ed;
}
</style>圆角 border-radius
通过 border-radius 设置卡片整体的圆角值,单位 px。
查看源码
vue
<template>
<ux-card title="自定义圆角" :border-radius="15">
<text class="body-text">通过 border-radius 属性调整卡片圆角,0 为直角。</text>
</ux-card>
</template>
<style scoped>
.body-text {
font-size: 13px;
color: #909399;
line-height: 20px;
}
</style>边框 border
通过 border 控制是否显示卡片外部边框。
查看源码
vue
<template>
<ux-card title="显示边框" sub-title="带边框" :border="true">
<text class="body-text">通过 border 属性显示卡片外部边框。</text>
</ux-card>
</template>
<style scoped>
.body-text {
font-size: 13px;
color: #909399;
line-height: 20px;
}
</style>全屏无间距 full
full=true 时卡片与屏幕两侧不留空隙,左右边距为 0。
查看源码
vue
<template>
<ux-card title="全屏卡片" sub-title="无间距" :full="true" :border="true">
<text class="body-text">full=true 时卡片与屏幕两侧不留空隙。</text>
</ux-card>
</template>
<style scoped>
.body-text {
font-size: 13px;
color: #909399;
line-height: 20px;
}
</style>API
Props
| 属性 | 说明 | 类型 | 默认值 | 版本 |
|---|---|---|---|---|
customStyle | 自定义根节点样式 | UTSJSONObject | - | - |
customClass | 自定义根节点样式类 | string | '' | - |
full | 与屏幕两侧是否留空隙 | boolean | false | - |
title | 头部左边的标题 | string | '' | - |
titleColor | 标题颜色 | string | '' | - |
titleSize | 标题字体大小,单位px | string | number | '15' | - |
subTitle | 头部右边的副标题 | string | '' | - |
subTitleColor | 副标题颜色 | string | '' | - |
subTitleSize | 副标题字体大小,单位px | string | number | '13' | - |
border | 是否显示外部边框 | boolean | false | - |
index | 用于标识点击了第几个卡片 | CardIndex | '' | - |
boxShadow | 卡片外围阴影,字符串形式 | string | 'none' | - |
margin | 卡片与上下左右元素的间距,需带单位 | string | '15px' | - |
borderRadius | 卡片整体的圆角值,单位px | string | number | '8' | - |
headStyle | 头部自定义样式 | UTSJSONObject | - | - |
bodyStyle | 主体自定义样式 | UTSJSONObject | - | - |
footStyle | 底部自定义样式 | UTSJSONObject | - | - |
headBorderBottom | 头部是否显示下边框 | boolean | true | - |
footBorderTop | 底部是否显示上边框 | boolean | true | - |
showHead | 是否显示头部 | boolean | true | - |
showFoot | 是否显示尾部 | boolean | true | - |
thumb | 标题左边缩略图路径 | string | '' | - |
thumbWidth | 缩略图宽度和高度,单位px | string | number | '30' | - |
thumbCircle | 缩略图是否为圆形 | boolean | false | - |
padding | 头部/主体/底部的内边距,单位px | string | number | '15' | - |
Events
| 事件名 | 说明 | 回调参数 | 版本 |
|---|---|---|---|
click | 整个卡片任意位置被点击时触发 | index: CardIndex | - |
head-click | 卡片头部被点击时触发 | index: CardIndex | - |
body-click | 卡片主体部分被点击时触发 | index: CardIndex | - |
foot-click | 卡片底部部分被点击时触发 | index: CardIndex | - |
Slots
| 插槽名 | 说明 | 版本 |
|---|---|---|
head | 自定义头部内容;传入后整体替换由 title / sub-title / thumb 组成的默认头部 | - |
default | 卡片主体内容 | - |
body | 附加到主体区域的内容,渲染在默认插槽之后 | - |
foot | 自定义底部内容 | - |
注意事项
- 传入
head插槽后,由title/sub-title/thumb组成的默认头部不再渲染,二者不会叠加。 - 头部与底部由
show-head/show-foot控制显隐,主体区域始终渲染;show-foot为 true 但未传foot插槽时,底部为空且内边距为 0。 full=true时组件用内联样式强制margin: 0并让卡片宽度占满父级,此时margin属性不再生效。head-border-bottom/foot-border-top分别控制头部下边框与底部上边框,默认均为 true。- 头部标题超长时限宽 200px 并以省略号截断;
sub-title与标题在同一行两端对齐。 - 除
click外,源码还提供head-click/body-click/foot-click,分别在头部、主体、底部被点击时触发(回调同为index)。