Reputation:
I want to make some funny movable divs. I have the following codes:
dragElement(document.getElementById("draggable_shortcut"));
function dragElement(elmnt) {
var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
if (document.getElementById(elmnt.id + "top-content")) {
/* if present, the header is where you move the DIV from:*/
document.getElementById(elmnt.id + "top-content").onmousedown = dragMouseDown;
} else {
/* otherwise, move the DIV from anywhere inside the DIV:*/
elmnt.onmousedown = dragMouseDown;
}
function dragMouseDown(e) {
e = e || window.event;
e.preventDefault();
// get the mouse cursor position at startup:
pos3 = e.clientX;
pos4 = e.clientY;
document.onmouseup = closeDragElement;
// call a function whenever the cursor moves:
document.onmousemove = elementDrag;
}
function elementDrag(e) {
e = e || window.event;
e.preventDefault();
// calculate the new cursor position:
pos1 = pos3 - e.clientX;
pos2 = pos4 - e.clientY;
pos3 = e.clientX;
pos4 = e.clientY;
// set the element's new position:
elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";
}
function closeDragElement() {
/* stop moving when mouse button is released:*/
document.onmouseup = null;
document.onmousemove = null;
}
}
#draggable_shortcut {
position: absolute;
z-index: 9;
text-align: center;
cursor: grab;
}
#draggable_shortcut img {
width: 50px;
height: 50px;
}
#draggable_shortcut p {
color: black;
font-size: 14px;
margin: 0px;
}
<div id="draggable_shortcut">
<img src="https://images.ecosia.org/F9NhZWwmi8VL4EI6ylXOrAhWob4=/0x390/smart/https%3A%2F%2Fmaxcdn.icons8.com%2FShare%2Ficon%2Fultraviolet%2FVery_Basic%2Fidea1600.png">
<p>Moveable Icon</p>
</div>
It works really god without issues. If I add some more divs, the script doesn't work anymore, because it's just for one and not more. But I want to make more than one div, which are movable.
I'm new in JavaScript Programming. Any ideas?
Thank you for the ideas and scripts.
Not my last question about this script:
Is it possible to make, if the user drops an div, the position should be setted on hundred px?
For example, if the user drops the div on the coordinates (120/105) it should get moved to (100/100). Another exmaple: (170/355) -> (200/400).
If possible, would be nice if an user can change, he would want with the hundred corrds or cutsom like this before.
Update 12.11.2018:
I found now a possibility to check the coords. But it will be only placed if the coords are exaclty 100 and not like 105. Any ideas? Demo here:
http://next.plnkr.co/edit/fBWlF0B3t4XbjuSW
Update 12.11.2018 Later...
I found now a possibillity. For these, who want the same: http://next.plnkr.co/edit/fBWlF0B3t4XbjuSW
Upvotes: 1
Views: 278
Reputation: 68933
Try with class instead of id.
You have to call the function for each element individually. You can use Document.querySelectorAll() to get a static (not live) NodeList representing a list of the document's elements. Then use Array.prototype.forEach() to call the function for each element:
document.querySelectorAll(".draggable_shortcut").forEach(function(el){
dragElement(el);
});
function dragElement(elmnt) {
var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
if (document.getElementById(elmnt.id + "top-content")) {
/* if present, the header is where you move the DIV from:*/
document.getElementById(elmnt.id + "top-content").onmousedown = dragMouseDown;
} else {
/* otherwise, move the DIV from anywhere inside the DIV:*/
elmnt.onmousedown = dragMouseDown;
}
function dragMouseDown(e) {
e = e || window.event;
e.preventDefault();
// get the mouse cursor position at startup:
pos3 = e.clientX;
pos4 = e.clientY;
document.onmouseup = closeDragElement;
// call a function whenever the cursor moves:
document.onmousemove = elementDrag;
}
function elementDrag(e) {
e = e || window.event;
e.preventDefault();
// calculate the new cursor position:
pos1 = pos3 - e.clientX;
pos2 = pos4 - e.clientY;
pos3 = e.clientX;
pos4 = e.clientY;
// set the element's new position:
elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";
}
function closeDragElement() {
/* stop moving when mouse button is released:*/
document.onmouseup = null;
document.onmousemove = null;
}
}
.draggable_shortcut {
position: absolute;
z-index: 9;
text-align: center;
cursor: grab;
}
.draggable_shortcut img {
width: 50px;
height: 50px;
}
.draggable_shortcut p {
color: black;
font-size: 14px;
margin: 0px;
}
<div class="draggable_shortcut">
<img src="https://images.ecosia.org/F9NhZWwmi8VL4EI6ylXOrAhWob4=/0x390/smart/https%3A%2F%2Fmaxcdn.icons8.com%2FShare%2Ficon%2Fultraviolet%2FVery_Basic%2Fidea1600.png">
<p>Moveable Icon</p>
</div>
<div class="draggable_shortcut">
<img src="https://images.ecosia.org/F9NhZWwmi8VL4EI6ylXOrAhWob4=/0x390/smart/https%3A%2F%2Fmaxcdn.icons8.com%2FShare%2Ficon%2Fultraviolet%2FVery_Basic%2Fidea1600.png">
<p>Moveable Icon</p>
</div>
Upvotes: 2
Reputation: 1487
You need to make loop for each item.
var elements = document.getElementsByClassName("drag");
for(var i=0; i<elements.length; i++) {
console.log(elements[i].id)
dragElement(document.getElementById(elements[i].id));
function dragElement(elmnt) {
var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
if (document.getElementById(elmnt.id + "top-content")) {
/* if present, the header is where you move the DIV from:*/
document.getElementById(elmnt.id + "top-content").onmousedown = dragMouseDown;
} else {
/* otherwise, move the DIV from anywhere inside the DIV:*/
elmnt.onmousedown = dragMouseDown;
}
function dragMouseDown(e) {
e = e || window.event;
e.preventDefault();
// get the mouse cursor position at startup:
pos3 = e.clientX;
pos4 = e.clientY;
document.onmouseup = closeDragElement;
// call a function whenever the cursor moves:
document.onmousemove = elementDrag;
}
function elementDrag(e) {
e = e || window.event;
e.preventDefault();
// calculate the new cursor position:
pos1 = pos3 - e.clientX;
pos2 = pos4 - e.clientY;
pos3 = e.clientX;
pos4 = e.clientY;
// set the element's new position:
elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";
}
function closeDragElement() {
/* stop moving when mouse button is released:*/
document.onmouseup = null;
document.onmousemove = null;
}
}
}
Also change id
to classes
.drag {
position: absolute;
z-index: 9;
text-align: center;
cursor: grab;
}
.drag img {
width: 50px;
height: 50px;
}
.drag p {
color: black;
font-size: 14px;
margin: 0px;
}
Now each div
must have individual id
and the same class
Working plunker: http://plnkr.co/edit/R8PUSaxkWebiZoq1XraC?p=preview
Upvotes: 0