I am trying to get coordinates of upper left corner of a dragable div inside parent div. I can get coordinates of mouse in parent div and this is done by a onmousemove tag. I wounder that is there any similar tag, for example onmove to use to get coordinates of a div. the code I used is below
<!DOCTYPE html>
<html>
<style>
#parent {
width: 148mm;
height: 160mm;
background-color: yellow;
position: relative;
<!--border: 3px solid #e5e5e5;-->
}
#mydiv {
position: absolute;
z-index: 9;
text-align: center;
border: 1px solid #d3d3d3;
}
#mydivheader {
padding: 10px;
cursor: move;
z-index: 10;
}
</style>
<body>
<div id="parent" style="float:left" onmouseout="clearCoor()">
<div id="mydiv">
<div id="mydivheader" onmove="showCoords(event)"><img src="bc.png" alt="image"></div>
</div>
</div>
<p id="demo"></p>
<script>
//Make the DIV element draggagle:
dragElement(document.getElementById("mydiv"));
function dragElement(elmnt) {
var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
if (document.getElementById(elmnt.id + "header")) {
/* if present, the header is where you move the DIV from:*/
document.getElementById(elmnt.id + "header").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;
let parentElement = elmnt.parentElement;
if(elmnt.offsetTop < 0){elmnt.style.top = "0px"; return;}
if(elmnt.offsetTop > (parentElement.offsetHeight - elmnt.offsetHeight)) {
elmnt.style.top = (parentElement.offsetHeight - elmnt.offsetHeight) + "px";
return;
}
if(elmnt.offsetLeft < 0){elmnt.style.left = "0px";return}
if(elmnt.offsetLeft > (parentElement.offsetWidth - elmnt.offsetWidth)){
elmnt.style.left = (parentElement.offsetWidth - elmnt.offsetWidth) + "px";
return;
}
// 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;
}
}
function showCoords(event) {
var x = event.clientX;
var y = event.clientY;
var coor = "X coords: " + x + ", Y coords: " + y;
document.getElementById("demo").innerHTML = coor;
}
function clearCoor() {
document.getElementById("demo").innerHTML = "";
}
</script>
</body>
</html>
I used tag onmove but it did not work.
If I understood correctly that you wish to get the coordinates of the actual draggable object whilst it is being dragged then perhaps getBoundingClientRect is what you seek?
I modified the original HTML to remove the need for IDs on elements as when you start concatenating strings to IDs to identify an element things are going a little astray. There are other far better methods to identify DOM elements and perhaps the most useful are querySelector and querySelectorAll which are both written below in shorthand notation using an arrow function for brevity.
The
getBoundingClientRectfunction has also been abbreviated toboxwhich is called within theshowCoordsfunction aslet obj=box(event.target);where theevent.targetis derived from the externally registered event listener, like so:Rather than
onmovethe event you need is actuallyonmousemovein this case - again assigned here with an external listener rather than the older, inline typeonmousemove=func(e)etc