u-line.vue 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <template>
  2. <view class="u-line" :style="[lineStyle]">
  3. </view>
  4. </template>
  5. <script>
  6. /**
  7. * line 线条
  8. * @description 此组件一般用于显示一根线条,用于分隔内容块,有横向和竖向两种模式,且能设置0.5px线条,使用也很简单
  9. * @tutorial https://www.uviewui.com/components/line.html
  10. * @property {String} color 线条的颜色(默认#e4e7ed)
  11. * @property {String} length 长度,竖向时表现为高度,横向时表现为长度,可以为百分比,带rpx单位的值等
  12. * @property {String} direction 线条的方向,row-横向,col-竖向(默认row)
  13. * @property {Boolean} hair-line 是否显示细线条(默认true)
  14. * @property {String} margin 线条与上下左右元素的间距,字符串形式,如"30rpx"
  15. * @example <u-line color="red"></u-line>
  16. */
  17. export default {
  18. name: 'u-line',
  19. props: {
  20. color: {
  21. type: String,
  22. default: '#e4e7ed'
  23. },
  24. // 长度,竖向时表现为高度,横向时表现为长度,可以为百分比,带rpx单位的值等
  25. length: {
  26. type: String,
  27. default: '100%'
  28. },
  29. // 线条方向,col-竖向,row-横向
  30. direction: {
  31. type: String,
  32. default: 'row'
  33. },
  34. // 是否显示细边框
  35. hairLine: {
  36. type: Boolean,
  37. default: true
  38. },
  39. // 线条与上下左右元素的间距,字符串形式,如"30rpx"、"20rpx 30rpx"
  40. margin: {
  41. type: String,
  42. default: '0'
  43. }
  44. },
  45. computed: {
  46. lineStyle() {
  47. let style = {};
  48. style.backgroundColor = this.color;
  49. style.margin = this.margin;
  50. // 如果是水平线条,高度为1px,再通过transform缩小一半,就是0.5px了
  51. if(this.direction == 'row') {
  52. style.height = '1px';
  53. style.width = this.length;
  54. if(this.hairLine) style.transform = 'scaleY(0.5)';
  55. } else {
  56. // 如果是竖向线条,宽度为1px,再通过transform缩小一半,就是0.5px了
  57. style.width = '1px';
  58. style.height = this.length;
  59. if(this.hairLine) style.transform = 'scaleX(0.5)';
  60. }
  61. return style;
  62. }
  63. }
  64. }
  65. </script>
  66. <style scoped lang="scss">
  67. .u-line {
  68. vertical-align: middle;
  69. }
  70. </style>