Implement Drag, Drop, and Sortable Behaviors jQuery UI | agnostic-draggable

agnostic-draggable is an undefined set of libraries that implement drag, drop, and sort behaviors inspired by jQuery UI. To enable the drag function on any DOM. Move a Draggable by clicking and dragging it anywhere inside the viewport.

Converts items into areas that can be dropped. This means that Draggable Controlled items can be dropped into Droppable if you accept them. Transforms items containing multiple subnodes in such a way that these subnodes can be sorted with the mouse.

Draggable

Enables the drag functionality on any DOM element. Move the Draggable element by clicking and dragging it anywhere within the viewport.

Elements modified or created will use the same CSS classes inherited from the jQuery UI implementation:

  • ui-draggable: the element being dragged
  • ui-draggable-handle: The handle of the pullable element. By default, each draggable is also a handle
  • ui-draggable-helper: the dragging helper

Must Read: Customizable, Draggable, and Infinitely Looping Carousels | Jelly Slider

How to make use of it:

1. Install & Download the package.

# NPM
$ npm i agnostic-draggable --save

2. Import the Draggable/Droppable/Sortable parts as ES modules.

// draggable
import { Draggable } from 'agnostic-draggable';

// sortable
import { Sortable } from 'agnostic-draggable';

// draggable + droppable
import { Draggable, Droppable } from 'agnostic-draggable';

3. Or immediately load the agnostic-draggable.min.js JavaScript within the doc.

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/agnostic-draggable.min.js"></script>

Draggable:

1. Enables a component to be draggable.

<div id="draggable">Drag Me</div>
new Draggable(document.querySelector('#draggable'), {
    // options here
}, eventHandlers);

2. Available options for Draggable.

new Draggable(document.querySelector('#draggable'), {

    // append the draggable element to a specific element
    appendTo: 'parent',

    // x or y
    axis: null,

    // connect to a Sortable list
    // e.g. '#sort'
    connectToSortable: null,

    // parent, document, window, a CSS selector or an array of 4 numbers in the form [x1, y1, x2, y2]
    containment: null,

    // cursor type
    cursor: null,

    // if the draggable element is disabled
    disabled: false,

    // distance in pixels that the mouse should move before the dragging should start.
    distance: 0,

    // snap the dragging helper to a grid
    // e.g. [10, 10]
    grid: null,

    // drag handle element
    handle: null,

    // original, clone or a function
    helper: 'original',

    // opacity of the draggable element while being dragged
    opacity: null,

    // invalid, valid, true, false or a function
    revert: false,

    // duration in milliseconds
    revertDuration: 200,

    // used to group sets of Draggable, Droppable and Sortable elements
    scope: 'default',

    // determine whether the container is scrollable
    scroll: true,
    scrollSensitivity: 20,
    scrollSpeed: 10,

    // stack to another element
    stack: null,

    // ignore these elements
    skip: 'input, textarea, button, select, option',

    // z-index property
    zIndex: null
    
});

3. Available events for Draggable.

new Draggable(document.querySelector('#draggable'), {OPTIONS}, {
    'draggable:init ': function (event) {
      // do something
    },
    'drag:start': function (event) {
      // do something
    },
    'drag:move': function (event) {
      // do something
    },
    'drag:stop': function (event) {
      // do something
    },
    'draggable:destroy': function (event) {
      // do something
    }
});

Droppable:

1. Enables an element to be droppable.

<div id="droppable">Drop Here</div>
new Droppable(document.querySelector('#droppable'), {
    // options here
}, eventHandlers);

2. Available options for Droppable.

new Droppable(document.querySelector('#droppable'), {

    // which Draggable elements are accepted
    accept: '*',

    // disable the Droppable behaviour
    disabled: false,

    // handle dropping on nested Droppable elements
    greedy: false,

    // used to group sets of Draggable, Droppable and Sortable elements
    scope: 'default',

    // fit, intersect, pointer or touch
    tolerance: 'intersect'
    
}, eventHandlers);

3. Available events for Droppable.

new Droppable(document.querySelector('#droppable'), {OPTIONS}, {
    'droppable:init': function (event) {
      // do something
    },
    'droppable:activate': function (event) {
      // do something
    },
    'droppable:over': function (event) {
      // do something
    },
    'droppable:drop': function (event) {
      // do something
    },
    'droppable:out': function (event) {
      // do something
    },
    'droppable:deactivate': function (event) {
      // do something
    },,
    'draggable:destry': function (event) {
      // do something
    },
});

Sortable

1. Enables a record to be sortable.

<div id="sortable">
  <div>Item 1</div>
  <div>Item 2</div>
  <div>Item 3</div>
  ...
</div>
new Sortable(document.querySelector('#sortable'), {
    // options here 
}, eventHandlers);

2. Available options for Sortable.

