Giter VIP home page Giter VIP logo

v-dragged's Introduction

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

  • The target element on which the directive binds.
  • type: HTMLElement

first

  • A boolean to indicate whether it is the first move of the drag. (drag starts here).
  • type: Boolean

last

  • A boolean to indicate whether it is the last move of the drag. (drag ends here).
  • type: Boolean

deltaX

  • The change of the pointer (mouse/touch)'s x coordinate from the last position.
    It is undefined when first or last is true.
  • type: Number

deltaY

  • The change of the pointer (mouse/touch)'s y coordinate from the last position.
    It is undefined when first or last is true.
  • type: Number

offsetX

  • The change of the pointer (mouse/touch)'s x coordinate from the starting position.
    It is undefined when first or last is true.
  • type: Number

offsetY

  • The change of the pointer (mouse/touch)'s y coordinate from the starting position.
    It is undefined when first or last is true.
  • type: Number

clientX

  • Current x coordinate of the pointer (mouse/touch).
  • type: Number

clientY

  • Current y coordinate of the pointer (mouse/touch).
  • type: Number

Modifier

prevent

  • prevent default events on pointer events (touchstart, touchmove, touchend, mousedown, mousemove, mouseup).
<div v-dragged.prevent="onDragged"></div>

v-dragged's People

Contributors

zhanziyang avatar zearin avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.