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>

 

上一篇:phoenix 三板斧之加盐压缩加索引


下一篇:Nginx中break和last的区别