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

相关推荐
我的心巴2 小时前
vue-print-nb 打印相关问题
前端·vue.js·elementui
Amy_cx13 小时前
在表单输入框按回车页面刷新的问题
前端·elementui
狼性书生14 小时前
uniapp实现的简约美观的星级评分组件
前端·uni-app·vue·组件
xiaogg36781 天前
网站首页菜单顶部下拉上下布局以及可关闭标签页实现vue+elementui
javascript·vue.js·elementui
宇宙的最后一粒尘埃1 天前
Typeerror: cannot read properties of undefined (reading ‘XXX‘)
vue
白云~️1 天前
table表格合并,循环渲染样式
javascript·vue.js·elementui
清幽竹客2 天前
vue-18(使用 Vuex 插件实现高级功能)
前端·vue.js·前端框架·vue
牧码岛2 天前
Web前端之隐藏元素方式的区别、Vue循环标签的时候在同一标签上隐藏元素的解决办法、hidden、display、visibility
前端·css·vue·html·web·web前端
夏之小星星2 天前
解决el-select选择框右侧下拉箭头遮挡文字问题
前端·vue.js·elementui
xiaogg36782 天前
网站首页菜单两种布局vue+elementui顶部和左侧栏导航
前端·vue.js·elementui