ResizableAside.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <template>
  2. <el-aside :style="{ width: asideWidth + 'px' }" class="resizable-aside">
  3. <slot></slot>
  4. <!-- 拖拽条 -->
  5. <div class="resize-handle" @mousedown="startResize" :class="{ 'resizing': isResizing }" title="拖拽调整宽度">
  6. <div class="resize-line"></div>
  7. </div>
  8. </el-aside>
  9. </template>
  10. <script>
  11. export default {
  12. name: "ResizableAside",
  13. props: {
  14. // 默认宽度
  15. defaultWidth: {
  16. type: Number,
  17. default: 260
  18. },
  19. // 最小宽度
  20. minWidth: {
  21. type: Number,
  22. default: 200
  23. },
  24. // 最大宽度
  25. maxWidth: {
  26. type: Number,
  27. default: 500
  28. },
  29. // localStorage存储key
  30. storageKey: {
  31. type: String,
  32. required: true
  33. }
  34. },
  35. data() {
  36. return {
  37. asideWidth: this.defaultWidth,
  38. isResizing: false,
  39. startX: 0,
  40. startWidth: 0
  41. }
  42. },
  43. mounted() {
  44. // 从localStorage恢复宽度设置
  45. const savedWidth = localStorage.getItem(this.storageKey);
  46. if (savedWidth) {
  47. this.asideWidth = parseInt(savedWidth);
  48. }
  49. // 添加全局事件监听
  50. document.addEventListener('mousemove', this.handleResize);
  51. document.addEventListener('mouseup', this.stopResize);
  52. },
  53. beforeDestroy() {
  54. // 清理事件监听
  55. document.removeEventListener('mousemove', this.handleResize);
  56. document.removeEventListener('mouseup', this.stopResize);
  57. },
  58. methods: {
  59. // 拖拽相关方法
  60. startResize(e) {
  61. this.isResizing = true;
  62. this.startX = e.clientX;
  63. this.startWidth = this.asideWidth;
  64. document.body.style.cursor = 'col-resize';
  65. document.body.style.userSelect = 'none';
  66. e.preventDefault();
  67. },
  68. handleResize(e) {
  69. if (!this.isResizing) return;
  70. const deltaX = e.clientX - this.startX;
  71. let newWidth = this.startWidth + deltaX;
  72. // 限制宽度范围
  73. newWidth = Math.max(this.minWidth, Math.min(this.maxWidth, newWidth));
  74. this.asideWidth = newWidth;
  75. },
  76. stopResize() {
  77. if (!this.isResizing) return;
  78. this.isResizing = false;
  79. document.body.style.cursor = '';
  80. document.body.style.userSelect = '';
  81. // 保存宽度到localStorage
  82. localStorage.setItem(this.storageKey, this.asideWidth.toString());
  83. }
  84. }
  85. }
  86. </script>
  87. <style lang="scss" scoped>
  88. .resizable-aside {
  89. display: flex;
  90. flex-direction: column;
  91. border-right: 1px solid rgba(0, 0, 0, 0.08);
  92. box-shadow: 2px 0 8px rgba(0, 0, 0, 0.05);
  93. position: relative;
  94. // 拖拽条样式
  95. .resize-handle {
  96. position: absolute;
  97. top: 0;
  98. right: -3px;
  99. width: 6px;
  100. height: 100%;
  101. cursor: col-resize;
  102. z-index: 10;
  103. display: flex;
  104. align-items: center;
  105. justify-content: center;
  106. transition: background-color 0.2s ease;
  107. .resize-line {
  108. width: 2px;
  109. height: 100%;
  110. background-color: var(--im-background-active-dark);
  111. border-radius: 1px;
  112. transition: all 0.2s ease;
  113. }
  114. &:hover .resize-line,
  115. &.resizing .resize-line {
  116. width: 3px;
  117. }
  118. }
  119. }
  120. </style>