zoukankan      html  css  js  c++  java
  • 【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   

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

  • 相关阅读:
    UVA12125 March of the Penguins (最大流+拆点)
    UVA 1317 Concert Hall Scheduling(最小费用最大流)
    UVA10249 The Grand Dinner(最大流)
    UVA1349 Optimal Bus Route Design(KM最佳完美匹配)
    UVA1212 Duopoly(最大流最小割)
    UVA1395 Slim Span(kruskal)
    UVA1045 The Great Wall Game(二分图最佳匹配)
    UVA12168 Cat vs. Dog( 二分图最大独立集)
    hdu3488Tour(KM最佳完美匹配)
    UVA1345 Jamie's Contact Groups(最大流+二分)
  • 原文地址:https://www.cnblogs.com/ylhssn/p/12035252.html
Copyright © 2011-2022 走看看