Home

Awesome

v-dragged

Vue directive plugin for drag event detection.

NOTE: This directive listens for mouse/touch events, and sets a handler for when a drag action is detected. This is different from setting draggable on element, in that you need to move the element yourself according to the information v-dragged provides.

Install

npm install --save v-dragged
import Vue from 'vue'
import VDragged from 'v-dragged'

Vue.use(VDragged) 

Example

In your component:

<div v-dragged="onDragged"></div>
{
  // ...other options omitted

  methods: {
    onDragged({ el, deltaX, deltaY, offsetX, offsetY, clientX, clientY, first, last }) {
      if (first) {
        this.isDragging = true
        return
      }
      if (last) {
        this.isDragging = false
        return
      }
      var l = +window.getComputedStyle(el)['left'].slice(0, -2) || 0
      var t = +window.getComputedStyle(el)['top'].slice(0, -2) || 0
      el.style.left = l + deltaX + 'px'
      el.style.top = t + deltaY + 'px'
    }
  }
}

Event Details

The argument passed to the event handler is an object containing the following properties:

el

first

last

deltaX

deltaY

offsetX

offsetY

clientX

clientY

Modifier

prevent

<div v-dragged.prevent="onDragged"></div>