elementUI可拖拉宽度抽屉

1,需求:

在elementUI的抽屉基础上,添加可拖动侧边栏宽度的功能,实现效果如下:

2,在原组件上添加自定义命令

html 复制代码
    <el-drawer v-drawerDrag="'left'" :visible.sync="drawerVisible" direction="ltr">
      <div id="showId" style="padding: 1rem;font-size: 12px;overflow-x: hidden;" v-html="form.introduce"></div>
    </el-drawer>

v-drawerDrag 属性是我们在原组件新加的命令,传入left或者right,需要与 direction 的let和rtl对应,

3,drawer-drag.js

xml 复制代码
export default {
  bind(el, binding, vnode, oldVnode) {
  	// 默认抽屉宽度,当宽度小于此值不在压缩
    const minWidth = 400
    const dragDom = el.querySelector('.el-drawer')
    dragDom.style.overflow = 'auto'
    const resizeElL = document.createElement('div')
    const img = new Image(24, 38)
    img.src = require('@/assets/images/stretch.png')
    dragDom.appendChild(img)
    dragDom.appendChild(resizeElL)
    resizeElL.style.cursor = 'w-resize'
    resizeElL.style.position = 'absolute'
    resizeElL.style.height = '100%'
    resizeElL.style.width = '10px'
    resizeElL.style.top = '0px'
    img.style.position = 'absolute'
    img.style.top = '50%'
    // console.log('binding', binding.value)
    // 区分右侧侧边栏和左侧侧边栏
    if (binding.value === 'right') {
      resizeElL.style.left = '0px'
      img.style.left = '-12px'
      resizeElL.onmousedown = (e) => {
        const elW = dragDom.clientWidth
        const EloffsetLeft = dragDom.offsetLeft
        const clientX = e.clientX
        document.onmousemove = function(e) {
          e.preventDefault()
          if (clientX > EloffsetLeft && clientX < EloffsetLeft + 10) {
            // 往右拖拽
            if (e.clientX > clientX) {
              // console.log('向右-----------------------------')
              if (dragDom.clientWidth >= minWidth) {
                dragDom.style.width = elW - (e.clientX - clientX) + 'px'
              }
            }
            if (e.clientX < clientX) {
              // console.log('向左-----------------------------')
              dragDom.style.width = elW + (clientX - e.clientX) + 'px'
            }
          }
        }
        // 拉伸结束
        document.onmouseup = function(e) {
          document.onmousemove = null
          document.onmouseup = null
        }
      }
    } else {
      resizeElL.style.right = '0px'
      img.style.right = '-12px'
      resizeElL.onmousedown = (e) => {
        const elW = dragDom.clientWidth
        const EloffsetLeft = dragDom.offsetLeft + dragDom.offsetWidth
        const clientX = e.clientX
        document.onmousemove = function(e) {
          e.preventDefault()
          if (clientX < EloffsetLeft && clientX > EloffsetLeft - 10) {
            if (e.clientX > clientX) {
              // console.log('向右-----------------------------')
              dragDom.style.width = elW + (e.clientX - clientX) + 'px'
            }
            if (e.clientX < clientX) {
              // console.log('向左-----------------------------')
              if (dragDom.clientWidth >= minWidth) {
                dragDom.style.width = elW - (clientX - e.clientX) + 'px'
              }
            }
          }
        }
        // 拉伸结束
        document.onmouseup = function(e) {
          document.onmousemove = null
          document.onmouseup = null
        }
      }
    }
  }
}

图标自取 stretch.png

相关推荐
Pro_er5 小时前
Vue3 性能优化十大技巧:打造高性能应用的终极指南
vue·前端开发
Pro_er19 小时前
Vue3响应式编程三剑客:计算属性、方法与侦听器深度实战指南
vue·前端开发
fendouweiqian1 天前
element-plus 根据条件显示多选框
elementui
鑫~阳1 天前
Vue2是如何利用Object.defineProperty实现数据的双向绑定?
前端·vue.js·vue
寰宇软件1 天前
PHP房屋出租出售高效预约系统小程序源码
前端·小程序·uni-app·vue·php
爱学习的小王!2 天前
nvm安装、管理node多版本以及配置环境变量【保姆级教程】
经验分享·笔记·node.js·vue
Jay丶萧邦2 天前
el-select:有关多选,options选项值不包含绑定值的回显问题
javascript·vue.js·elementui
我爱学习_zwj2 天前
后台管理系统-月卡管理
javascript·vue.js·elementui
是你的小熊啊2 天前
解决elementUi el-select 响应式不生效的问题
前端·vue.js·elementui
程序员小续2 天前
Excel 表格和 Node.js 实现数据转换工具
前端·javascript·react.js·前端框架·vue·excel·reactjs