new Sortable(document.querySelector('#sortable'), {

    // append the draggable element to a specific element
    appendTo: 'parent',

    // x or y
    axis: null,

    // connect to a Sortable list
    // e.g. '#sort'
    connectToSortable: null,

    // parent, document, window, a CSS selector or an array of 4 numbers in the form [x1, y1, x2, y2]
    containment: null,

    // cursor type
    cursor: null,

    // if the draggable element is disabled
    disabled: false,

    // distance in pixels that the mouse should move before the dragging should start.
    distance: 0,

    // if false, items from this Sortable can't be dropped on an empty connected Sortable
    dropOnEmpty: true,

    // force the sorting helper to have a size
    forceHelperSize: false,

    // force the sorting placeholder to have a size
    forcePlaceholderSize: false,

    // snap the dragging helper to a grid
    // e.g. [10, 10]
    grid: null,

    // drag handle element
    handle: null,

    // original, clone or a function
    helper: 'original',

    // specify which items should be sortable
    items: null,

    // opacity of the draggable element while being dragged
    opacity: null,

    // invalid, valid, true, false or a function
    revert: false,

    // duration in milliseconds
    revertDuration: 200,

    // used to group sets of Draggable, Droppable and Sortable elements
    scope: 'default',

    // determine whether the container is scrollable
    scroll: true,
    scrollSensitivity: 20,
    scrollSpeed: 10,

    // stack to another element
    stack: null,

    // ignore these elements
    skip: 'input, textarea, button, select, option',

    // z-index property
    zIndex: null

});

3. Available events for Sortable.

new Droppable(document.querySelector('#droppable'), {OPTIONS}, {
    'sortable:init': function (event) {
      // do something
    },
    'sortable:activate': function (event) {
      // do something
    },
    'sort:start': function (event) {
      // do something
    },
    'sort:move': function (event) {
      // do something
    },
    'sort:stop': function (event) {
      // do something
    },
    'sortable:over': function (event) {
      // do something
    },
    'sortable:change': function (event) {
      // do something
    },
    'sortable:remove': function (event) {
      // do something
    },
    'sortable:receive': function (event) {
      // do something
    },
    'sortable:update': function (event) {
      // do something
    },
    'sortable:out': function (event) {
      // do something
    },
    'sortable:deactivate': function (event) {
      // do something
    },
    'sortable:destroy': function (event) {
      // do something
    }
});

Options

Must Read: Draggable Drag-n-Drop Email Editor Component for Vue.js

NameTypeDefaultDescription
appendTo{String}parentWhere to append the dragging helper.
axis{String}nullConstraint dragging movement to an axis.
connectToSortable{String}nullAllows the Draggable to be dropped onto the specified Sortable elements.
containment{String,Array}nullConstraints dragging movement to an element or region.
cursor{String}nullAllows changing the cursor style while dragging.
disabled{Boolean}falseAllows disabling the dragging behavior.
distance{Number}0Distance in pixels before dragging can start.
grid{Array}nullSnaps the dragging helper to a grid, every x and y pixel.
handle{String}nullDragging only starts if the click matches this selector.
helper{String,Function}originalConfigures the dragging helper node.
opacity{Number}nullAllows changing the opacity while dragging.
revert{Boolean,String,Function}falseWhether the element should be reverted after dragging stops.
revertDuration{Number}200Duration in milliseconds of the revert animation.
scope{String}defaultUsed to group sets of Draggable, Droppable, and Sortable elements.
scroll{Boolean}trueAllows auto-scrolling within the container.
scrollSensitivity{Number}20Scroll sensitivity in pixels.
scrollSpeed{Number}10Scroll speed in pixels.
skip{String}input, textarea, button, select, optionPrevents dragging when this CSS selector is matched.
stack{String}nullManages the z-indexes of Draggable elements matching this CSS selector.
zIndex{Number}nullAllows changing the z-index while dragging.

Events

NameDescription
draggable:initCalled when the Draggable is initialized.
drag:startCalled when the drag operation is started. Can be canceled.
drag:moveCalled while dragging the element. Can be canceled.
drag:stopCalled when the drag operation stops. Can be canceled, preventing an unwanted drop.
draggable:destroyCalled when the Draggable is destroyed.

See Demo And Download

agnostic-draggable

Official Website(marcospont): Click Here

This superior jQuery/javascript plugin is developed by marcospont. For extra Advanced Usage, please go to the official website.

Related Posts

lazyload-embed-vimeo-player

Simple and Lightweight LazyLoad Embed Vimeo Player Plugin in Pure JavaScript

LazyLoad Embed Vimeo Player – Simple and Lightweight Plugin – Pure JavaScript. Vimeo LazyLoad is the sister project of Youtube LazyLoad that loads an on-demand Vimeo video…

Star-Rating-Plugin

Star Rating Plugin Control Based On Radio Buttons

Star Rating Radio Buttons, the star rating plugin is a plugin for the jQuery Javascript library that creates a non-intrusive star rating control based on a set…

QR-Code-With-Pure-JavaScript

A Pure QRCode Encode and Decode JavaScript Library | Decoding/Encoding

QR Code With Pure JavaScript – A pure JavaScript QRCode encode and decode library. QR code encoder and decoder instruments help you shortly generate QR codes from…

Vertical-3-Dot-Context-Menu

Simple and Lightweight Vertical 3-Dot Context Menu

The 3-dot Context Menu allows for a simple and lightweight implementation to show 3 vertical dots allowing the user to click and show a menu. Below is…

Pure-JavaScript-Toast-Notification

Pure Toast Notification JavaScript Library | Toastify JS

Toast Notification JavaScript – Toastify JS is an easy, lightweight, vanilla JavaScript library used for sending stackable, non-blocking toast messages to end customers. Features Multiple stacked notifications…

bootstrap-bs-dialogs

Dialogs Based on Bootstrap 5 Modal Component | BS-DIALOGS

BS-DIALOGS is a small library for creating modal windows for notification and input using the Bootstrap 5 modal. It automatically handles DOM processing and provides callbacks to…

Leave a Reply

Your email address will not be published. Required fields are marked *