draggable-vue-directive

Vue2 directive that handles drag & drop.

Getting Started

npm install draggable-vue-directive --save

Typical use:

<div v-draggable>
    classic draggable
</div>

.vue file:

  import { Draggable } from 'draggable-vue-directive'
  ...
  export default {
        directives: {
            Draggable,
        },
  ...

with handler:

<div v-draggable="draggableValue">
    <div :ref="handleId">
        <img src="../assets/move.svg" alt="move">
    </div>
    drag and drop using handler
</div>

.vue file:

  import { Draggable } from 'draggable-vue-directive'
  ...
  export default {
        directives: {
            Draggable,
        },
        data() {
            return {
                handleId: "handle-id",
                draggableValue: { };
            }
        },
        mounted() {
            this.draggableValue.handle = this.$refs[this.handleId];
        }
  ...

draggable Value

The object passed to the directive is called the directive value.

For example in v-draggable="draggableValue" draggableValue can be an object containing the folowing fields:

handle

Type: HtmlElement | Vue

Required: false

There are two ways to use the draggable-vue-directive as showen in the demo above.

The simple use is just to put the directive on any Vue component or Html element and boom! the element is draggable.

The second option is to use handler. if you choose to use handler, the component itself will be draggable only using the handler.

onPositionChange

Type: Function

Required: false

In some cases it is useful to know the coordinates of the element when it's been dragged.

Passing a callback to draggableValue will achieve this goal and every time the element is being dragged the callback
will be executed with the current position as param.

  import { Draggable } from 'draggable-vue-directive'
  ...
  export default {
        directives: {
            Draggable,
        },
        data() {
            return {
                handleId: "handle-id",
                draggableValue: { };
            }
        },
        mounted() {
            this.draggableValue.handle = this.$refs[this.handleId];
            this.draggableValue.onPositionChange = this.onPosChanged;
        },
        methods: {
            onPosChanged: function(pos) {
                console.log("left corner", pos.x);
                console.log("top corner", pos.y);
            }
        }
  ...

resetInitialPos

Type: Boolean

Required: false

default: undefined

Returns to the initial position the element was before mounted.

stopDragging

Type: Boolean

Required: false

default: undefined

Immediately stop dragging.

boundingRect

Type: ClientRect

Required: false

default: undefined

Constrains dragging to within the bounds of the rectangle.

GitHub