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.
npm install --save v-dragged
import Vue from 'vue'
import VDragged from 'v-dragged'
Vue.use(VDragged)
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'
}
}
}
The argument passed to the event handler is an object containing the following properties:
- The target element on which the directive binds.
- type: HTMLElement
- A boolean to indicate whether it is the first move of the drag. (drag starts here).
- type: Boolean
- A boolean to indicate whether it is the last move of the drag. (drag ends here).
- type: Boolean
- The change of the pointer (mouse/touch)'s x coordinate from the last position.
It isundefined
whenfirst
orlast
istrue
. - type: Number
- The change of the pointer (mouse/touch)'s y coordinate from the last position.
It isundefined
whenfirst
orlast
istrue
. - type: Number
- The change of the pointer (mouse/touch)'s x coordinate from the starting position.
It isundefined
whenfirst
orlast
istrue
. - type: Number
- The change of the pointer (mouse/touch)'s y coordinate from the starting position.
It isundefined
whenfirst
orlast
istrue
. - type: Number
- Current x coordinate of the pointer (mouse/touch).
- type: Number
- Current y coordinate of the pointer (mouse/touch).
- type: Number
- prevent default events on pointer events (
touchstart
,touchmove
,touchend
,mousedown
,mousemove
,mouseup
).
<div v-dragged.prevent="onDragged"></div>