vue+element+dialog实现弹窗可拖动

新建dialog.js文件,并在main.js中引用(全局),也可以在使用的地方局部引用
dialog.js代码内容

import Vue from 'vue'

// v-dialogDrag: 弹窗拖拽
Vue.directive('dialogDrag', {
  bind(el, binding, vnode, oldVnode) {
    const dialogHeaderEl = el.querySelector('.el-dialog__header')
    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, '')
      }

      document.onmousemove = function(e) {
        // 通过事件委托,计算移动的距离
        const l = e.clientX - disX
        const t = e.clientY - disY

        // 移动当前元素
        dragDom.style.left = `${l + styL}px`
        dragDom.style.top = `${t + styT}px`

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

      document.onmouseup = function(e) {
        document.onmousemove = null
        document.onmouseup = null
      }
    }
  }
})

在使用的el-dialog中加入v-dialogDrag

<el-dialog v-dialogDrag :visible.sync="imgShow" width="600px" top="30px" :modal="false" :show-close="false"  :close-on-click-modal="false">
	<div slot="title"></div>
	<div class="order_box"></div>
</el-dialog>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue Element UI 提供了 el-dialog 组件,但是默认情况下不支持拖拽,需要自己进行实现。下面是实现 el-dialog 拖拽的步骤: 1. 在 el-dialog 的 title 区域添加一个拖拽的区域,可以使用 CSS 的 cursor 属性来设置鼠标样式。 ```html <template> <el-dialog :visible.sync="dialogVisible" title="可拖拽的对话框"> <div class="dialog-header" @mousedown="startDrag"> <span>可拖拽的对话框</span> </div> <div> <!-- 对话框内容 --> </div> </el-dialog> </template> <style> .dialog-header { cursor: move; } </style> ``` 2. 在 startDrag 方法中记录鼠标按下的位置和对话框的位置。 ```js export default { data() { return { dialogVisible: false, dragging: false, // 是否正在拖拽 dragX: 0, // 鼠标按下时的位置 dragY: 0, dialogLeft: 0, // 对话框的位置 dialogTop: 0, }; }, methods: { startDrag(e) { this.dragging = true; this.dragX = e.clientX; this.dragY = e.clientY; this.dialogLeft = this.$refs.dialog.getBoundingClientRect().left; this.dialogTop = this.$refs.dialog.getBoundingClientRect().top; }, }, }; ``` 3. 在监听 document 的 mousemove 和 mouseup 事件,在 mousemove 事件中计算出对话框应该移动到的位置,在 mouseup 事件中停止拖拽。 ```js methods: { startDrag(e) { // ... document.addEventListener('mousemove', this.drag); document.addEventListener('mouseup', this.stopDrag); }, drag(e) { if (this.dragging) { const offsetX = e.clientX - this.dragX; const offsetY = e.clientY - this.dragY; this.$refs.dialog.style.left = `${this.dialogLeft + offsetX}px`; this.$refs.dialog.style.top = `${this.dialogTop + offsetY}px`; } }, stopDrag() { this.dragging = false; document.removeEventListener('mousemove', this.drag); document.removeEventListener('mouseup', this.stopDrag); }, } ``` 完整代码如下: ```html <template> <el-dialog :visible.sync="dialogVisible" title="可拖拽的对话框" ref="dialog"> <div class="dialog-header" @mousedown="startDrag"> <span>可拖拽的对话框</span> </div> <div> <!-- 对话框内容 --> </div> </el-dialog> </template> <style> .dialog-header { cursor: move; } </style> <script> export default { data() { return { dialogVisible: false, dragging: false, // 是否正在拖拽 dragX: 0, // 鼠标按下时的位置 dragY: 0, dialogLeft: 0, // 对话框的位置 dialogTop: 0, }; }, methods: { startDrag(e) { this.dragging = true; this.dragX = e.clientX; this.dragY = e.clientY; this.dialogLeft = this.$refs.dialog.getBoundingClientRect().left; this.dialogTop = this.$refs.dialog.getBoundingClientRect().top; document.addEventListener('mousemove', this.drag); document.addEventListener('mouseup', this.stopDrag); }, drag(e) { if (this.dragging) { const offsetX = e.clientX - this.dragX; const offsetY = e.clientY - this.dragY; this.$refs.dialog.style.left = `${this.dialogLeft + offsetX}px`; this.$refs.dialog.style.top = `${this.dialogTop + offsetY}px`; } }, stopDrag() { this.dragging = false; document.removeEventListener('mousemove', this.drag); document.removeEventListener('mouseup', this.stopDrag); }, }, }; </script> ``` 这样就实现el-dialog拖拽功能。注意,由于拖拽是直接修改 DOM 元素的样式,如果使用了动画或者过渡效果,可能会有一些问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值