当前位置:   article > 正文

Vue3分割线(Divider)_vue 分割线

vue 分割线

可自定义设置以下属性:

  • 是否为虚线(dashed),类型:boolean,默认 false

  • 分割线标题的位置(orientation),类型:string,默认 'center',可选 left | center | right

  • 标题和最近 left/right 边框之间的距离,去除了分割线,同时 orientation 必须为 left 或 right(disabled),类型:string | number,默认 ''

  • 分割线宽度(borderWidth),类型:number,默认 1

  • 水平或者垂直类型(type),类型:'horizontal'|'vertical',默认 'horizontal'

效果如下图:在线预览

① 创建分割线组件Divider.vue:

  1. <script setup lang="ts">
  2. import { computed, useSlots } from 'vue'
  3. interface Props {
  4. dashed?: boolean // 是否为虚线
  5. orientation?: 'left'|'center'|'right' // 分割线标题的位置
  6. orientationMargin?: string|number // 标题和最近 left/right 边框之间的距离,去除了分割线,同时 orientation 必须为 left 或 right
  7. borderWidth?: number // 分割线宽度
  8. type?: 'horizontal'|'vertical' // 水平或者垂直类型
  9. }
  10. const props = withDefaults(defineProps<Props>(), {
  11. dashed: false,
  12. orientation: 'center', // 可选 left center right
  13. orientationMargin: '',
  14. borderWidth: 1,
  15. type: 'horizontal'
  16. })
  17. const margin = computed(() => {
  18. if (props.orientationMargin !== '') {
  19. if (typeof props.orientationMargin === 'number') {
  20. return props.orientationMargin + 'px'
  21. } else {
  22. return props.orientationMargin
  23. }
  24. }
  25. })
  26. const slots = useSlots()
  27. const showText = computed(() => {
  28. const defaultSlots = slots.default?.()
  29. if (defaultSlots) {
  30. return Boolean(defaultSlots[0].children !== 'v-if' && defaultSlots[0].children?.length)
  31. }
  32. return false
  33. })
  34. </script>
  35. <template>
  36. <div
  37. v-if="type==='horizontal'"
  38. :class="[`m-divider-horizontal ${orientation}`,
  39. {
  40. dashed: dashed,
  41. margin24: !showText,
  42. marginLeft: orientationMargin !== '' && orientation === 'left',
  43. marginRight: orientationMargin !== '' && orientation === 'right'
  44. }
  45. ]"
  46. :style="`--border-width: ${borderWidth}px;`">
  47. <span class="u-text" v-if="orientation === 'left'" :style="`margin-left: ${margin};`" v-show="showText">
  48. <slot></slot>
  49. </span>
  50. <span class="u-text" v-else-if="orientation === 'right'" :style="`margin-right: ${margin};`" v-show="showText">
  51. <slot></slot>
  52. </span>
  53. <span class="u-text" v-else v-show="showText">
  54. <slot></slot>
  55. </span>
  56. </div>
  57. <div v-else class="m-divider-vertical"></div>
  58. </template>
  59. <style lang="less" scoped>
  60. .m-divider-horizontal {
  61. display: flex;
  62. align-items: center;
  63. margin: 16px 0;
  64. width: 100%;
  65. min-width: 100%;
  66. &::before, &::after {
  67. position: relative;
  68. width: 50%;
  69. border-top-width: var(--border-width);
  70. border-top-style: solid;
  71. border-top-color: rgba(5, 5, 5, .06);
  72. transform: translateY(50%);
  73. content: '';
  74. }
  75. .u-text {
  76. display: inline-block;
  77. font-size: 16px;
  78. color: rgba(0, 0, 0, .88);
  79. font-weight: 500;
  80. line-height: 1.5714285714285714;
  81. white-space: nowrap;
  82. text-align: center;
  83. padding: 0 16px;
  84. }
  85. }
  86. .m-divider-vertical {
  87. position: relative;
  88. top: -.06em;
  89. display: inline-block;
  90. height: .9em;
  91. margin: 0 8px;
  92. vertical-align: middle;
  93. border-top: 0;
  94. border-inline-start: 1px solid rgba(5, 5, 5, .06);
  95. }
  96. .dashed {
  97. &::before {
  98. border-top-style: dashed;
  99. }
  100. &::after {
  101. border-top-style: dashed;
  102. }
  103. }
  104. .left {
  105. &::before {
  106. width: 5%;
  107. }
  108. &::after {
  109. width: 95%;
  110. }
  111. }
  112. .right {
  113. &::before {
  114. width: 95%;
  115. }
  116. &::after {
  117. width: 5%;
  118. }
  119. }
  120. .margin24 {
  121. margin: 24px 0;
  122. }
  123. .marginLeft {
  124. &::before {
  125. width: 0;
  126. }
  127. &::after {
  128. width: 100%;
  129. }
  130. }
  131. .marginRight {
  132. &::before {
  133. width: 100%;
  134. }
  135. &::after {
  136. width: 0;
  137. }
  138. }
  139. </style>

②在要使用的页面引入:

  1. <script setup lang="ts">
  2. import Divider from './Divider.vue'
  3. </script>
  4. <template>
  5. <div>
  6. <h1>Divider 分割线</h1>
  7. <h2 class="mt30 mb10">基本使用</h2>
  8. <Divider>Center Text</Divider>
  9. <h2 class="mt30 mb10">中间无文字</h2>
  10. <Divider />
  11. <h2 class="mt30 mb10">垂直分割线</h2>
  12. <div>
  13. Text
  14. <Divider type="vertical" />
  15. <a href="#">Link</a>
  16. <Divider type="vertical" />
  17. <a href="#">Link</a>
  18. </div>
  19. <h2 class="mt30 mb10">指定文字位置</h2>
  20. <Divider orientation="left">Left Text</Divider>
  21. <Divider orientation="right">Right Text</Divider>
  22. <h2 class="mt30 mb10">自定义文字文字</h2>
  23. <h3 class="mb10">文字居左(右)并距左(右)边 120px</h3>
  24. <Divider orientation="left" :orientation-margin="120">Left Text</Divider>
  25. <Divider orientation="right" :orientation-margin="120">Right Text</Divider>
  26. <h2 class="mt30 mb10">使用虚线</h2>
  27. <Divider dashed>Center Text</Divider>
  28. <h2 class="mt30 mb10">自定义线宽</h2>
  29. <h3 class="mb10">线宽 3px</h3>
  30. <Divider :borderWidth="3">Center Text</Divider>
  31. </div>
  32. </template>
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/113621
推荐阅读
相关标签
  

闽ICP备14008679号