u-line.vue 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <template>
  2. <view
  3. class="u-line"
  4. :style="[lineStyle]"
  5. >
  6. </view>
  7. </template>
  8. <script>
  9. import { props } from './props';
  10. import { mpMixin } from '../../libs/mixin/mpMixin';
  11. import { mixin } from '../../libs/mixin/mixin';
  12. import { addUnit, addStyle, deepMerge } from '../../libs/function/index';
  13. /**
  14. * line 线条
  15. * @description 此组件一般用于显示一根线条,用于分隔内容块,有横向和竖向两种模式,且能设置0.5px线条,使用也很简单
  16. * @tutorial https://ijry.github.io/uview-plus/components/line.html
  17. * @property {String} color 线条的颜色 ( 默认 '#d6d7d9' )
  18. * @property {String | Number} length 长度,竖向时表现为高度,横向时表现为长度,可以为百分比,带px单位的值等 ( 默认 '100%' )
  19. * @property {String} direction 线条的方向,row-横向,col-竖向 (默认 'row' )
  20. * @property {Boolean} hairline 是否显示细线条 (默认 true )
  21. * @property {String | Number} margin 线条与上下左右元素的间距,字符串形式,如"30px" (默认 0 )
  22. * @property {Boolean} dashed 是否虚线,true-虚线,false-实线 (默认 false )
  23. * @property {Object} customStyle 定义需要用到的外部样式
  24. * @example <u-line color="red"></u-line>
  25. */
  26. export default {
  27. name: 'u-line',
  28. mixins: [mpMixin, mixin, props],
  29. computed: {
  30. lineStyle() {
  31. const style = {}
  32. style.margin = this.margin
  33. // 如果是水平线条,边框高度为1px,再通过transform缩小一半,就是0.5px了
  34. if (this.direction === 'row') {
  35. // 此处采用兼容分开写,兼容nvue的写法
  36. style.borderBottomWidth = '1px'
  37. style.borderBottomStyle = this.dashed ? 'dashed' : 'solid'
  38. style.width = addUnit(this.length)
  39. if (this.hairline) style.transform = 'scaleY(0.5)'
  40. } else {
  41. // 如果是竖向线条,边框宽度为1px,再通过transform缩小一半,就是0.5px了
  42. style.borderLeftWidth = '1px'
  43. style.borderLeftStyle = this.dashed ? 'dashed' : 'solid'
  44. style.height = addUnit(this.length)
  45. if (this.hairline) style.transform = 'scaleX(0.5)'
  46. }
  47. style.borderColor = this.color
  48. return deepMerge(style, addStyle(this.customStyle))
  49. }
  50. }
  51. }
  52. </script>
  53. <style lang="scss" scoped>
  54. @import "../../libs/css/components.scss";
  55. .u-line {
  56. /* #ifndef APP-NVUE */
  57. vertical-align: middle;
  58. /* #endif */
  59. }
  60. </style>