uni-card.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <template>
  2. <view class="uni-card" :class="{ 'uni-card--full': isFull, 'uni-card--shadow': isShadow,'uni-card--border':border}"
  3. :style="{'margin':isFull?0:margin,'padding':spacing,'box-shadow':isShadow?shadow:''}">
  4. <!-- 封面 -->
  5. <slot name="cover">
  6. <view v-if="cover" class="uni-card__cover">
  7. <image class="uni-card__cover-image" mode="widthFix" @click="onClick('cover')" :src="cover"></image>
  8. </view>
  9. </slot>
  10. <slot name="title">
  11. <view v-if="title || extra" class="uni-card__header">
  12. <!-- 卡片标题 -->
  13. <view class="uni-card__header-box" @click="onClick('title')">
  14. <view v-if="thumbnail" class="uni-card__header-avatar">
  15. <image class="uni-card__header-avatar-image" :src="thumbnail" mode="aspectFit" />
  16. </view>
  17. <view class="uni-card__header-content">
  18. <text class="uni-card__header-content-title uni-ellipsis">{{ title }}</text>
  19. <text v-if="title&&subTitle"
  20. class="uni-card__header-content-subtitle uni-ellipsis">{{ subTitle }}</text>
  21. </view>
  22. </view>
  23. <view class="uni-card__header-extra" @click="onClick('extra')">
  24. <slot name="extra">
  25. <text class="uni-card__header-extra-text">{{ extra }}</text>
  26. </slot>
  27. </view>
  28. </view>
  29. </slot>
  30. <!-- 卡片内容 -->
  31. <view class="uni-card__content" :style="{padding:padding}" @click="onClick('content')">
  32. <slot></slot>
  33. </view>
  34. <view class="uni-card__actions" @click="onClick('actions')">
  35. <slot name="actions"></slot>
  36. </view>
  37. </view>
  38. </template>
  39. <script>
  40. /**
  41. * Card 卡片
  42. * @description 卡片视图组件
  43. * @tutorial https://ext.dcloud.net.cn/plugin?id=22
  44. * @property {String} title 标题文字
  45. * @property {String} subTitle 副标题
  46. * @property {Number} padding 内容内边距
  47. * @property {Number} margin 卡片外边距
  48. * @property {Number} spacing 卡片内边距
  49. * @property {String} extra 标题额外信息
  50. * @property {String} cover 封面图(本地路径需要引入)
  51. * @property {String} thumbnail 标题左侧缩略图
  52. * @property {Boolean} is-full = [true | false] 卡片内容是否通栏,为 true 时将去除padding值
  53. * @property {Boolean} is-shadow = [true | false] 卡片内容是否开启阴影
  54. * @property {String} shadow 卡片阴影
  55. * @property {Boolean} border 卡片边框
  56. * @event {Function} click 点击 Card 触发事件
  57. */
  58. export default {
  59. name: 'UniCard',
  60. emits: ['click'],
  61. props: {
  62. title: {
  63. type: String,
  64. default: ''
  65. },
  66. subTitle: {
  67. type: String,
  68. default: ''
  69. },
  70. padding: {
  71. type: String,
  72. default: '10px'
  73. },
  74. margin: {
  75. type: String,
  76. default: '15px'
  77. },
  78. spacing: {
  79. type: String,
  80. default: '0 10px'
  81. },
  82. extra: {
  83. type: String,
  84. default: ''
  85. },
  86. cover: {
  87. type: String,
  88. default: ''
  89. },
  90. thumbnail: {
  91. type: String,
  92. default: ''
  93. },
  94. isFull: {
  95. // 内容区域是否通栏
  96. type: Boolean,
  97. default: false
  98. },
  99. isShadow: {
  100. // 是否开启阴影
  101. type: Boolean,
  102. default: true
  103. },
  104. shadow: {
  105. type: String,
  106. default: '0px 0px 3px 1px rgba(0, 0, 0, 0.08)'
  107. },
  108. border: {
  109. type: Boolean,
  110. default: true
  111. }
  112. },
  113. methods: {
  114. onClick(type) {
  115. this.$emit('click', type)
  116. }
  117. }
  118. }
  119. </script>
  120. <style lang="scss">
  121. $uni-border-3: #EBEEF5 !default;
  122. $uni-shadow-base:0 0px 6px 1px rgba($color: #a5a5a5, $alpha: 0.2) !default;
  123. $uni-main-color: #3a3a3a !default;
  124. $uni-base-color: #6a6a6a !default;
  125. $uni-secondary-color: #909399 !default;
  126. $uni-spacing-sm: 8px !default;
  127. $uni-border-color:$uni-border-3;
  128. $uni-shadow: $uni-shadow-base;
  129. $uni-card-title: 15px;
  130. $uni-cart-title-color:$uni-main-color;
  131. $uni-card-subtitle: 12px;
  132. $uni-cart-subtitle-color:$uni-secondary-color;
  133. $uni-card-spacing: 10px;
  134. $uni-card-content-color: $uni-base-color;
  135. .uni-card {
  136. margin: $uni-card-spacing;
  137. padding: 0 $uni-spacing-sm;
  138. border-radius: 4px;
  139. overflow: hidden;
  140. font-family: Helvetica Neue, Helvetica, PingFang SC, Hiragino Sans GB, Microsoft YaHei, SimSun, sans-serif;
  141. background-color: #fff;
  142. flex: 1;
  143. .uni-card__cover {
  144. position: relative;
  145. margin-top: $uni-card-spacing;
  146. flex-direction: row;
  147. overflow: hidden;
  148. border-radius: 4px;
  149. .uni-card__cover-image {
  150. flex: 1;
  151. // width: 100%;
  152. /* #ifndef APP-PLUS */
  153. vertical-align: middle;
  154. /* #endif */
  155. }
  156. }
  157. .uni-card__header {
  158. display: flex;
  159. border-bottom: 1px $uni-border-color solid;
  160. flex-direction: row;
  161. align-items: center;
  162. padding: $uni-card-spacing;
  163. overflow: hidden;
  164. .uni-card__header-box {
  165. /* #ifndef APP-NVUE */
  166. display: flex;
  167. /* #endif */
  168. flex: 1;
  169. flex-direction: row;
  170. align-items: center;
  171. overflow: hidden;
  172. }
  173. .uni-card__header-avatar {
  174. width: 40px;
  175. height: 40px;
  176. overflow: hidden;
  177. border-radius: 5px;
  178. margin-right: $uni-card-spacing;
  179. .uni-card__header-avatar-image {
  180. flex: 1;
  181. width: 40px;
  182. height: 40px;
  183. }
  184. }
  185. .uni-card__header-content {
  186. /* #ifndef APP-NVUE */
  187. display: flex;
  188. /* #endif */
  189. flex-direction: column;
  190. justify-content: center;
  191. flex: 1;
  192. // height: 40px;
  193. overflow: hidden;
  194. .uni-card__header-content-title {
  195. font-size: $uni-card-title;
  196. color: $uni-cart-title-color;
  197. // line-height: 22px;
  198. }
  199. .uni-card__header-content-subtitle {
  200. font-size: $uni-card-subtitle;
  201. margin-top: 5px;
  202. color: $uni-cart-subtitle-color;
  203. }
  204. }
  205. .uni-card__header-extra {
  206. line-height: 12px;
  207. .uni-card__header-extra-text {
  208. font-size: 12px;
  209. color: $uni-cart-subtitle-color;
  210. }
  211. }
  212. }
  213. .uni-card__content {
  214. padding: $uni-card-spacing;
  215. font-size: 14px;
  216. color: $uni-card-content-color;
  217. line-height: 22px;
  218. }
  219. .uni-card__actions {
  220. font-size: 12px;
  221. }
  222. }
  223. .uni-card--border {
  224. border: 1px solid $uni-border-color;
  225. }
  226. .uni-card--shadow {
  227. position: relative;
  228. /* #ifndef APP-NVUE */
  229. box-shadow: $uni-shadow;
  230. /* #endif */
  231. }
  232. .uni-card--full {
  233. margin: 0;
  234. border-left-width: 0;
  235. border-left-width: 0;
  236. border-radius: 0;
  237. }
  238. /* #ifndef APP-NVUE */
  239. .uni-card--full:after {
  240. border-radius: 0;
  241. }
  242. /* #endif */
  243. .uni-ellipsis {
  244. /* #ifndef APP-NVUE */
  245. overflow: hidden;
  246. white-space: nowrap;
  247. text-overflow: ellipsis;
  248. /* #endif */
  249. /* #ifdef APP-NVUE */
  250. lines: 1;
  251. /* #endif */
  252. }
  253. </style>