drag.js

export default {
  install(Vue) {
  // v-dialogDrag: 弹窗拖拽
    Vue.directive('dragModal', (el, bindings, vnode) => {
      Vue.nextTick(() => {
        const { visible, destroyOnClose } = vnode.componentInstance
        // 防止未定义 destroyOnClose 关闭弹窗dom未被销毁,指令重复调用
        if (!visible) return
        const modal = el.getElementsByClassName('ant-modal')[0]
        const header = el.getElementsByClassName('ant-modal-header')[0]
        let left = 0
        let top = 0

        header.style.cursor = 'move'

        // 未定义 destroyOnClose 时,dom未被销毁,关闭弹窗再次打开弹窗会停留在上一次拖动位置
        if (!destroyOnClose) {
          left = modal.left || 0
          top = modal.top || 0
        }
        // top 初始值offsetTop
        top = top || modal.offsetTop
        header.onmousedown = e => {
          const startX = e.clientX
          const startY = e.clientY
          header.left = header.offsetLeft
          header.top = header.offsetTop
          el.onmousemove = event => {
            const endX = event.clientX
            const endY = event.clientY
            modal.left = header.left + (endX - startX) + left
            modal.top = header.top + (endY - startY) + top
            modal.style.left = modal.left + 'px'
            modal.style.top = modal.top + 'px'
          }
          el.onmouseup = event => {
            left = modal.left
            top = modal.top
            el.onmousemove = null
            el.onmouseup = null
            header.releaseCapture && header.releaseCapture()
          }
          header.setCapture && header.setCapture()
        }
      })
    })
  }
}

main.js引入

import '@/utils/drag'

使用vdragModal

   <a-modal
      v-dragModal
      :title="editModal.modalTitle + '详情'"
      :visible="editModal.visible"
      destroy-on-close
      :footer="null"
      :width="1200"
      @cancel="editModal.visible = false"
    >
      <div  style="height: 500px">
        1
      </div>
    </a-modal>

原文地址:https://blog.csdn.net/i_am_a_div/article/details/134685999

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_10943.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注