Giter Site home page Giter Site logo

liangzuan1983 / draggable-vue-directive-unility Goto Github PK

View Code? Open in Web Editor NEW

This project forked from israelzablianov/draggable-vue-directive

0.0 1.0 0.0 36 KB

任意拖动改变控件位置。Vue2 directive that handles drag & drop

License: MIT License

TypeScript 100.00%

draggable-vue-directive-unility's Introduction

draggable-vue-directive

GitHub open issues npm download npm download per month npm version Package Quality vue2 MIT License

Vue directive (Vue.js 2.x) for handling element drag & drop.

Installation

npm install draggable-vue-directive --save

Demo

demo gif

The live demo can be found in https://israelzablianov.github.io/draggable-demo

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: {
                    handle: undefined
                };
            }
        },
        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 3 params: positionDiff, absolutePosition (the current position, the first time the directive added to the DOM or being initialized, the value will be undefined unless the element has left and top values), event.

  import { Draggable } from 'draggable-vue-directive'
  ...
  export default {
        directives: {
            Draggable,
        },
        data() {
            return {
                draggableValue: {
                    onPositionChange: this.onPosChanged
                };
            }
        },
        methods: {
            onPosChanged: function(positionDiff, absolutePosition, event) {
                console.log("left corner", absolutePosition.left);
                console.log("top corner", absolutePosition.top);
            }
        }
  ...

onDragEnd

Type: Function
Required: false

Emits only when draging ended, has the same functionality as onPositionChange.

onDragStart

Type: Function
Required: false

Emits only when draging started, has the same functionality as onPositionChange.

resetInitialPos

Type: Boolean
Required: false
default: undefined

Returns to the initial position the element was before mounted.

initialPosition

Type: Position
Required: false
default: undefined

Sets the absolute starting position of this element.
Will be applied when resetInitialPos is true.

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.

boundingElement

Type: HtmlElement
Required: false
default: undefined

Constrains dragging to within the bounds of the element.

boundingRectMargin

Type: MarginOptions
Required: false
default: undefined

When using boundingRect or boundingElement, you can pass an object with top, left, bottom, right properties, to define a margin between the elements and the boundries.

draggable-vue-directive-unility's People

Contributors

israelzablianov avatar cavanmflynn avatar safrmo avatar deckele avatar

Watchers

James Cloos 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.