Top Related Projects
:ok_hand: Drag and drop so simple it hurts
Reorderable drag-and-drop lists for modern browsers and touch devices. No jQuery or framework required.
Moveable! Draggable! Resizable! Scalable! Rotatable! Warpable! Pinchable! Groupable! Snappable!
Beautiful and accessible drag and drop for lists with React
The JavaScript Drag & Drop library your grandparents warned you about.
Quick Overview
Draggabilly is a lightweight JavaScript library that makes HTML elements draggable. It provides a simple and flexible way to add drag-and-drop functionality to web applications, with support for both mouse and touch events.
Pros
- Easy to use and integrate into existing projects
- Supports both mouse and touch events for broad device compatibility
- Customizable with various options and events
- Lightweight with no dependencies
Cons
- Limited to dragging functionality only (no resizing or other interactions)
- May require additional code for complex drag-and-drop scenarios
- Not actively maintained (last update was in 2021)
Code Examples
Basic usage:
// Make an element draggable
var elem = document.querySelector('#draggable');
var draggie = new Draggabilly(elem);
Draggable within a container:
var draggie = new Draggabilly('#draggable', {
containment: '#container'
});
Multiple draggable elements:
var draggableElems = document.querySelectorAll('.draggable');
var draggies = [];
for (var i = 0; i < draggableElems.length; i++) {
var draggie = new Draggabilly(draggableElems[i]);
draggies.push(draggie);
}
Getting Started
-
Include the Draggabilly script in your HTML:
<script src="https://unpkg.com/draggabilly@2.3.0/dist/draggabilly.pkgd.min.js"></script>
-
Create an element to be draggable:
<div id="draggable">Drag me!</div>
-
Initialize Draggabilly in your JavaScript:
document.addEventListener('DOMContentLoaded', function() { var elem = document.querySelector('#draggable'); var draggie = new Draggabilly(elem, { // options... }); });
-
Customize the draggable behavior using options and events as needed:
draggie.on('dragEnd', function(event, pointer) { console.log('Dragging ended!'); });
Competitor Comparisons
:ok_hand: Drag and drop so simple it hurts
Pros of Dragula
- Supports drag and drop between containers, not just within a single container
- Lightweight and has no dependencies
- Provides more extensive customization options and events
Cons of Dragula
- May have a steeper learning curve due to more advanced features
- Less focused on single-element dragging, which might be overkill for simpler use cases
Code Comparison
Draggabilly:
var elem = document.querySelector('.item');
var draggie = new Draggabilly(elem, {
axis: 'x',
containment: '.container'
});
Dragula:
dragula([document.querySelector('#left-container'),
document.querySelector('#right-container')], {
moves: function (el, source, handle, sibling) {
return handle.className === 'handle';
}
});
Key Differences
- Draggabilly focuses on making individual elements draggable within a container
- Dragula specializes in drag-and-drop functionality between multiple containers
- Draggabilly provides more straightforward setup for basic dragging scenarios
- Dragula offers more advanced features like custom drag handles and drop rules
Both libraries have their strengths, with Draggabilly being simpler for basic dragging needs and Dragula offering more powerful features for complex drag-and-drop interactions.
Reorderable drag-and-drop lists for modern browsers and touch devices. No jQuery or framework required.
Pros of Sortable
- More feature-rich, supporting sorting, dragging, and dropping within and between lists
- Supports touch devices and works well with mobile interfaces
- Offers animation and transition effects for smoother user experience
Cons of Sortable
- Larger file size and potentially higher performance overhead
- Steeper learning curve due to more complex API and configuration options
Code Comparison
Draggabilly:
var elem = document.querySelector('.item');
var draggie = new Draggabilly(elem, {
axis: 'x',
containment: '.container'
});
Sortable:
var el = document.getElementById('items');
var sortable = Sortable.create(el, {
animation: 150,
ghostClass: 'blue-background-class'
});
Key Differences
- Draggabilly focuses on making individual elements draggable, while Sortable specializes in creating sortable lists
- Sortable offers more advanced features like multi-list sorting and nested lists
- Draggabilly provides finer control over drag behavior, such as axis constraints and grid snapping
Use Cases
- Draggabilly: Ideal for simple drag-and-drop interfaces, custom sliders, or repositionable elements
- Sortable: Better suited for complex list management, kanban boards, or drag-and-drop file uploads
Both libraries have their strengths, and the choice between them depends on the specific requirements of your project and the level of functionality needed.
Moveable! Draggable! Resizable! Scalable! Rotatable! Warpable! Pinchable! Groupable! Snappable!
Pros of Moveable
- More comprehensive feature set, including resizing, rotating, and snapping
- Supports multiple elements and grouping
- Better touch device support and performance optimization
Cons of Moveable
- Steeper learning curve due to more complex API
- Larger file size and potentially higher overhead for simple use cases
- May require additional setup for basic dragging functionality
Code Comparison
Draggabilly:
var elem = document.querySelector('#draggable');
var draggie = new Draggabilly(elem, {
axis: 'x',
containment: true
});
Moveable:
const moveable = new Moveable(document.body, {
target: document.querySelector("#target"),
draggable: true,
resizable: true,
rotatable: true
});
moveable.on("drag", ({ target, transform }) => {
target.style.transform = transform;
});
Summary
Moveable offers a more feature-rich solution with advanced capabilities like resizing and rotating, making it suitable for complex interactions. However, this comes at the cost of a steeper learning curve and potentially unnecessary overhead for simpler use cases. Draggabilly, on the other hand, provides a straightforward API for basic dragging functionality, making it easier to implement for simple drag-and-drop scenarios.
Beautiful and accessible drag and drop for lists with React
Pros of react-beautiful-dnd
- Specifically designed for React applications, offering seamless integration
- Provides a more accessible drag-and-drop experience with keyboard support
- Offers advanced features like multi-drag and customizable animations
Cons of react-beautiful-dnd
- Limited to React applications, whereas Draggabilly is framework-agnostic
- Has a steeper learning curve due to its more complex API
- Larger bundle size compared to the lightweight Draggabilly
Code Comparison
react-beautiful-dnd:
import { DragDropContext, Droppable, Draggable } from 'react-beautiful-dnd';
<DragDropContext onDragEnd={onDragEnd}>
<Droppable droppableId="list">
{(provided) => (
<ul {...provided.droppableProps} ref={provided.innerRef}>
{items.map((item, index) => (
<Draggable key={item.id} draggableId={item.id} index={index}>
{(provided) => (
<li ref={provided.innerRef} {...provided.draggableProps} {...provided.dragHandleProps}>
{item.content}
</li>
)}
</Draggable>
))}
{provided.placeholder}
</ul>
)}
</Droppable>
</DragDropContext>
Draggabilly:
var elem = document.querySelector('#draggable');
var draggie = new Draggabilly(elem, {
axis: 'x',
containment: true
});
draggie.on('dragEnd', function(event, pointer) {
console.log('dragEnd', pointer.pageX, pointer.pageY);
});
The JavaScript Drag & Drop library your grandparents warned you about.
Pros of Draggable
- More comprehensive feature set, including sortable lists and multiple draggable types
- Better performance for complex drag operations and large numbers of elements
- Active development and maintenance by Shopify team
Cons of Draggable
- Steeper learning curve due to more complex API
- Larger file size, which may impact load times for smaller projects
Code Comparison
Draggabilly:
var elem = document.querySelector('.item');
var draggie = new Draggabilly(elem, {
axis: 'x'
});
Draggable:
import { Draggable } from '@shopify/draggable';
const draggable = new Draggable(document.querySelectorAll('.item'), {
draggable: '.item',
axis: 'x'
});
Key Differences
- Draggabilly is simpler and more lightweight, ideal for basic drag-and-drop functionality
- Draggable offers more advanced features and better suits complex applications
- Draggabilly uses a constructor-based approach, while Draggable employs a more modular, import-based system
- Draggable provides better TypeScript support and more extensive documentation
Use Cases
- Choose Draggabilly for simple, standalone drag-and-drop elements
- Opt for Draggable when building complex interfaces with multiple draggable types or sortable lists
- Consider Draggable for larger projects that require scalability and ongoing maintenance
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Draggabilly
Make that shiz draggable
Rad because it supports mouse and touch devices.
Draggabilly v3.0.0
Install
Download
- draggabilly.pkgd.min.js minified
- draggabilly.pkgd.js un-minified
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 originalmousedown
ortouchstart
eventpointer
- 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 originalmousemove
ortouchmove
eventpointer
- 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 originalmouseup
ortouchend
eventpointer
- 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 originalmousedown
ortouchstart
eventpointer
- 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 originalmousemove
ortouchmove
eventpointer
- 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 originalmouseup
ortouchend
eventpointer
- 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 originalmouseup
ortouchend
eventpointer
- 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 positiony
- 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: Objectx
- Type: Numbery
- 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+.
- Use Draggabilly v2 for IE10 support and Safari 8 support.
- Use Draggabilly v2.1 for Android 4+ and Safari 6+ support.
- Use Draggabilly v1 for IE8 & 9, and Android 2.3+ support.
License
Draggabilly is released under the MIT License. Have at it.
Made by David DeSandro ð»
Top Related Projects
:ok_hand: Drag and drop so simple it hurts
Reorderable drag-and-drop lists for modern browsers and touch devices. No jQuery or framework required.
Moveable! Draggable! Resizable! Scalable! Rotatable! Warpable! Pinchable! Groupable! Snappable!
Beautiful and accessible drag and drop for lists with React
The JavaScript Drag & Drop library your grandparents warned you about.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot