Code Monkey home page Code Monkey logo

draggy's Introduction


Draggy

<a href="/license"><img src="https://img.shields.io/npm/l/draggy.svg"/></a>
<img src="https://david-dm.org/dfcreative/draggy.svg"/>

Draggable behaviour for elements. Demo.

npm install draggy

var Draggable = require('draggy');

//make an element draggable
var el = document.querySelector('.my-element');
var draggy = new Draggable(el, {
	release: true,
	sniper: false,
	axis: 'x'
});

//bind event handler
draggy.on('drag', function () {

});

Options

Parameter Default Description
axis null Restrict movement by axis: 'x', 'y' or null.
pin [0,0, selfWidth, selfHeight] An area within draggable element which is surely resides within movement limits. Useful if you need draggable element to be restricted not by it’s own shape, but by some inner shape.
precision 1 Round position to that extent, in pixels.
css3 true Use position or translate3d to place element. The first is more precise and reliable, the second is faster.
release false Continue movement when user releases drag.
repeat false Cycle movement by one of axis: 'x', 'y' or 'both'.
sniper true Slow down movement by pressing Ctrl/Cmd.
threshold 0 A movement threshold required to start drag - whether array, number or function.
within document Restrict movement within the container. Pass 'parent' to take parent node.
handle self.element Use the passed element or selector as a handle for drag. Clicking not on the handle will be ignored. Passing null will ignore handle.
cancel undefined Ignore dragging started on the elements matching the selector.
droppable undefined Selector, element or list of elements to detect droppable areas. For each drop element will be invoked drop, dragoveranddragout` events.

Events

Name Description
dragstart Drag started. Called both on element/controller.
threshold Entered threshold mode. Called on controller.
drag Drag iteration. Called both on element/controller.
track Track movement. Called on controller.
release User released drag. Called on controller.
dragend Drag has completely finished. Called both on element/controller.
dragover Called on self and on drop target.
dragout Called on self and on drop target.
drop Called on self and on drop target.

API

Name Description
Draggy.cache WeakMap containing draggy instances for elements. Draggy.cache.get(element) - return draggy instance for the element.
Draggy.prototype.move(x, y) Move to a new position, taking into account axis and limits. You can redefine this method to implement custom kinds of movement restrictions, like circular movement. But who dares?
Draggy.prototype.state Current drag state: 'idle', 'threshold', 'drag', 'release'.
Draggy.prototype.update(event?) Update movement limits. event is optional.

What draggy is not

  • It doesn’t do ghost move, as it is not draggable behaviour and can be implemented externally.
  • It doesn’t do mouse hiding on drag, as it is implementable externally via callbacks.
  • It doesn’t init itself automatically on elements as it is due to user to decide when to create/init draggable elements.
  • It doesn’t polyfill native draggable, as it targets to complete simple task of visual placement of element.

draggy's People

Contributors

dy 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.