Okay it would seem like it should be simple. I need to take an already existing div and move it according to mouse position within the window. I have searched everywhere and
jquery is much easier to deploy. I am surprised you say you don't want to learn it.
You can save the jquery file in your local computer so you do not need internet to use jquery features.
In my case i have saved it in tools folder. So i do not need to be on internet.
For all the js many lines of js code answered above you only need one small line.
<script src="/common/tools/jquery-1.10.2.js"></script>
<script src="/common/tools/jquery-ui.js"></script>
<script>
$(function() {
$( "#mydiv_to_make_draggable" ).draggable();
});
</script>
All other answers (including the accepted one) do not work with touch inputs. Touch inputs have events different than that of mouse inputs. See Using Touch Events on MDN.
The following code snippet works even with touch inputs. I have highlighted all lines of code that need to be added to support touch devices.
The basic idea here is that every element containing draggable
in the class list should be draggable. This concept is easier to follow when you have a big number of elements that need to be dragged.
See this Glitch page and following for a demo.
const d = document.getElementsByClassName("draggable");
for (let i = 0; i < d.length; i++) {
d[i].style.position = "relative";
}
function filter(e) {
let target = e.target;
if (!target.classList.contains("draggable")) {
return;
}
target.moving = true;
//NOTICE THIS
I just made a small change to the @adeneo very well working answer. If everything is enclosed in a function, and every event is attached to the div, you can use it as part of a library.
Call the following function passing an id. If the div does not exist it is created.
function drag_div(div_id){
var div;
div = document.getElementById(div_id);
if(div == null){
div = document.createElement("div");
div.id = div_id;
div.style.position = "absolute";
div.style.left = "0px";
div.style.top = "0px";
div.style.width = "100px";
div.style.height = "100px";
div.style.background = "red";
div.style.color = "blue";
document.body.appendChild(div);
}
div.addEventListener('mousedown', function(e) {
div.isDown = true;
div.offset = [
div.offsetLeft - e.clientX,
div.offsetTop - e.clientY
];
}, true);
div.addEventListener('mouseup', function() {
div.isDown = false;
}, true);
div.addEventListener('mousemove', function(event) {
event.preventDefault();
if (div.isDown) {
div.mousePosition = {
x : event.clientX,
y : event.clientY
};
div.style.left = (div.mousePosition.x + div.offset[0]) + 'px';
div.style.top = (div.mousePosition.y + div.offset[1]) + 'px';
}
}, true);
}
I think you're looking for something more like this
var mousePosition;
var offset = [0,0];
var div;
var isDown = false;
div = document.createElement("div");
div.style.position = "absolute";
div.style.left = "0px";
div.style.top = "0px";
div.style.width = "100px";
div.style.height = "100px";
div.style.background = "red";
div.style.color = "blue";
document.body.appendChild(div);
div.addEventListener('mousedown', function(e) {
isDown = true;
offset = [
div.offsetLeft - e.clientX,
div.offsetTop - e.clientY
];
}, true);
document.addEventListener('mouseup', function() {
isDown = false;
}, true);
document.addEventListener('mousemove', function(event) {
event.preventDefault();
if (isDown) {
mousePosition = {
x : event.clientX,
y : event.clientY
};
div.style.left = (mousePosition.x + offset[0]) + 'px';
div.style.top = (mousePosition.y + offset[1]) + 'px';
}
}, true);
FIDDLE
Check if this is smoother than adeneo: FIDDLE
var m = document.getElementById('move');
m.addEventListener('mousedown', mouseDown, false);
window.addEventListener('mouseup', mouseUp, false);
function mouseUp() {
window.removeEventListener('mousemove', move, true);
}
function mouseDown(e) {
window.addEventListener('mousemove', move, true);
}
function move(e) {
m.style.top = e.clientY + 'px';
m.style.left = e.clientX + 'px';
};
You can use this one as a library. Works perfectly. I found it on github but it was getting stuck sometimes because the sharer put "mouseup" to element. I changed it to document and it fixed the problem. This is fixed version
'use strict';
/**
* Makes an element draggable.
*
* @param {HTMLElement} element - The element.
*/
function draggable(element) {
var isMouseDown = false;
// initial mouse X and Y for `mousedown`
var mouseX;
var mouseY;
// element X and Y before and after move
var elementX = 0;
var elementY = 0;
// mouse button down over the element
element.addEventListener('mousedown', onMouseDown);
/**
* Listens to `mousedown` event.
*
* @param {Object} event - The event.
*/
function onMouseDown(event) {
mouseX = event.clientX;
mouseY = event.clientY;
isMouseDown = true;
}
// mouse button released
document.addEventListener('mouseup', onMouseUp);
/**
* Listens to `mouseup` event.
*
* @param {Object} event - The event.
*/
function onMouseUp(event) {
isMouseDown = false;
elementX = parseInt(element.style.left) || 0;
elementY = parseInt(element.style.top) || 0;
}
// need to attach to the entire document
// in order to take full width and height
// this ensures the element keeps up with the mouse
document.addEventListener('mousemove', onMouseMove);
/**
* Listens to `mousemove` event.
*
* @param {Object} event - The event.
*/
function onMouseMove(event) {
if (!isMouseDown) return;
var deltaX = event.clientX - mouseX;
var deltaY = event.clientY - mouseY;
element.style.left = elementX + deltaX + 'px';
element.style.top = elementY + deltaY + 'px';
}
}