index.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <template>
  2. <div>
  3. <div class="layout">
  4. <div class="layout-left" :style="{ width: leftWidth }">
  5. <slot name="left" />
  6. </div>
  7. <div ref="drag" class="layout-drag" :style="{ left: leftWidth }" @mousedown="handleDown">
  8. <span class="line" />
  9. <span class="line" />
  10. <span class="line" />
  11. <span class="line" />
  12. <span class="line" />
  13. <span class="line" />
  14. </div>
  15. <div class="layout-right" :style="{ width: rightWidth }">
  16. <slot name="right" />
  17. </div>
  18. </div>
  19. </div>
  20. </template>
  21. <script>
  22. export default {
  23. name: 'DragColumn',
  24. data() {
  25. return {
  26. disX: 0,
  27. layoutWidth: 0,
  28. dragWidth: 0,
  29. leftWidth: 0,
  30. rightWidth: 0
  31. }
  32. },
  33. mounted() {
  34. this.layoutWidth = document.querySelector('.layout').clientWidth
  35. this.dragWidth = document.querySelector('.layout-drag').clientWidth
  36. this.leftWidth = (0.255 * this.layoutWidth) + 'px'
  37. this.rightWidth = (0.735 * this.layoutWidth) + 'px'
  38. window.onresize = () => {
  39. this.layoutWidth = document.querySelector('.layout').clientWidth
  40. this.dragWidth = document.querySelector('.layout-drag').clientWidth
  41. this.leftWidth = (0.255 * this.layoutWidth) + 'px'
  42. this.rightWidth = (0.735 * this.layoutWidth) + 'px'
  43. }
  44. },
  45. methods: {
  46. handleDown(e) {
  47. const ev = e || event
  48. const pos = this.getPos(ev)
  49. const drag = this.$refs.drag
  50. this.disX = pos.x - drag.offsetLeft
  51. document.onmousemove = moveEvent => {
  52. const oEvent = moveEvent || event
  53. const movePos = this.getPos(oEvent)
  54. const t = movePos.x - this.disX
  55. const surplusWidth = this.layoutWidth - this.dragWidth - t
  56. const leftWidth = this.layoutWidth - this.dragWidth - surplusWidth
  57. if (leftWidth < 260) {
  58. return false
  59. } else if (surplusWidth < 260) {
  60. return false
  61. }
  62. this.leftWidth = leftWidth + 'px'
  63. this.rightWidth = surplusWidth + 'px'
  64. }
  65. document.onmouseup = () => {
  66. document.onmousemove = null
  67. document.onmouseup = null
  68. }
  69. return false
  70. },
  71. getPos(ev) {
  72. const scrollTop = document.documentElement.scrollTop || document.body.scrollTop
  73. const scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft
  74. return { x: ev.clientX + scrollLeft, y: ev.clientY + scrollTop }
  75. }
  76. }
  77. }
  78. </script>
  79. <style lang="scss" scoped>
  80. .layout {
  81. height: calc(100vh - 113px);
  82. position: relative;
  83. .layout-left {
  84. width: 25.5%;
  85. height: 100%;
  86. position: absolute;
  87. top: 0;
  88. left: 0;
  89. bottom: 0;
  90. }
  91. .layout-drag {
  92. width: 1%;
  93. position: absolute;
  94. height: 100%;
  95. top: 0;
  96. bottom: 0;
  97. left: 25.5%;
  98. display: flex;
  99. justify-content: center;
  100. align-items: center;
  101. flex-direction: column;
  102. cursor: col-resize;
  103. &:active{
  104. .line{
  105. transition: all 0.3s ease-in-out;
  106. background: #0f82ff;
  107. width: 3.5px;
  108. height: 3.5px;
  109. border-radius: 50%;
  110. }
  111. }
  112. .line {
  113. display: inline-block;
  114. width: 2px;
  115. height: 2px;
  116. background: #898989;
  117. margin-bottom: 5px;
  118. border-radius: 50%;
  119. }
  120. }
  121. .layout-right {
  122. width: 73.5%;
  123. height: 100%;
  124. position: absolute;
  125. top: 0;
  126. right: 0;
  127. bottom: 0;
  128. }
  129. }
  130. .focus{
  131. .line{
  132. color: red;
  133. }
  134. }
  135. </style>