A Tiny JavaScript Library to Make DOM Elements Drag & Drop | dragmove.js

A very small JavaScript library to make DOM elements draggable and movable. It has touch screen support. A tiny drag and drop library that makes any DOM draggable by clicking the handle and holding the mouse.

drag and drop example, javascript drag and drop example, html drag and drop example, html5 drag and drop multiple elements, drag and drop css, drag and drop javascript

How to make use of it:

1. Install the dragmove.js package with NPM.

# NPM
$ npm i @knadh/dragmove --save

2. Import the dragmove.js as an ES module.

import { dragmove } from @knadh/dragmove;

3. Add a drag handle to the aspect.

<div id="example">
  <div class="handle">Drag here</div>
</div>

4. Apply the drag and drop functionality to the aspect.

dragmove(document.querySelector(“#example”), document.querySelector(“#example .handle”))

5. Make the draggable aspect move inside the boundaries of the viewport.

<div id="example" data-drag-bound="true">
  <div class="handle">Drag here</div>
</div>

6. Use the start/end events to simulate a “snap to edge” behaviour.

const snapThreshold = 50;
function onStart(el, x, y) {
  // On drag start, remove the fixed bottom style to prevent the bottom
  // from sticking on the screen.
  el.style.top = el.offsetTop + "px"
  el.style.bottom = "auto"
}
function onEnd(el, x, y) {
  // Automatically snap to corners.
  if (window.innerHeight - (el.offsetTop + el.offsetHeight) < snapThreshold) {
      el.style.top = "auto"
      el.style.bottom = "0px"
  }
  if (window.innerWidth - (el.offsetLeft + el.offsetWidth) < snapThreshold) {
      el.style.left = "auto"
      el.style.right = "0px"
  }
  if (el.offsetTop < snapThreshold) {
      el.style.top = "0px"
  }
  if (el.offsetLeft < snapThreshold) {
      el.style.left = "0px"
  }
}
dragmove(document.querySelector("#example"), document.querySelector("#example .handle"), onStart, onEnd)

Tiny Drag To Move JavaScript Library, Drag Move JS Plugin/Github, jquery drag and drop


See Demo And Download

Official Website(knadh): Click Here

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

Related Posts

drag-drop-file-5x5-jq-uploader

Responsive Drag and Drop File Uploader/Download Plugin | 5x5_jq_uploader

5x5_jq_uploader plugin can be used to instantly create a drop file area and file queue with a little bit of preparation. Bootstrap is used for responsive planning…

Bootstrap-Dark-Mode

Stylesheet For Implementing Dark Mode with Bootstrap

Bootstrap Dark Mode provides a style sheet and two texts that allow you to implement Dark Mode on your website. Initially loaded based on user preferences, can…

responsive-navigation-menu

Multi-purpose Navigation Menu for Javascript Library | jQuery Corenav

coreNavigation is a multipurpose navigation menu for a jquery based javascript library, comes with more style, and is easy to integrate. 11 Default Menu 2 Responsive Navigation…

Simple-Multi-Select-Dropdown-Pure-Vanilla-Javascript

Simple Multi-Select Dropdown Pure Vanilla Javascript | multiSelect.js

MultiSelect.js is a simple, clean, and progressive JavaScript library designed for easy integration with any type of project or system. The design was heavily influenced by the…

Confetti-Falling-Animation-Effect-party

Confetti Falling Animation Effect In JavaScript | party.js

Party.js is a JavaScript library to brighten user site experience with visual effects! Celebrate success with dom confetti! The library is written in TypeScript and compiled into…

how-to-create-popup-in-html-with-css

How To Create A Popup in HTML with CSS

How to create popup in html with css – Popup without JavaScript is an elegant pop-up panel component with an animated scale, written in CSS. Have you…