• Stars
    star
    3,839
  • Rank 10,932 (Top 0.3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 11 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

👇 Make that shiz draggable

Draggabilly

Make that shiz draggable

draggabilly.desandro.com

Rad because it supports mouse and touch devices.

Draggabilly v3.0.0

Install

Download

Package managers

Install with npm: npm install draggabilly

Install with Yarn: yarn add draggabilly

CDN

Link directly to draggabilly.pkgd.min.js on unpkg.com.

<script src="https://unpkg.com/draggabilly@3/dist/draggabilly.pkgd.min.js"></script>

Usage

Initialize Draggabilly as a jQuery plugin

var $draggable = $('.draggable').draggabilly({
  // options...
})

Initialize Draggabilly with vanilla JS

var elem = document.querySelector('.draggable');
var draggie = new Draggabilly( elem, {
  // options...
});

// or pass in selector string as first argument
var draggie = new Draggabilly( '.draggable', {
  // options...
});

// if you have multiple .draggable elements
// get all draggie elements
var draggableElems = document.querySelectorAll('.draggable');
// array of Draggabillies
var draggies = []
// init Draggabillies
for ( var i=0; i < draggableElems.length; i++ ) {
  var draggableElem = draggableElems[i];
  var draggie = new Draggabilly( draggableElem, {
    // options...
  });
  draggies.push( draggie );
}

Classes

  • .is-pointer-down added when the user's pointer (mouse, touch, pointer) first presses down.
  • .is-dragging added when elements starts to drag.

Options

axis

Type: String

Values: 'x' or 'y'

axis: 'x'

Constrains movement to horizontal or vertical axis.

containment

Type: Element, Selector String, or Boolean

containment: '.container'

Contains movement to the bounds of the element. If true, the container will be the parent element.

grid

Type: Array

Values: [ x, y ]

grid: [ 20, 20 ]

Snaps the element to a grid, every x and y pixels.

handle

Type: Selector String, Array, HTMLElement

// select all .handle children with selector string
handle: '.handle'

// set as element
handle: element.querySelector('.handle')

// set as array or NodeList
handle: [ element.querySelector('.handle1'), element.querySelector('.handle2') ]

Specifies on what element the drag interaction starts.

handle is useful for when you do not want all inner elements to be used for dragging, like inputs and forms. See back handle example on CodePen.

Events

Bind events with jQuery with standard jQuery event methods .on(), .off(), and .one(). Inside jQuery event listeners this refers to the Draggabilly element.

// jQuery
function listener(/* parameters */) {
  // get Draggabilly instance
  var draggie = $(this).data('draggabilly');
  console.log( 'eventName happened', draggie.position.x, draggie.position.y );
}
// bind event listener
$draggable.on( 'eventName', listener );
// unbind event listener
$draggable.off( 'eventName', listener );
// bind event listener to trigger once. note ONE not ON
$draggable.one( 'eventName', function() {
  console.log('eventName happened just once');
});

Bind events with vanilla JS with .on(), .off(), and .once() methods. Inside vanilla JS event listeners this refers to the Draggabilly instance.

// vanilla JS
function listener(/* parameters */) {
  console.log( 'eventName happened', this.position.x, this.position.y );
}
// bind event listener
draggie.on( 'eventName', listener );
// unbind event listener
draggie.off( 'eventName', listener );
// bind event listener to trigger once. note ONCE not ONE or ON
draggie.once( 'eventName', function() {
  console.log('eventName happened just once');
});

dragStart

Triggered when dragging starts and the element starts moving. Dragging starts after the user's pointer has moved a couple pixels to allow for clicks.

// jQuery
$draggable.on( 'dragStart', function( event, pointer ) {...})
// vanilla JS
draggie.on( 'dragStart', function( event, pointer ) {...})
  • event - Type: Event - the original mousedown or touchstart event
  • pointer - Type: MouseEvent or Touch - the event object that has .pageX and .pageY

dragMove

Triggered when dragging moves.

// jQuery
$draggable.on( 'dragMove', function( event, pointer, moveVector ) {...})
// vanilla JS
draggie.on( 'dragMove', function( event, pointer, moveVector ) {...})
  • event - Type: Event - the original mousemove or touchmove event
  • pointer - Type: MouseEvent or Touch - the event object that has .pageX and .pageY
  • moveVector Type: Object - How far the pointer has moved from its start position { x: 20, y: -30 }

dragEnd

Triggered when dragging ends.

// jQuery
$draggable.on( 'dragEnd', function( event, pointer ) {...})
// vanilla JS
draggie.on( 'dragEnd', function( event, pointer ) {...})
  • event - Type: Event - the original mouseup or touchend event
  • pointer - Type: MouseEvent or Touch - the event object that has .pageX and .pageY

pointerDown

Triggered when the user's pointer (mouse, touch, pointer) presses down.

// jQuery
$draggable.on( 'pointerDown', function( event, pointer ) {...})
// vanilla JS
draggie.on( 'pointerDown', function( event, pointer ) {...})
  • event - Type: Event - the original mousedown or touchstart event
  • pointer - Type: MouseEvent or Touch - the event object that has .pageX and .pageY

pointerMove

Triggered when the user's pointer moves.

// jQuery
$draggable.on( 'pointerMove', function( event, pointer, moveVector ) {...})
// vanilla JS
draggie.on( 'pointerMove', function( event, pointer, moveVector ) {...})
  • event - Type: Event - the original mousemove or touchmove event
  • pointer - Type: MouseEvent or Touch - the event object that has .pageX and .pageY
  • moveVector Type: Object - How far the pointer has moved from its start position { x: 20, y: -30 }

pointerUp

Triggered when the user's pointer unpresses.

// jQuery
$draggable.on( 'pointerUp', function( event, pointer ) {...})
// vanilla JS
draggie.on( 'pointerUp', function( event, pointer ) {...})
  • event - Type: Event - the original mouseup or touchend event
  • pointer - Type: MouseEvent or Touch - the event object that has .pageX and .pageY

staticClick

Triggered when the user's pointer is pressed and unpressed and has not moved enough to start dragging.

click events are hard to detect with draggable UI, as they are triggered whenever a user drags. Draggabilly's staticClick event resolves this, as it is triggered when the user has not dragged.

// jQuery
$draggable.on( 'staticClick', function( event, pointer ) {...})
// vanilla JS
draggie.on( 'staticClick', function( event, pointer ) {...})
  • event - Type: Event - the original mouseup or touchend event
  • pointer - Type: MouseEvent or Touch - the event object that has .pageX and .pageY

Methods

disable

// jQuery
$draggable.draggabilly('disable')
// vanilla JS
draggie.disable()

enable

// jQuery
$draggable.draggabilly('enable')
// vanilla JS
draggie.enable()

setPosition

// jQuery
$draggable.draggabilly( 'setPosition', x, y )
// vanilla JS
draggie.setPosition( x, y )
  • x - Type: Number - horizontal position
  • y - Type: Number - vertical position

dragEnd

Stop dragging.

// jQuery
$draggable.draggabilly('dragEnd')
// vanilla JS
draggie.dragEnd()

destroy

// jQuery
$draggable.draggabilly('destroy')
// vanilla JS
draggie.destroy()

jQuery.fn.data('draggabilly')

Get the Draggabilly instance from a jQuery object. Draggabilly instances are useful to access Draggabilly properties.

var draggie = $('.draggable').data('draggabilly')
// access Draggabilly properties
console.log( 'draggie at ' + draggie.position.x + ', ' + draggie.position.y )

Properties

position

draggie.position
// => { x: 20, y: -30 }
  • position - Type: Object
  • x - Type: Number
  • y - Type: Number

Webpack & Browserify

Install Draggabilly with npm.

npm install draggabilly
var Draggabilly = require('draggabilly');

var draggie = new Draggabilly( '.draggable', {
  // options
});

To use Draggabilly as a jQuery plugin, you need to install and call jQuery Bridget.

npm install jquery-bridget
var $ = require('jquery');
var jQueryBridget = require('jquery-bridget');
var Draggabilly = require('draggabilly');

// make Draggabilly a jQuery plugin
jQueryBridget( 'draggabilly', Draggabilly, $ );
// now you can use $().draggabilly()
$('.draggable').draggabilly({...})

Browser support

Draggabilly v3 supports Chrome 49+, Firefox 41+, Safari 14+ (mobile & desktop), and Edge 12+.

License

Draggabilly is released under the MIT License. Have at it.


Made by David DeSandro 😻

More Repositories

1

masonry

🏩 Cascading grid layout plugin
HTML
16,189
star
2

imagesloaded

📷 JavaScript is all like "You images done yet or what?"
JavaScript
8,860
star
3

3dtransforms

📦 Introduction to CSS 3D transforms
CSS
1,003
star
4

classie

🎩 class helper functions
JavaScript
871
star
5

close-pixelate

Pixelate an image with <canvas> a la Chuck Close
JavaScript
725
star
6

colcade

Lightweight masonry layout
HTML
480
star
7

vanilla-masonry

[deprecated] Masonry layouts without any frameworks
JavaScript
329
star
8

practical-ui-physics

Learn basic physics for UI
JavaScript
323
star
9

breathing-halftone

Images go whoa with lots of floaty dots
JavaScript
254
star
10

windex

Pretty up your localhost. No more 1996 jank.
CSS
183
star
11

get-size

📐 Measure elements
JavaScript
176
star
12

arpeggiator

Web audio arpeggiator
JavaScript
176
star
13

CSS3.tmbundle

TextMate bundle for new CSS3 properties. Includes vendor specific properties and HTML5 elements
108
star
14

jquery-bridget

🌉 bridget makes jQuery plugins
JavaScript
108
star
15

zui-site-riot

Tutorial for zoom-scrolling on 2011.beercamp.com
JavaScript
54
star
16

eventie

event binding helper
JavaScript
54
star
17

doc-ready

lets get this party started... on document ready
JavaScript
50
star
18

sticky-titles

iOS/Android-like fixied titles that scroll sorta
JavaScript
33
star
19

jumbyl

Post to Tumblr from the command line
JavaScript
31
star
20

neo-vision

Chrome extension that syntax-highlights source code files with customizable themes
JavaScript
30
star
21

matches-selector

👗 matchesSelector helper
JavaScript
29
star
22

dotfiles

Personalized settings for Terminal, Git, TextMate, etc.
Shell
20
star
23

v3.desandro.com

All about me! In orange!
JavaScript
20
star
24

masonry-docs

📝 Documentation for Masonry
Handlebars
20
star
25

textmate-bundles

My modifications to TextMate's bundles
HTML
19
star
26

v2.desandro.com

My personal site. Runs on Stacey CMS
PHP
18
star
27

curtis-css-typeface

type made with CSS shapes
CSS
18
star
28

transitn

JS utility class for CSS transitions
JavaScript
17
star
29

get-style-property

quick & dirty CSS property testing
JavaScript
16
star
30

welovemariokart

JavaScript
14
star
31

dropshado.ws

👻 blog about front-end development minutiae
JavaScript
13
star
32

smallblog

🐭 I don't know how to put this in words but this is me trying
CSS
9
star
33

ani-halftone

Animated halftones
JavaScript
9
star
34

typekit-table

Sort and filter high-quality fonts from Typekit
JavaScript
8
star
35

requirejs-bower-homework

Help me figure out this RequireJS business
JavaScript
7
star
36

jtetypes

Try out fonts by James T. Edmondson
JavaScript
7
star
37

lists

📜 of lists
7
star
38

motion-emotion

Presentation on CSS transitions and transforms
6
star
39

demo

Demos, tests, and misfit fun things
JavaScript
6
star
40

organize-bower-sources

Get Bower dependency source files, in order, listed by file extension.
JavaScript
5
star
41

nclud-v3-talk

Presentation outline for talk about nclud.com v3
JavaScript
5
star
42

issues-agreement

Help me dev you
5
star
43

touchy-feely-dev

Outline for presentation at Future Insights Live 2012
CSS
4
star
44

mcgilldesandro.com

I'm getting hitched. This is the website.
JavaScript
4
star
45

dotvim

Vim config
Vim Script
4
star
46

make-julesmarie-cry

A sadistic bookmarklet for "Click here" links
JavaScript
4
star
47

mobile-safari-machete

"Mobile Safari: Bring your machete" presentation from Front Trends 2012
JavaScript
4
star
48

prefs

Personal preferences
3
star
49

blahble

A boring Tumblr theme
HTML
3
star
50

concatminify.com

Help newbie devs learn concat & minify
CSS
3
star
51

open-source-aint-free

Talk for Valio Con 2012
JavaScript
3
star
52

bower-introduction

Notes for presentation
2
star
53

masonry-v2-3-shim

Maintain backwards compatibility using Masonry v3 with code for Masonry v2.
JavaScript
2
star
54

v4.desandro.com

Personal site
Handlebars
1
star
55

button-group

JavaScript
1
star