u-card.vue 6.5 KB

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