ElementUI框架el-aside组件实现拖拽改变宽度
    		       		warning:
    		            这篇文章距离上次修改已过433天,其中的内容可能已经有所变动。
    		        
        		                
                在ElementUI中,可以使用el-aside组件结合Vue的自定义指令来实现拖拽改变宽度的功能。以下是一个简单的示例:
- 首先,创建一个Vue自定义指令来处理拖拽逻辑:
 
Vue.directive('resizable', {
  bind(el, binding, vnode) {
    let startX;
    const dom = el; // 当前aside DOM元素
    const minWidth = 200; // 最小宽度
    const maxWidth = 400; // 最大宽度
 
    dom.onmousedown = (e) => {
      startX = e.clientX;
      dom.left = dom.offsetWidth;
 
      document.onmousemove = (e) => {
        let endX = e.clientX;
        let moveLen = dom.left + (endX - startX); // 移动的距离
        if (moveLen < minWidth) moveLen = minWidth;
        if (moveLen > maxWidth) moveLen = maxWidth;
 
        dom.style.width = moveLen + 'px';
      };
 
      document.onmouseup = () => {
        document.onmousemove = null;
        document.onmouseup = null;
      };
    };
  }
});- 在你的组件中使用
el-aside和自定义指令: 
<template>
  <el-container style="height: 100vh;">
    <el-aside v-resizable style="background-color: #304156; width: 200px;">
      <!-- 拖拽区域 -->
      <div class="resize-handler" style="position: absolute; top: 0; right: -5px; width: 10px; height: 100%; cursor: col-resize;"></div>
    </el-aside>
    <el-main>
      <!-- 主要内容 -->
    </el-main>
  </el-container>
</template>
 
<style>
.resize-handler {
  position: absolute;
  top: 0;
  right: -5px;
  width: 10px;
  height: 100%;
  cursor: col-resize;
}
</style>确保在Vue实例中引入了ElementUI并正确使用了el-container和el-aside组件。
这个示例中,.resize-handler是一个占位符,它会显示在侧边栏的右侧,用户可以点击并拖动它来改变宽度。自定义指令v-resizable绑定在el-aside上,使其能够接收鼠标事件并实现拖拽调整宽度的功能。
评论已关闭