当前位置:   article > 正文

前端——封装vue通用拖拽滑动分隔面板组件(Split)_splitpanes

splitpanes

目录

 

1.通过splitpanes封装


1.通过splitpanes封装

参考文章:封装vue通用拖拽滑动分隔面板组件(Split) - 掘金

使用js库:splitpanes  https://antoniandre.github.io/splitpanes/https://antoniandre.github.io/splitpanes/

使用方法:

npm i splitpanes # For Vue 3

完整代码:

  1. <template>
  2. <div ref="splitPane" class="split-pane" :class="props.direction" :style="{ flexDirection: direction }">
  3. <div class="pane pane-one" :style="lengthType + ':' + paneLengthValue">
  4. <slot name="one"></slot>
  5. </div>
  6. <div class="pane-trigger" :style="lengthType + ':' + triggerLengthValue" @mousedown="handleMouseDown"></div>
  7. <div class="pane pane-two">
  8. <slot name="two"></slot>
  9. </div>
  10. </div>
  11. </template>
  12. <script setup>
  13. import { ref, defineProps, computed } from 'vue'
  14. /* 属性 */
  15. const splitPane = ref()
  16. let paneLengthPercent = ref(50) // 区域1宽度 (%)
  17. let triggerLength = ref(10) // 滑动器宽度 (px)
  18. let triggerLeftOffset = ref(0) // 鼠标距滑动器左(顶)侧偏移量
  19. const props = defineProps({
  20. direction: {
  21. type: String,
  22. default: 'column'
  23. },
  24. //滑动限制-最小
  25. min: {
  26. type: Number,
  27. default: 10
  28. },
  29. //滑动限制-最大
  30. max: {
  31. type: Number,
  32. default: 90
  33. },
  34. paneLengthPercent: {
  35. type: Number,
  36. default: 50
  37. },
  38. triggerLength: {
  39. type: Number,
  40. default: 10
  41. }
  42. })
  43. const lengthType = computed(() => {
  44. return props.direction === 'row' ? 'width' : 'height'
  45. })
  46. const paneLengthValue = computed(() => {
  47. return `calc(${paneLengthPercent.value}% - ${triggerLength.value / 2 + 'px'})`
  48. })
  49. const triggerLengthValue = computed(() => {
  50. return triggerLength.value + 'px'
  51. })
  52. /* 方法 */
  53. // 按下滑动器
  54. const handleMouseDown = (e) => {
  55. document.addEventListener('mousemove', handleMouseMove)
  56. document.addEventListener('mouseup', handleMouseUp)
  57. if (props.direction === 'row') {
  58. triggerLeftOffset.value = e.pageX - e.srcElement.getBoundingClientRect().left
  59. } else {
  60. triggerLeftOffset.value = e.pageY - e.srcElement.getBoundingClientRect().top
  61. }
  62. }
  63. // 按下滑动器后移动鼠标
  64. const handleMouseMove = (e) => {
  65. const clientRect = splitPane.value.getBoundingClientRect()
  66. paneLengthPercent.value = 0
  67. //方向判断
  68. if (props.direction === 'row') {
  69. const offset = e.pageX - clientRect.left - triggerLeftOffset.value + triggerLength.value / 2
  70. paneLengthPercent.value = (offset / clientRect.width) * 100
  71. } else {
  72. const offset = e.pageY - clientRect.top - triggerLeftOffset.value + triggerLength.value / 2
  73. paneLengthPercent.value = (offset / clientRect.height) * 100
  74. }
  75. //滑动限制判断
  76. if (paneLengthPercent.value < props.min) {
  77. paneLengthPercent.value = props.min
  78. }
  79. if (paneLengthPercent.value > props.max) {
  80. paneLengthPercent.value = props.max
  81. }
  82. }
  83. // 松开滑动器
  84. const handleMouseUp = () => {
  85. document.removeEventListener('mousemove', handleMouseMove)
  86. }
  87. </script>
  88. <style scoped lang="scss">
  89. .split-pane {
  90. background: palegreen;
  91. height: 100%;
  92. display: flex;
  93. &.row {
  94. .pane {
  95. height: 100%;
  96. }
  97. .pane-trigger {
  98. height: 100%;
  99. cursor: col-resize; //鼠标样式
  100. }
  101. }
  102. &.column {
  103. .pane {
  104. width: 100%;
  105. }
  106. .pane-trigger {
  107. width: 100%;
  108. cursor: row-resize; //鼠标样式
  109. }
  110. }
  111. .pane-one {
  112. width: 50%;
  113. background: palevioletred;
  114. }
  115. .pane-trigger {
  116. background: palegoldenrod;
  117. user-select: none;
  118. }
  119. .pane-two {
  120. flex: 1;
  121. background: turquoise;
  122. }
  123. }
  124. </style>

2.通过vue-splitpane插件

参考文章:Vue中 引入使用 vue-splitpane 实现窗格的拆分、调节_明天也要努力的博客-CSDN博客_vue-splitpane

3.通过Vue-Split-Panel插件

参考文章:vue-split-panel - npm 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/76848
推荐阅读
相关标签
  

闽ICP备14008679号