Drag and Drop

Version Author

This is a small library to enable drag and drop functionality in lists of HTML elements.

This library use the polyfill DragDropTouch by Bernardo-Castilho to support touch.

Installation

To install Drag and Drop you can use npm:

npm install sgb004-drag-drop

Or you can copy the code of the files:

Examples

Common use

Option A
Option B
Option C
Option D
Option E

Pass elements between columns

Option A
Option B
Option C
Option D
Option E
Option 1
Option 2
Option 3
Option 4
Option 5

Usage

The library consists of the following files:

DragDrop class

This class enables drag and drop on a list of elements inside a container.

import DragDrop from 'sgb004-drag-drop';

const container = document.getElementById('list');
const dragDrop = new DragDrop(container, 'li');

The DragDrop class handles:

Parameters

Methods

DragDropItem helper

The DragDropItem class handles drag and drop functionality for a single item. This is used internally by DragDrop.

import DragDropItem from 'sgb004-drag-drop';

const item = document.getElementById('item');
const dragDropItem = new DragDropItem(item, container, (origin, destiny) => {
//code to exchange elements...
});

Parameters

Methods

CSS

The DragDropItem class add to the item the next css classes:

Example

/* Generates an opacity on the element that is being dragged */
.drag-start {
opacity: 0.5;
}

/* Displays a dotted border on the element below the element being dragged */
.drag-over {
border-color: 1px dotted #5fa8d3;
}

Author

sgb004

License

MIT