blob: add01cba02eafa9ff81b7a02870c9454e37401dc [file] [log] [blame]
guangchao.xu070005a2020-12-07 09:56:40 +08001<template>
2 <view
3 class="u-card"
4 @tap.stop="click"
5 :class="{ 'u-border': border, 'u-card-full': full, 'u-card--border': borderRadius > 0 }"
6 :style="{
7 borderRadius: borderRadius + 'rpx',
8 margin: margin,
9 boxShadow: boxShadow
10 }"
11 >
12 <view
13 v-if="showHead"
14 class="u-card__head"
15 :style="[{padding: padding + 'rpx'}, headStyle]"
16 :class="{
17 'u-border-bottom': headBorderBottom
18 }"
19 @tap="headClick"
20 >
21 <view v-if="!$slots.head" class="u-flex u-row-between">
22 <view class="u-card__head--left u-flex u-line-1" v-if="title">
23 <image
24 :src="thumb"
25 class="u-card__head--left__thumb"
26 mode="aspectfull"
27 v-if="thumb"
28 :style="{
29 height: thumbWidth + 'rpx',
30 width: thumbWidth + 'rpx',
31 borderRadius: thumbCircle ? '100rpx' : '6rpx'
32 }"
33 ></image>
34 <text
35 class="u-card__head--left__title u-line-1"
36 :style="{
37 fontSize: titleSize + 'rpx',
38 color: titleColor
39 }"
40 >
41 {{ title }}
42 </text>
43 </view>
44 <view class="u-card__head--right u-line-1" v-if="subTitle">
45 <text
46 class="u-card__head__title__text"
47 :style="{
48 fontSize: subTitleSize + 'rpx',
49 color: subTitleColor
50 }"
51 >
52 {{ subTitle }}
53 </text>
54 </view>
55 </view>
56 <slot name="head" v-else />
57 </view>
58 <view @tap="bodyClick" class="u-card__body" :style="[{padding: padding + 'rpx'}, bodyStyle]"><slot name="body" /></view>
59 <view
60 v-if="showFoot"
61 class="u-card__foot"
62 @tap="footClick"
63 :style="[{padding: $slots.foot ? padding + 'rpx' : 0}, footStyle]"
64 :class="{
65 'u-border-top': footBorderTop
66 }"
67 >
68 <slot name="foot" />
69 </view>
70 </view>
71</template>
72
73<script>
74/**
75 * card 卡片
76 * @description 卡片组件一般用于多个列表条目,且风格统一的场景
77 * @tutorial https://www.uviewui.com/components/card.html
78 * @property {Boolean} full 卡片与屏幕两侧是否留空隙(默认false)
79 * @property {String} title 头部左边的标题
80 * @property {String} title-color 标题颜色(默认#303133)
81 * @property {String | Number} title-size 标题字体大小,单位rpx(默认30)
82 * @property {String} sub-title 头部右边的副标题
83 * @property {String} sub-title-color 副标题颜色(默认#909399)
84 * @property {String | Number} sub-title-size 副标题字体大小(默认26)
85 * @property {Boolean} border 是否显示边框(默认true)
86 * @property {String | Number} index 用于标识点击了第几个卡片
87 * @property {String} box-shadow 卡片外围阴影,字符串形式(默认none)
88 * @property {String} margin 卡片与屏幕两边和上下元素的间距,需带单位,如"30rpx 20rpx"(默认30rpx)
89 * @property {String | Number} border-radius 卡片整体的圆角值,单位rpx(默认16)
90 * @property {Object} head-style 头部自定义样式,对象形式
91 * @property {Object} body-style 中部自定义样式,对象形式
92 * @property {Object} foot-style 底部自定义样式,对象形式
93 * @property {Boolean} head-border-bottom 是否显示头部的下边框(默认true)
94 * @property {Boolean} foot-border-top 是否显示底部的上边框(默认true)
95 * @property {Boolean} show-head 是否显示头部(默认true)
96 * @property {Boolean} show-head 是否显示尾部(默认true)
97 * @property {String} thumb 缩略图路径,如设置将显示在标题的左边,不建议使用相对路径
98 * @property {String | Number} thumb-width 缩略图的宽度,高等于宽,单位rpx(默认60)
99 * @property {Boolean} thumb-circle 缩略图是否为圆形(默认false)
100 * @event {Function} click 整个卡片任意位置被点击时触发
101 * @event {Function} head-click 卡片头部被点击时触发
102 * @event {Function} body-click 卡片主体部分被点击时触发
103 * @event {Function} foot-click 卡片底部部分被点击时触发
104 * @example <u-card padding="30" title="card"></u-card>
105 */
106export default {
107 name: 'u-card',
108 props: {
109 // 与屏幕两侧是否留空隙
110 full: {
111 type: Boolean,
112 default: false
113 },
114 // 标题
115 title: {
116 type: String,
117 default: ''
118 },
119 // 标题颜色
120 titleColor: {
121 type: String,
122 default: '#303133'
123 },
124 // 标题字体大小,单位rpx
125 titleSize: {
126 type: [Number, String],
127 default: '30'
128 },
129 // 副标题
130 subTitle: {
131 type: String,
132 default: ''
133 },
134 // 副标题颜色
135 subTitleColor: {
136 type: String,
137 default: '#909399'
138 },
139 // 副标题字体大小,单位rpx
140 subTitleSize: {
141 type: [Number, String],
142 default: '26'
143 },
144 // 是否显示外部边框,只对full=false时有效(卡片与边框有空隙时)
145 border: {
146 type: Boolean,
147 default: true
148 },
149 // 用于标识点击了第几个
150 index: {
151 type: [Number, String, Object],
152 default: ''
153 },
154 // 用于隔开上下左右的边距,带单位的写法,如:"30rpx 30rpx","20rpx 20rpx 30rpx 30rpx"
155 margin: {
156 type: String,
157 default: '30rpx'
158 },
159 // card卡片的圆角
160 borderRadius: {
161 type: [Number, String],
162 default: '16'
163 },
164 // 头部自定义样式,对象形式
165 headStyle: {
166 type: Object,
167 default() {
168 return {};
169 }
170 },
171 // 主体自定义样式,对象形式
172 bodyStyle: {
173 type: Object,
174 default() {
175 return {};
176 }
177 },
178 // 底部自定义样式,对象形式
179 footStyle: {
180 type: Object,
181 default() {
182 return {};
183 }
184 },
185 // 头部是否下边框
186 headBorderBottom: {
187 type: Boolean,
188 default: true
189 },
190 // 底部是否有上边框
191 footBorderTop: {
192 type: Boolean,
193 default: true
194 },
195 // 标题左边的缩略图
196 thumb: {
197 type: String,
198 default: ''
199 },
200 // 缩略图宽高,单位rpx
201 thumbWidth: {
202 type: [String, Number],
203 default: '60'
204 },
205 // 缩略图是否为圆形
206 thumbCircle: {
207 type: Boolean,
208 default: false
209 },
210 // 给head,body,foot的内边距
211 padding: {
212 type: [String, Number],
213 default: '30'
214 },
215 // 是否显示头部
216 showHead: {
217 type: Boolean,
218 default: true
219 },
220 // 是否显示尾部
221 showFoot: {
222 type: Boolean,
223 default: true
224 },
225 // 卡片外围阴影,字符串形式
226 boxShadow: {
227 type: String,
228 default: 'none'
229 }
230 },
231 data() {
232 return {};
233 },
234 methods: {
235 click() {
236 this.$emit('click', this.index);
237 },
238 headClick() {
239 this.$emit('head-click', this.index);
240 },
241 bodyClick() {
242 this.$emit('body-click', this.index);
243 },
244 footClick() {
245 this.$emit('foot-click', this.index);
246 }
247 }
248};
249</script>
250
251<style lang="scss" scoped>
252@import "../../libs/css/style.components.scss";
253
254.u-card {
255 position: relative;
256 overflow: hidden;
257 font-size: 28rpx;
258 background-color: #ffffff;
259 box-sizing: border-box;
260
261 &-full {
262 // 如果是与屏幕之间不留空隙,应该设置左右边距为0
263 margin-left: 0 !important;
264 margin-right: 0 !important;
265 }
266
267 &--border:after {
268 border-radius: 16rpx;
269 }
270
271 &__head {
272 &--left {
273 color: $u-main-color;
274
275 &__thumb {
276 margin-right: 16rpx;
277 }
278
279 &__title {
280 max-width: 400rpx;
281 }
282 }
283
284 &--right {
285 color: $u-tips-color;
286 margin-left: 6rpx;
287 }
288 }
289
290 &__body {
291 color: $u-content-color;
292 }
293
294 &__foot {
295 color: $u-tips-color;
296 }
297}
298</style>