uv-line.vue 2.4 KB

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