u-card.vue 7.7 KB

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