u-card.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <template>
  2. <view
  3. class="u-card"
  4. @tap.stop="click"
  5. :class="{ 'u-border': border, 'u-card-full': full, 'u-card--border': round > 0 }"
  6. :style="{
  7. borderRadius: $u.addUnit(round),
  8. margin: margin,
  9. boxShadow: boxShadow
  10. }"
  11. >
  12. <view
  13. v-if="showHead"
  14. class="u-card__head"
  15. :style="[{padding: $u.addUnit(padding)}, headStyle]"
  16. :class="{
  17. 'u-border-bottom': headBorderBottom
  18. }"
  19. @tap="headClick"
  20. >
  21. <slot name="head">
  22. <view class="u-card__head--left" v-if="title">
  23. <image
  24. :src="thumb"
  25. class="u-card__head--left__thumb"
  26. mode="aspectFill"
  27. v-if="thumb"
  28. :style="{
  29. height: $u.addUnit(thumbWidth),
  30. width: $u.addUnit(thumbWidth),
  31. borderRadius: thumbCircle ? '50px' : $u.addUnit(thumbRadius)
  32. }"
  33. ></image>
  34. <text
  35. class="u-card__head--left__title"
  36. :style="{
  37. fontSize: $u.addUnit(titleSize),
  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: $u.addUnit(subTitleSize),
  49. color: subTitleColor
  50. }"
  51. >
  52. {{ subTitle }}
  53. </text>
  54. </view>
  55. </slot>
  56. </view>
  57. <view @tap="bodyClick" class="u-card__body" :style="[{padding: $u.addUnit(padding)}, bodyStyle]">
  58. <slot name="body" />
  59. </view>
  60. <view
  61. v-if="showFoot"
  62. class="u-card__foot"
  63. @tap="footClick"
  64. :style="[{padding: $slots.foot ? $u.addUnit(padding) : 0}, footStyle]"
  65. :class="{
  66. 'u-border-top': footBorderTop
  67. }"
  68. >
  69. <slot name="foot" />
  70. </view>
  71. </view>
  72. </template>
  73. <script>
  74. import props from './props.js';
  75. import mixin from '../../libs/mixin/mixin'
  76. import mpMixin from '../../libs/mixin/mpMixin'
  77. /**
  78. * card 卡片
  79. * @description 卡片组件一般用于多个列表条目,且风格统一的场景
  80. * @tutorial https://uview.d3u.cn/components/card.html
  81. * @property {Boolean} full 卡片与屏幕两侧是否留空隙(默认false)
  82. * @property {String} title 头部左边的标题
  83. * @property {String} titleColor 标题颜色(默认#303133)
  84. * @property {String | Number} titleSize 标题字体大小,单位rpx(默认30)
  85. * @property {String} subTitle 头部右边的副标题
  86. * @property {String} subTitleColor 副标题颜色(默认#909399)
  87. * @property {String | Number} subTitleSize 副标题字体大小(默认26)
  88. * @property {Boolean} border 是否显示边框(默认true)
  89. * @property {String | Number} index 用于标识点击了第几个卡片
  90. * @property {String} boxShadow 卡片外围阴影,字符串形式(默认none)
  91. * @property {String} margin 卡片与屏幕两边和上下元素的间距,需带单位,如"30rpx 20rpx"(默认30rpx)
  92. * @property {String | Number} round 卡片整体的圆角值,单位rpx(默认16)
  93. * @property {Object} headStyle 头部自定义样式,对象形式
  94. * @property {Object} bodyStyle 中部自定义样式,对象形式
  95. * @property {Object} footStyle 底部自定义样式,对象形式
  96. * @property {Boolean} headBorderBottom 是否显示头部的下边框(默认true)
  97. * @property {Boolean} footBorderTop 是否显示底部的上边框(默认true)
  98. * @property {Boolean} showHead 是否显示头部(默认true)
  99. * @property {Boolean} showFoot 是否显示尾部(默认true)
  100. * @property {String} thumb 缩略图路径,如设置将显示在标题的左边,不建议使用相对路径
  101. * @property {String | Number} thumbWidth 缩略图的宽度,高等于宽,单位rpx(默认60)
  102. * @property {Boolean} thumbCircle 缩略图是否为圆形(默认false)
  103. * @event {Function} click 整个卡片任意位置被点击时触发
  104. * @event {Function} headClick 卡片头部被点击时触发
  105. * @event {Function} bodyClick 卡片主体部分被点击时触发
  106. * @event {Function} footClick 卡片底部部分被点击时触发
  107. * @example <u-card padding="30" title="card"></u-card>
  108. */
  109. export default {
  110. name: 'u-card',
  111. mixins: [mpMixin, mixin, props],
  112. data() {
  113. return {};
  114. },
  115. // #ifdef VUE3
  116. emits: ["click","headClick","bodyClick","footClick"],
  117. // #endif
  118. methods: {
  119. click() {
  120. this.$emit('click', this.index);
  121. },
  122. headClick() {
  123. this.$emit('headClick', this.index);
  124. },
  125. bodyClick() {
  126. this.$emit('bodyClick', this.index);
  127. },
  128. footClick() {
  129. this.$emit('footClick', this.index);
  130. }
  131. }
  132. };
  133. </script>
  134. <style lang="scss" scoped>
  135. @import "../../libs/css/components.scss";
  136. .u-card {
  137. position: relative;
  138. overflow: hidden;
  139. /* #ifndef APP-NVUE */
  140. box-sizing: border-box;
  141. /* #endif */
  142. background-color: #ffffff;
  143. &-full {
  144. // 如果是与屏幕之间不留空隙,应该设置左右边距为0
  145. margin-left: 0 !important;
  146. margin-right: 0 !important;
  147. width: 100%;
  148. }
  149. &--border:after {
  150. border-radius: 8px;
  151. }
  152. &__head {
  153. @include flex();
  154. justify-content: space-between;
  155. align-items: center;
  156. &--left {
  157. @include flex();
  158. align-items: center;
  159. color: $u-main-color;
  160. flex: 1;
  161. min-width: 0;
  162. &__thumb {
  163. margin-right: 8px;
  164. flex-shrink: 0;
  165. }
  166. &__title {
  167. /* #ifndef APP-NVUE */
  168. white-space: nowrap;
  169. overflow: hidden;
  170. text-overflow: ellipsis;
  171. flex: 1;
  172. min-width: 0;
  173. /* #endif */
  174. }
  175. }
  176. &--right {
  177. color: $u-tips-color;
  178. margin-left: 3px;
  179. }
  180. }
  181. &__body {
  182. color: $u-content-color;
  183. }
  184. &__foot {
  185. color: $u-tips-color;
  186. }
  187. }
  188. </style>