When using the .draggableElement method on an element, a JSON object can be passed as an argument to instantiate the draggable element with certain settings that affect its behavior. Below are some key-value pairs that can be included in the JSON object. Any keys not in the list below will be ignored. Note that keys are case-sensitive and always appear in camelCase format.
bounds |
Type: |
Array, Element, or String |
Default: |
[-Infinity, -Infinity, Infinity, Infinity] |
Desc: |
Determines the bounding box for the draggable element to remain in while being dragged.‣ If Array, must be of the form [left, top, right, bottom], where each array element is a number in pixels from the top of the top left of the document. Note that resizing by the user may cause this bounding box to shrink or grow more than you want since each number is measured in absolute pixels. In this case, add a resize event listener in your code to accommodate for this and update the bounds to the necessary amount.‣ If Element, the bounding box of the element will be used as the boundaries (using the .getBoundingClientRect method). Window resizing is taken into account: when the window is resized, the boundaries are updated and the element is moved if necessary to keep it inside the bounds. To ensure that the element is exactly where you want it however, it is recommended to add your own resize listener to update the element's position manually.‣ If String, must be one of two values: "parent" or "body". "parent" will be evaluated as the element's parent node and prevents it from leaving the parent's bounding box. "body" will be evaluated as document.body and prevents it from going off screen or expanding an existing element's size. |
|
css |
Type: |
Object |
Default: |
{} |
Desc: |
Defines any CSS that should be applied to the element while it is being dragged. Use {cssProperty: valueForThatProperty} to define any CSS changes. After being released, it will return to its old CSS, which is gathered when the draggable element is first instantiated. To update its CSS that it should return to, use the .updateCSS method on the element. The following CSS properties are ignored: top, left, bottom, right, position, and cursor. To alter the cursor to be used, use the cursor option. |
|
cursor |
Type: |
String or Boolean |
Default: |
"move" , "ns-resize" , or "ew-resize" depending on the active axes |
Desc: |
Defines what cursor should appear on the element while being dragged. The entire <html> element also receives this cursor style. After the element is released, the default cursors are restored for both the element and the <html> element.‣ If String, that string is used for the cursor. Click here for a list of cursors that can be applied. If the string is not any of these, the cursor style will not be applied.‣ If Boolean, defines whether the cursor should be changed at all. false will not alter the cursor styling at all while true will use the default values. |
|
dependents |
Type: |
Array, NodeList, HTMLCollection, or Element |
Default: |
[] |
Desc: |
Defines any elements that should be moved alongside the draggable element and mirror its movements. Each time the draggable element is moved, its dependents also move the same amount. Dependents can be outside of the draggable element's bounding box and will still mirror the movement of the draggable element. Note that on window resizing, the dependents are moved the same amount that the draggable elements are moved. It is recommended that you add your own resizing event handler to manually position the dependents to the correct location. |
|
doX |
Type: |
Boolean |
Default: |
true |
Desc: |
Indicates whether or not the element should be draggable horizontally. If false, the element will have an "ns-resize" cursor and will not be able to move side to side. |
|
doY |
Type: |
Boolean |
Default: |
true |
Desc: |
Indicates whether or not the element should be draggable vertically. If false, the element will have an "ew-resize" cursor and will not be able to move up and down. |
|
inertia |
Type: |
Boolean |
Default: |
false |
Desc: |
Indicates whether or not the element will have inertia. See Inertia for more information on the behavior of this option. |
|
inertiaCollision |
Type: |
String |
Default: |
"bounce" |
Desc: |
Dictates what happens when the element collides with a boundary while in its inertia state. If the user is still dragging and hasn't released the element (i.e. the element is still in its dragging state), the element will remain at the cursor's position. If inertia is disabled, this option has no effect.‣ If "bounce", the element will bounce off the wall in the opposite direction it collided in. Works similar to how a mirror reflects light.InertiaCollision>Slide">‣ If "slide", the element will slide along the wall it collided into. Works similar to "bounce" except the element will stay adjacent to the wall.‣ If "stop", the element will completely halt once it collides with a boundary. If a 'done' callback is applied, the callback will be invoked right when it collides. |
|
inertiaResistance |
Type: |
Number |
Default: |
1 |
Desc: |
Affects how resistant the element is to slowing down while in its inertia state. If inertia is disabled, this option has no effect. The number must be in a range between 02.5. 0 is the same as having no inertia; the element will stop as soon as it is released. 2.5 is the same as having no friction to slow the element; it will move at the same speed forever or until grabbed again by the user. If the number is outside the range, it will automatically be put into the range depending on which number it is closest to. See Inertia for more information on how resistance affects the element's behavior. |
|
inertiaThreshold |
Type: |
Number |
Default: |
5 |
Desc: |
After the user releases an element, all the movements in the last 120 milliseconds are gathered and evaluated. If at least one of their distances (measured using Chebyshev Distance) exceeds the inertiaThreshold, then inertia is applied to the element. Otherwise, the element will remain stationary after being released and the 'done' callback is not invoked. This is to prevent small movements from activating the inertia state. A higher inertiaThreshold will force the user to move faster to apply the inertia. |
|
keepSelection |
Type: |
Boolean |
Default: |
false |
Desc: |
When a draggable element is moved, by default the user may also accidentally highlight certain elements because they are dragging the cursor over them. If this property is set to false, the selection is removed each time the element is moved to prevent accidental highlighting. Set to true to keep the default highlighting behavior. |
|
init |
Type: |
Function |
Default: |
null |
Desc: |
Function to be called after the element has been instantiated. This callback function will only run once right after all the default properties for the new draggable element have been defined and it is ready to be dragged.Receives one argument: DragInitEvent. |
|
start |
Type: |
Function |
Default: |
null |
Desc: |
Function to be called each time the user starts a new drag. This callback function may be called more than once depending on how many times the user drags the element.Receives three arguments: DragStartEvent, the original mouse or touch event that started the drag, and a function callback. The callback, when called, will cancel the user's drag and prevent them from moving the element. No other callbacks are called and the element simply isn't allowed to drag until the user tries again. This allows you to decide in the start callback whether or not the user should be allowed to drag. |
|
move |
Type: |
Function |
Default: |
null |
Desc: |
Function to be called each time the user moves the draggable element at least one pixel. This function may be called hundreds of times per drag, or may not be called at all if the user doesn't move the element at least one pixel while dragging.Receives two arguments: DragMoveEvent, and the original mouse or touch move event that caused the element to be dragged. |
|
end |
Type: |
Function |
Default: |
null |
Desc: |
Function to be called once the user releases the draggable element and ends its dragging state. If the draggable element has no inertia applied to it, this is the last event that will be fired before starting again and calling the start callback. Otherwise, two other events are also fired as shown below.Receives three arguments: DragEndEvent, the original mouse or touch event that ended the dragging, and a callable function that will prevent inertia from starting if invoked. If inertia is not applied to the element, the function does nothing. The third argument is meant for you to allow, or disallow inertia from taking place, based on any calculations made in this function. |
|
frame |
Type: |
Function |
Default: |
null |
Desc: |
Function to be called for each "frame" of inertia that takes place about every 25 milliseconds. Each frame of inertia moves the element a certain distance and calls this function after the element has been moved. This function may be called hundreds of time if the user gave the element sufficient speed, or may not be called at all if inertia is never triggered. This callback function is only invoked if inertia is enabled for the element.Receives two arguments: DragFrameEvent, and a function that, when invoked, will end the inertia earlier than planned. This allows you to control how long inertia lasts based on any calculations made in this function. |
|
done |
Type: |
Function |
Default: |
null |
Desc: |
Function to be called after the element has finished its inertia state and is completely done moving. This function is only called once, even if inertia never actually took place. If this is the case, it is called immediately after the end event. This callback function is only invoked if inertia is enabled for the element.Receives one argument: DragDoneEvent. This argument contains information pertaining to whether the element actually entered its inertia state, whether the user grabbed the element again before it came to a complete stop, and other information about the element. |
|
Any keys not in the table above will be ignored when instantiating the draggable element. Any keys that are not of the correct type will either raise an error along the way, or just not function as they are supposed to.