其他分享
首页 > 其他分享> > vue 元素鼠标拖动动画

vue 元素鼠标拖动动画

作者:互联网

自定义指令

Vue.directive('removable', {
  bind(el, binding, vnode, oldVnode) {
    if (!binding) return
    el.onmousedown = e => {
      el.style.cursor = 'move'
      const [transX, transY] = el.style.transform.match(/\-?\d+/g) || [0, 0]
      document.onmousemove = e1 => {
        e.preventDefault()
        const x = +transX + e1.clientX - e.clientX
        const y = +transY + e1.clientY - e.clientY
        el.style.transform = `translate(${x}px, ${y}px)`
      }
      document.onmouseup = e => {
        el.style.cursor = 'auto'
        document.onmousemove = null
        document.onmouseup = null
      }
    }
  }
})

使用方式

<div v-removable></div>

 

标签:el,style,vue,const,鼠标,clientY,拖动,document,e1
来源: https://www.cnblogs.com/senjer/p/15597463.html