天天看點

vue實作彈窗可拉伸寬高、移動彈窗的功能

檔案目錄

vue實作彈窗可拉伸寬高、移動彈窗的功能

directive/index.js

/* 全局批量注冊自定義指令 */
import drag from './dialog/drag'
import dragHeight from './dialog/dragHeight'
import dragWidth from './dialog/dragWidth'

// 自定義指令
const directives = {
  elDragDialog,
  drag,
  dragHeight,
  dragWidth,
}

export default {
  install(Vue) {
    Object.keys(directives).forEach((key) => {
      Vue.directive(key, directives[key])
    })
  },
}
           

mian.js

/* 自定義指令的全局注冊 */
import Directive from '@/directive';
Vue.use(Directive);
           

directive/dialog/drag.js

/**
* v-dialogDrag 彈窗拖拽
* Copyright (c) 2022
*/

export default {
  bind(el, binding, vnode, oldVnode) {
    const value = binding.value
    if (value == false) return
    // 擷取拖拽内容頭部
    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)
    dragDom.style.position = 'absolute'
    dragDom.style.marginTop = 0
    let width = dragDom.style.width
    if (width.includes('%')) {
      width = +document.body.clientWidth * (+width.replace(/\%/g, '') / 100)
    } else {
      width = +width.replace(/\px/g, '')
    }
    dragDom.style.left = `${(document.body.clientWidth - width) / 2}px`
    // 滑鼠按下事件
    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

        const finallyL = l + styL
        const finallyT = t + styT

        // 移動目前元素
        dragDom.style.left = `${finallyL}px`
        dragDom.style.top = `${finallyT}px`
      }

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

directive/dialog/dragHeight.js

/**
* v-dialogDragWidth 可拖動彈窗高度(右下角)
* Copyright (c) 2022
*/

export default {
  bind(el) {
    const dragDom = el.querySelector('.el-dialog')
    const lineEl = document.createElement('div')
    lineEl.style = 'width: 6px; background: inherit; height: 10px; position: absolute; right: 0; bottom: 0; margin: auto; z-index: 1; cursor: nwse-resize;'
    lineEl.addEventListener('mousedown',
      function(e) {
        // 滑鼠按下,計算目前元素距離可視區的距離
        const disX = e.clientX - el.offsetLeft
        const disY = e.clientY - el.offsetTop
        // 目前寬度 高度
        const curWidth = dragDom.offsetWidth
        const curHeight = dragDom.offsetHeight
        document.onmousemove = function(e) {
          e.preventDefault() // 移動時禁用預設事件
          // 通過事件委托,計算移動的距離
          const xl = e.clientX - disX
          const yl = e.clientY - disY
          dragDom.style.width = `${curWidth + xl}px`
          dragDom.style.height = `${curHeight + yl}px`
        }
        document.onmouseup = function(e) {
          document.onmousemove = null
          document.onmouseup = null
        }
      }, false)
    dragDom.appendChild(lineEl)
  }
}
           

directive/dialog/dragWidth.js

/**
* v-dialogDragWidth 可拖動彈窗寬度(右側邊)
* Copyright (c) 2022
*/

export default {
  bind(el) {
    const dragDom = el.querySelector('.el-dialog')
    const lineEl = document.createElement('div')
    lineEl.style = 'width: 5px; background: inherit; height: 80%; position: absolute; right: 0; top: 0; bottom: 0; margin: auto; z-index: 1; cursor: w-resize;'
    lineEl.addEventListener('mousedown',
      function(e) {
        // 滑鼠按下,計算目前元素距離可視區的距離
        const disX = e.clientX - el.offsetLeft
        // 目前寬度
        const curWidth = dragDom.offsetWidth
        document.onmousemove = function(e) {
          e.preventDefault() // 移動時禁用預設事件
          // 通過事件委托,計算移動的距離
          const l = e.clientX - disX
          dragDom.style.width = `${curWidth + l}px`
        }
        document.onmouseup = function(e) {
          document.onmousemove = null
          document.onmouseup = null
        }
      }, false)
    dragDom.appendChild(lineEl)
  }
}
           

使用

<el-dialog
      v-drag
      v-drag-height
      v-drag-width
      :visible.sync="visible"
      :before-close="hideDialog"
      :modal-append-to-body="false"
      append-to-body
      width="1200px"
      class="my-dialog-map-mark"
      title="點位選擇"
    ></el-dialog>
           

繼續閱讀