vue 元素鼠标拖动动画(Vue element mouse drag animation)

自定义指令

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>
————————

Custom instruction

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
      }
    }
  }
})

Mode of use

<div v-removable></div>