Draggable
Allows you to drag around elements on the screen.
new draggable(draggableOptions);Basic implementation
Section titled “Basic implementation”const square = new draggable({ element: '.square' });draggableOptions
Section titled “draggableOptions”element
Section titled “element”Type:
type element = stringThe element selector.
restrictTo
Section titled “restrictTo”Type:
type restrictTo = string;Restricts the dragged element to another element. That way the dragged element won’t go out of the other element bounds.
Example
const square = new draggable({ element: '.square', restrictTo: '.container' });dragClass
Section titled “dragClass”Type:
type dragClass = stringClass to be added to the dragged element.
lockAxis
Section titled “lockAxis”Type:
type lockAxis = 'x' | 'y'Locks the dragged element to either the x or y axis.
Example
const square1 = new draggable({ element: '.square1', lockAxis: 'x' });const square2 = new draggable({ element: '.square2', lockAxis: 'y' });onDragStart
Section titled “onDragStart”Type:
type onDragStart = (element: HTMLElement) => void;Executes when you start dragging the element.
onDragMove
Section titled “onDragMove”Type:
type onDragMove = (position: { x: number; y: number }) => void;Executes when you move the dragged element.
onDragEnd
Section titled “onDragEnd”Type:
type onDragEnd = (element: HTMLElement) => void;Executes when you stop dragging the element.
Properties
Section titled “Properties”actionName readonly
Section titled “actionName ”Type:
type actionName = stringThe name of the action that can be used to programmatically drag the element.
touchEnabled
Section titled “touchEnabled”Type:
type touchEnabled = booleanEnables or disables touch events. Default is false.
Methods
Section titled “Methods”deinit()
Section titled “deinit()”Removes the event listeners and disables the draggable functionality.
Actions
Section titled “Actions”You are able to drag elements using actions. Since every draggable action is unique you can do the following:
const element = new draggable({ element: '.square'});
actions.execute(element.actionName, {x: 100, y: 100, index: 0});You will need to pass the x and y coordinates where you want the element to go and the index of the element.