u-icon.vue 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <template>
  2. <view :style="[customStyle]" class="u-icon" @tap="click" :class="['u-icon--' + labelPos]">
  3. <image class="u-icon__img" v-if="isImg" :src="name" :mode="imgMode" :style="[imgStyle]"></image>
  4. <text v-else class="u-icon__icon" :class="customClass" :style="[iconStyle]" :hover-class="hoverClass" @touchstart="touchstart"></text>
  5. <text v-if="label" class="u-icon__label" :style="{
  6. color: labelColor,
  7. fontSize: $u.addUnit(labelSize),
  8. marginLeft: labelPos == 'right' ? $u.addUnit(marginLeft) : 0,
  9. marginTop: labelPos == 'bottom' ? $u.addUnit(marginTop) : 0,
  10. marginRight: labelPos == 'left' ? $u.addUnit(marginRight) : 0,
  11. marginBottom: labelPos == 'top' ? $u.addUnit(marginBottom) : 0,
  12. }">{{label}}</text>
  13. </view>
  14. </template>
  15. <script>
  16. /**
  17. * icon 图标
  18. * @description 基于字体的图标集,包含了大多数常见场景的图标。
  19. * @tutorial https://www.uviewui.com/components/icon.html
  20. * @property {String} name 图标名称,见示例图标集
  21. * @property {String} color 图标颜色(默认inherit)
  22. * @property {String | Number} size 图标字体大小,单位rpx(默认32)
  23. * @property {String | Number} label-size label字体大小,单位rpx(默认28)
  24. * @property {String} label 图标右侧的label文字(默认28)
  25. * @property {String} label-pos label文字相对于图标的位置,只能right或bottom(默认right)
  26. * @property {String} label-color label字体颜色(默认#606266)
  27. * @property {Object} custom-style icon的样式,对象形式
  28. * @property {String} custom-prefix 自定义字体图标库时,需要写上此值
  29. * @property {String | Number} margin-left label在右侧时与图标的距离,单位rpx(默认6)
  30. * @property {String | Number} margin-top label在下方时与图标的距离,单位rpx(默认6)
  31. * @property {String | Number} margin-bottom label在上方时与图标的距离,单位rpx(默认6)
  32. * @property {String | Number} margin-right label在左侧时与图标的距离,单位rpx(默认6)
  33. * @property {String} label-pos label相对于图标的位置,只能right或bottom(默认right)
  34. * @property {String} index 一个用于区分多个图标的值,点击图标时通过click事件传出
  35. * @property {String} hover-class 图标按下去的样式类,用法同uni的view组件的hover-class参数,详情见官网
  36. * @property {String} width 显示图片小图标时的宽度
  37. * @property {String} height 显示图片小图标时的高度
  38. * @property {String} top 图标在垂直方向上的定位
  39. * @event {Function} click 点击图标时触发
  40. * @example <u-icon name="photo" color="#2979ff" size="28"></u-icon>
  41. */
  42. export default {
  43. name: 'u-icon',
  44. props: {
  45. // 图标类名
  46. name: {
  47. type: String,
  48. default: ''
  49. },
  50. // 图标颜色,可接受主题色
  51. color: {
  52. type: String,
  53. default: ''
  54. },
  55. // 字体大小,单位rpx
  56. size: {
  57. type: [Number, String],
  58. default: 'inherit'
  59. },
  60. // 是否显示粗体
  61. bold: {
  62. type: Boolean,
  63. default: false
  64. },
  65. // 点击图标的时候传递事件出去的index(用于区分点击了哪一个)
  66. index: {
  67. type: [Number, String],
  68. default: ''
  69. },
  70. // 触摸图标时的类名
  71. hoverClass: {
  72. type: String,
  73. default: ''
  74. },
  75. // 自定义扩展前缀,方便用户扩展自己的图标库
  76. customPrefix: {
  77. type: String,
  78. default: 'uicon'
  79. },
  80. // 图标右边或者下面的文字
  81. label: {
  82. type: String,
  83. default: ''
  84. },
  85. // label的位置,只能右边或者下边
  86. labelPos: {
  87. type: String,
  88. default: 'right'
  89. },
  90. // label的大小
  91. labelSize: {
  92. type: [String, Number],
  93. default: '28'
  94. },
  95. // label的颜色
  96. labelColor: {
  97. type: String,
  98. default: '#606266'
  99. },
  100. // label与图标的距离(横向排列)
  101. marginLeft: {
  102. type: [String, Number],
  103. default: '6'
  104. },
  105. // label与图标的距离(竖向排列)
  106. marginTop: {
  107. type: [String, Number],
  108. default: '6'
  109. },
  110. // label与图标的距离(竖向排列)
  111. marginRight: {
  112. type: [String, Number],
  113. default: '6'
  114. },
  115. // label与图标的距离(竖向排列)
  116. marginBottom: {
  117. type: [String, Number],
  118. default: '6'
  119. },
  120. // 图片的mode
  121. imgMode: {
  122. type: String,
  123. default: 'widthFix'
  124. },
  125. // 自定义样式
  126. customStyle: {
  127. type: Object,
  128. default() {
  129. return {}
  130. }
  131. },
  132. // 用于显示图片小图标时,图片的宽度
  133. width: {
  134. type: [String, Number],
  135. default: ''
  136. },
  137. // 用于显示图片小图标时,图片的高度
  138. height: {
  139. type: [String, Number],
  140. default: ''
  141. },
  142. // 用于解决某些情况下,让图标垂直居中的用途
  143. top: {
  144. type: [String, Number],
  145. default: 0
  146. }
  147. },
  148. computed: {
  149. customClass() {
  150. let classes = [];
  151. classes.push(this.customPrefix + '-' + this.name);
  152. // uView的自定义图标类名为u-iconfont
  153. if (this.customPrefix == 'uicon') classes.push('u-iconfont');
  154. else classes.push(this.customPrefix);
  155. // 主题色,通过类配置
  156. if (this.color && this.$u.config.type.includes(this.color)) classes.push('u-icon__icon--' + this.color);
  157. // 阿里,头条,百度小程序通过数组绑定类名时,无法直接使用[a, b, c]的形式,否则无法识别
  158. // 故需将其拆成一个字符串的形式,通过空格隔开各个类名
  159. //#ifdef MP-ALIPAY || MP-TOUTIAO || MP-BAIDU
  160. classes = classes.join(' ');
  161. //#endif
  162. return classes;
  163. },
  164. iconStyle() {
  165. let style = {};
  166. style = {
  167. fontSize: this.size == 'inherit' ? 'inherit' : this.$u.addUnit(this.size),
  168. fontWeight: this.bold ? 'bold' : 'normal',
  169. // 某些特殊情况需要设置一个到顶部的距离,才能更好的垂直居中
  170. top: this.$u.addUnit(this.top)
  171. };
  172. // 非主题色值时,才当作颜色值
  173. if (this.color && !this.$u.config.type.includes(this.color)) style.color = this.color;
  174. return style;
  175. },
  176. // 判断传入的name属性,是否图片路径,只要带有"/"均认为是图片形式
  177. isImg() {
  178. return this.name.indexOf('/') !== -1;
  179. },
  180. imgStyle() {
  181. let style = {};
  182. // 如果设置width和height属性,则优先使用,否则使用size属性
  183. style.width = this.width ? this.$u.addUnit(this.width) : this.$u.addUnit(this.size);
  184. style.height = this.height ? this.$u.addUnit(this.height) : this.$u.addUnit(this.size);
  185. return style;
  186. }
  187. },
  188. methods: {
  189. click() {
  190. this.$emit('click', this.index);
  191. },
  192. touchstart() {
  193. this.$emit('touchstart', this.index);
  194. }
  195. }
  196. };
  197. </script>
  198. <style scoped lang="scss">
  199. @import "../../libs/css/style.components.scss";
  200. @import '../../iconfont.css';
  201. .u-icon {
  202. display: inline-flex;
  203. align-items: center;
  204. &--left {
  205. flex-direction: row-reverse;
  206. align-items: center;
  207. }
  208. &--right {
  209. flex-direction: row;
  210. align-items: center;
  211. }
  212. &--top {
  213. flex-direction: column-reverse;
  214. justify-content: center;
  215. }
  216. &--bottom {
  217. flex-direction: column;
  218. justify-content: center;
  219. }
  220. &__icon {
  221. position: relative;
  222. &--primary {
  223. color: $u-type-primary;
  224. }
  225. &--success {
  226. color: $u-type-success;
  227. }
  228. &--error {
  229. color: $u-type-error;
  230. }
  231. &--warning {
  232. color: $u-type-warning;
  233. }
  234. &--info {
  235. color: $u-type-info;
  236. }
  237. }
  238. &__img {
  239. height: auto;
  240. will-change: transform;
  241. }
  242. &__label {
  243. line-height: 1;
  244. }
  245. }
  246. </style>