m-line.vue 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <template>
  2. <view
  3. class="u-line"
  4. :style="[lineStyle]"
  5. >
  6. </view>
  7. </template>
  8. <!-- <m-line color="#B8B8B8" length="186rpx" :hairline="true"></m-line> -->
  9. <script>
  10. export default {
  11. name: 'm-line',
  12. props: {
  13. color: {
  14. type: String,
  15. default: "#BBBBBB"
  16. },
  17. // 长度,竖向时表现为高度,横向时表现为长度,可以为百分比,带px单位的值等
  18. length: {
  19. type: [String, Number],
  20. default: "100%"
  21. },
  22. // 线条方向,col-竖向,row-横向
  23. direction: {
  24. type: String,
  25. default: "row"
  26. },
  27. // 是否显示细边框
  28. hairline: {
  29. type: Boolean,
  30. default: true
  31. },
  32. // 线条与上下左右元素的间距,字符串形式,如"30px"、"20px 30px"
  33. margin: {
  34. type: [String, Number],
  35. default: "0px"
  36. },
  37. marginTop:{
  38. type: [String, Number],
  39. default: "0px"
  40. },
  41. // 是否虚线,true-实线,false-虚线
  42. dashed: {
  43. type: Boolean,
  44. default: false
  45. }
  46. },
  47. computed: {
  48. lineStyle() {
  49. const style = {}
  50. style.marginTop = this.marginTop
  51. style.margin = this.margin
  52. // 如果是水平线条,边框高度为1px,再通过transform缩小一半,就是0.5px了
  53. if (this.direction === 'row') {
  54. // 此处采用兼容分开写,兼容nvue的写法
  55. style.borderBottomWidth = '1px'
  56. style.borderBottomStyle = this.dashed ? 'dashed' : 'solid'
  57. style.width = this.length
  58. if (this.hairline) style.transform = 'scaleY(0.5)'
  59. } else {
  60. // 如果是竖向线条,边框宽度为1px,再通过transform缩小一半,就是0.5px了
  61. style.borderLeftWidth = '1px'
  62. style.borderLeftStyle = this.dashed ? 'dashed' : 'solid'
  63. style.height = this.length
  64. if (this.hairline) style.transform = 'scaleX(0.5)'
  65. }
  66. style.borderColor = this.color
  67. return style;
  68. }
  69. }
  70. }
  71. </script>
  72. <style lang="scss" scoped>
  73. .u-line {
  74. /* #ifndef APP-NVUE */
  75. vertical-align: middle;
  76. /* #endif */
  77. }
  78. </style>