vue结合el-dialog自定义拖拽指令

本文详细介绍了如何在Vue项目中使用自定义指令实现el-dialog弹窗的拖拽功能,包括设置最小宽度和高度,以及处理鼠标操作和大小调整。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

el-dialog自定义拖拽指令(vue)

代码如下

import Vue from 'vue'
// v-dialogDrag: 弹窗拖拽
Vue.directive('dialogDrag', {
  bind(el, binding, vnode, oldVnode) {
    // 弹框可拉伸最小宽高
    const minWidth = 50
    const minHeight = 50

    const dialogHeaderEl = el.querySelector('.el-dialog__header')
    dialogHeaderEl.onselectstart = new Function('return false')
    const dragDom = el.querySelector('.el-dialog')
    dialogHeaderEl.style.cursor = 'move'

    // 获取原有属性 ie dom元素.currentStyle 火狐谷歌 window.getComputedStyle(dom元素, null);
    const sty = dragDom.currentStyle || window.getComputedStyle(dragDom, null)

    dialogHeaderEl.onmousedown = (e) => {
      // 鼠标按下,计算当前元素距离可视区的距离
      const disX = e.clientX - dialogHeaderEl.offsetLeft
      const disY = e.clientY - dialogHeaderEl.offsetTop

      // 获取到的值带px 正则匹配替换
      let styL, styT

      // 注意在ie中 第一次获取到的值为组件自带50% 移动之后赋值为px
      if (sty.left.includes('%')) {
        styL = +document.body.clientWidth * (+sty.left.replace(/\%/g, '') / 100)
        styT = +document.body.clientHeight * (+sty.top.replace(/\%/g, '') / 100)
      } else {
        styL = +sty.left.replace(/\px/g, '')
        styT = +sty.top.replace(/\px/g, '')
      }
      // console.log(styL);
      // console.log(styT);
      document.onmousemove = function(e) {
        // 通过事件委托,计算移动的距离
        const l = e.clientX - disX
        const t = e.clientY - disY
        // console.log('l>>>', l);
        // console.log('t>>>', t);
        // 移动当前元素
        dragDom.style.left = l + styL + 'px'
        dragDom.style.top = t + styT + 'px'

        // 将此时的位置传出去
        // binding.value({x:e.pageX,y:e.pageY})
      }

      document.onmouseup = function(e) {
        // dragDom.style.left = "${styL}px";
        // dragDom.style.top = "${styT}px";
        document.onmousemove = null
        document.onmouseup = null
      }
    }
    const resizeEl = document.createElement('div')
    const imgDialog = el.querySelector('.imgDialog')
    dragDom.appendChild(resizeEl)
    // 在弹窗右下角加上一个10-10px的控制块
    resizeEl.style.cursor = 'se-resize'
    resizeEl.style.position = 'absolute'
    resizeEl.style.height = '10px'
    resizeEl.style.width = '10px'
    resizeEl.style.right = '20px'
    resizeEl.style.bottom = '20px'
    // resizeEl.style.backgroundColor = 'red';
    // 鼠标拉伸弹窗
    resizeEl.onmousedown = (e) => {
      // 记录初始x位置
      const clientX = e.clientX
      // 鼠标按下,计算当前元素距离可视区的距离
      const disX = e.clientX - resizeEl.offsetLeft
      const disY = e.clientY - resizeEl.offsetTop

      document.onmousemove = function(e) {
        e.preventDefault() // 移动时禁用默认事件

        // 通过事件委托,计算移动的距离
        const x = e.clientX - disX + (e.clientX - clientX) // 这里 由于elementUI的dialog控制居中的,所以水平拉伸效果是双倍
        const y = e.clientY - disY
        // 比较是否小于最小宽高
        // imgDialog.style.width = "${x}px";
        // imgDialog.style.height = "${y}px";
        imgDialog.style.width = x > minWidth ? x + 'px' : minWidth + 'px'
        imgDialog.style.height = y > minHeight ? y + 'px' : minHeight + 'px'
      }
      // 拉伸结束
      document.onmouseup = function(e) {
        document.onmousemove = null
        document.onmouseup = null
      }
    }
  }
})

使用方式

  <el-dialog
    v-dialogDrag
    title="人员列表"
    :visible.sync="isShow"
    width="70%"
    :before-close="handleClose"
    append-to-body
    :close-on-click-modal="false"
  >
     </el-dialog>
### 解决方案 为了防止 `el-dialog` 组件在自定义拖拽过程中超出页面边界,可以采用监听鼠标移动事件并计算对话框位置的方法来动态调整其坐标。具体实现如下: #### HTML 部分 ```html <template> <el-dialog :visible.sync="dialogVisible" @mousedown.native="handleMouseDown"> <!-- 对话框内容 --> </el-dialog> </template> ``` #### JavaScript 方法部分 通过 Vue 实例中的方法绑定鼠标的按下、移动以及释放事件。 当检测到用户点击了 `.el-dialog__header` 或者其他指定区域时触发拖动逻辑,在此期间不断更新 dialog 的 top 和 left 属性值以保持在其可视区域内[^1]。 ```javascript export default { data() { return { dialogVisible: false, startX: 0, startY: 0, startLeft: 0, startTop: 0, isDragging: false, }; }, methods: { handleMouseDown(event) { const target = event.target; // 判断是否是在头部触发的事件 if (target.closest('.el-dialog__header')) { this.startX = event.clientX; this.startY = event.clientY; let rect = document.querySelector('.el-dialog').getBoundingClientRect(); this.startLeft = rect.left; this.startTop = rect.top; this.isDragging = true; window.addEventListener('mousemove', this.handleMouseMove); window.addEventListener('mouseup', this.handleMouseUp); } }, handleMouseMove(event) { if (!this.isDragging) return; let newX = this.startLeft + (event.clientX - this.startX); let newY = this.startTop + (event.clientY - this.startY); // 获取窗口尺寸 var winWidth = window.innerWidth || document.documentElement.clientWidth || document.body.offsetWidth; var winHeight = window.innerHeight || document.documentElement.clientHeight || document.body.offsetHeight; // 计算最大最小范围 var maxRight = winWidth - parseInt(getComputedStyle(document.querySelector('.el-dialog')).width); var maxBottom = winHeight - parseInt(getComputedStyle(document.querySelector('.el-dialog')).height); // 边界控制 newX = Math.max(0, Math.min(newX, maxRight)); newY = Math.max(0, Math.min(newY, maxBottom)); document.querySelector('.el-dialog').style.transform = `translate(${newX}px, ${newY}px)`; }, handleMouseUp() { this.isDragging = false; window.removeEventListener('mousemove', this.handleMouseMove); window.removeEventListener('mouseup', this.handleMouseUp); } } } ``` 上述代码实现了对 `el-dialog` 拖拽边界的限制功能,确保即使用户尝试将对话框移至屏幕边缘之外也不会发生越界现象。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值