
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to create a draggable HTML element with JavaScript and CSS?
To create a draggable HTML element with JavaScript and CSS, the code is as follows −
Example
<!DOCTYPE html> <html> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .dragDiv { position: absolute; z-index: 9; text-align: center; border: 1px solid #d3d3d3; padding: 30px; cursor: move; z-index: 10; background-color: rgb(108, 24, 177); color: #fff; font-size: 20px; font-weight: 500; } </style> <body> <h1>Draggable DIV Element Example</h1> <h2>Click and drag the below element to move it around</h2> <div class="dragDiv"> This div can be moved around </div> <script> dragElement(document.querySelector(".dragDiv")); function dragElement(ele) { var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0; if (document.querySelector(ele.id + "header")) { document.getElementById( ele.id + "header" ).onmousedown = dragMouseDown; } else { ele.onmousedown = dragMouseDown; } function dragMouseDown(e) { e = e || window.event; e.preventDefault(); pos3 = e.clientX; pos4 = e.clientY; document.onmouseup = closeDragElement; document.onmousemove = elementDrag; } function elementDrag(e) { e = e || window.event; e.preventDefault(); pos1 = pos3 - e.clientX; pos2 = pos4 - e.clientY; pos3 = e.clientX; pos4 = e.clientY; ele.style.top = ele.offsetTop - pos2 + "px"; ele.style.left = ele.offsetLeft - pos1 + "px"; } function closeDragElement() { document.onmouseup = null; document.onmousemove = null; } } </script> </body> </html>
Output
The above code will produce the following output −
On moving the div around by dragging −
- Related Articles
- How to create a signup form with HTML and CSS?
- How to create a sticky element with CSS?
- How to Create a Color Picker using HTML, CSS, and JavaScript?
- How to Create a Parallax Effect using HTML, CSS, and JavaScript?
- How to create a revealing sidebar using HTML, CSS, and JavaScript?
- How to Create a Binary Calculator using HTML, CSS and JavaScript?
- How to create Expanding Cards using HTML, CSS, and JavaScript
- How to set whether an element is draggable or not in HTML?
- How to create a responsive Image Grid with HTML and CSS?
- How to create a valid HTML document with no and element?
- How to Create a Profit and Loss Calculator using HTML, CSS, and JavaScript?
- How to create tabs with CSS and JavaScript?
- How to create popups with CSS and JavaScript?
- How to create a responsive slideshow with CSS and JavaScript?
- How to create a Modal Box with CSS and JavaScript?

Advertisements