elementui el-dialog 拖拽封装 兼容ie以及添加可视区域拖拽

公共组件里添加elDialog文件夹
分别添加三个文件
Dlalog.vue

<template>
  <div class="ElDialog">
    <Dialog
      v-bind="$attrs"
      v-on="$listeners"
      v-dialogDrag="isDrag"
    >
      <slot
        v-for="(_, name) in $slots"
        :slot="name"
        :name="name"
      />
      <template
        v-for="(_, name) in $scopedSlots"
        :slot="name"
        slot-scope="slotData"
      >
        <slot
          :name="name"
          v-bind="slotData"
        />
      </template>
    </Dialog>
  </div>
</template>
<script>
import './dragDialog'
import { Dialog } from 'element-ui'
export default {
  name: 'ElDialog',
  /**
   * 弹框组件
   * */
  props: {
    // 是否需要拖拽
    isDrag: {
      type: Boolean,
      default: true
    }
  },
  components: { Dialog }
}
</script>

drag.js

import Vue from 'vue'
// 拖动时取消文字选中
var clearSlct = 'getSelection' in window ? function () {
  window.getSelection().removeAllRanges()
} : function () {
  document.selection.empty()
}
// v-dialogDrag: 弹窗拖拽
Vue.directive('dialogDrag', {
  bind (el, binding, vnode, oldVnode) {
    const dialogHeaderEl = el.querySelector('.el-dialog__header')
    const dragDom = el.querySelector('.el-dialog')
    if (!binding.value || !dialogHeaderEl || !dragDom) return
    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
      if (sty.left.includes('%')) {
        // eslint-disable-next-line no-useless-escape
        styL = +document.body.clientWidth * (+sty.left.replace(/\%/g, '') / 100)
        // eslint-disable-next-line no-useless-escape
        styT = +document.body.clientHeight * (+sty.top.replace(/\%/g, '') / 100)
      } else if (sty.left.includes('auto')) {
        styL = 0
        styT = 0
      } else {
        // eslint-disable-next-line no-useless-escape
        styL = +sty.left.replace(/\px/g, '')
        // eslint-disable-next-line no-useless-escape
        styT = +sty.top.replace(/\px/g, '')
      }
      document.onmousemove = function (e) {
        // 通过事件委托,计算移动的距离
        let l = e.clientX - disX
        const t = e.clientY - disY
        const leftMin = document.body.clientWidth / 2 - dragDom.offsetWidth / 2
        const percent = parseInt(dragDom.style.marginTop.match(/\d+/g))
        const topMin = document.body.clientHeight * (percent / 100)
        const topMax = document.body.clientHeight * (100 - percent) / 100 - dragDom.offsetHeight
        let left = 0
        let top = 0
        // 限制拖动区域
        if (l + styL <= -leftMin) {
          left = -leftMin
        } else if (l + styL >= leftMin) {
          left = leftMin
        } else {
          left = l + styL
        }
        if (t + styT <= -topMin) {
          top = -topMin
        } else if (t + styT >= topMax) {
          top = topMax
        } else {
          top = t + styT
        }
        // left = l + styL >= leftMin ? leftMin : l + styL
        dragDom.style.left = `${left}px`
        dragDom.style.top = `${top}px`
        clearSlct()
      }

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

// v-dialogDragWidth: 弹窗宽度拖大 拖小
Vue.directive('dialogDragWidth', {
  bind (el, binding, vnode, oldVnode) {
    const dragDom = binding.value.$el.querySelector('.el-dialog')

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

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

        // 通过事件委托,计算移动的距离
        const l = e.clientX - disX
        dragDom.style.width = `${l}px`
      }

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

index.js

import Dialog from './Dialog'
/**
 * ElDialog功能扩展
 * @namespace ElDialog
 */
const ElDialog = {
  install (Vue) {
    Vue.component(Dialog.name, Dialog)
  }
}
export default ElDialog

在main.js引入
import dialog from 'xxx
Vue.use(Dialog)
参考文章
弹框拖拽

你可能感兴趣的:(elementui,umyui,vue,elementui,js)