• 【Vue 2.X】基于ElementUI 实现 dialog弹窗移动效果-自定义指令系列(二)


    v-dialogDrag: 弹窗拖拽

    使用: <el-dialog XXX v-dialogDrag></el-dialog>
     1 
     2 Vue.directive('dialogDrag', {
     3     bind(el, binding, vnode, oldVnode) {
     4       const dialogHeaderEl = el.querySelector('.el-dialog__header')
     5       const dragDom = el.querySelector('.el-dialog')
     6       dialogHeaderEl.style.cursor = 'move'
     7   
     8       // 获取原有属性 ie dom元素.currentStyle 火狐谷歌 window.getComputedStyle(dom元素, null);
     9       const sty = dragDom.currentStyle || window.getComputedStyle(dragDom, null)
    10   
    11       dialogHeaderEl.onmousedown = (e) => {
    12         // 鼠标按下,计算当前元素距离可视区的距离
    13         const disX = e.clientX - dialogHeaderEl.offsetLeft
    14         const disY = e.clientY - dialogHeaderEl.offsetTop
    15   
    16         // 获取到的值带px 正则匹配替换
    17         let styL, styT
    18   
    19         // 注意在ie中 第一次获取到的值为组件自带50% 移动之后赋值为px
    20         if (sty.left.includes('%')) {
    21           styL = +document.body.clientWidth * (+sty.left.replace(/\%/g, '') / 100)
    22           styT = +document.body.clientHeight * (+sty.top.replace(/\%/g, '') / 100)
    23         } else {
    24           styL = +sty.left.replace(/px/g, '')
    25           styT = +sty.top.replace(/px/g, '')
    26         }
    27   
    28         document.onmousemove = function(e) {
    29           // 通过事件委托,计算移动的距离
    30           const l = e.clientX - disX
    31           const t = e.clientY - disY
    32   
    33           // 移动当前元素
    34           dragDom.style.left = `${l + styL}px`
    35           dragDom.style.top = `${t + styT}px`
    36   
    37           // 将此时的位置传出去
    38           // binding.value({x:e.pageX,y:e.pageY})
    39         }
    40   
    41         document.onmouseup = function(e) {
    42           document.onmousemove = null
    43           document.onmouseup = null
    44         }
    45       }
    46     }
    47   })
    48   

     声明:此自定义指令照搬的网络上的文章,但由于时间久远、已不记得文章地址,若有侵犯、请留言告知文章地址,看见后会及时挂上文章转载地址。谢谢!

  • 相关阅读:
    大话西游·『一生所爱』
    大话西游·『一生所爱』
    大话西游·『一生所爱』
    2013年终的总结
    2013年终的总结
    2013年终的总结
    Python高效编程技巧
    Python高效编程技巧
    Python高效编程技巧
    分分钟搞定Python之排序与列表
  • 原文地址:https://www.cnblogs.com/ylhssn/p/12035252.html
Copyright © 2020-2023  润新知