I am trying to drag an element a little way on a mousedown event, so that it reveals a new item which will be drawn underneath. But when I try a move, it jumps to all the way to the right side of the window. How do I make it move to where I wanted? I have tried changing positions from ‘relative’ to ‘absolute’ , I have tried setting the new position to a fixed x and y, and relative to the mouse event; they all give the same result.
Something wrong with my basic understanding here. As background, I have been using vue.js
quite a lot, to avoid getting too involved with dom manipulation myself (as I’m not too experienced!), and came across this problem in a vue project. Initially I thought it was a problem with Vue, but I’ve now shown it not to be.
document.getElementById("red").addEventListener("mousedown", mouseDown);
function mouseDown(e) {
const el = e.target;
// const x = e.pageX;
// const y = e.pageY;
const rect = el.getBoundingClientRect();
const x = rect.left + 20;
const y = rect.top + 20;
el.style.left = `${x}px`
el.style.top = `${y}px`
const newrect = el.getBoundingClientRect();
document.getElementById("from").innerHTML = "drag from: " + rect.left + ", " + rect.top;
document.getElementById("to").innerHTML = "try to drag to: " + x + ", " + y;
document.getElementById("result").innerHTML = "dragged to: " + + newrect.left + ", " + newrect.top;
}
.box {
position:relative;
margin: auto;
border: 3px solid #73AD21;
width: 200px;
height: 250px;
}
.button {
position:relative;
background-color: red;
color: white;
padding: 10px 20px;
text-align: center;
width: 100px;
font-size: 16px;
cursor: pointer;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="icon" href="/favicon.ico">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Drag Drop Test</title>
</head>
<body>
<div id="app" align="center" class="box">
<button id="red" class="button" >Red</button>
<p id="from"></p>
<p id="to"></p>
<p id="result"></p>
</div>
</body>
</html>
2
Answers
Simplifying @Mol nAK's answer for my specific requirement, I just need the following:
The button is moving to the extreme right because in your code here:
since in your CSS you have made both button and box to be relative, as soon as you push the button 20px from left and top, and since you haven’t considered the dimensions of the parent element of the button which is the box in this case, the button moves to the farthest right. If you want to keep the button inside the box, then you have to get the positions of the button relative to the parent element and not the whole viewport also the position of button should be absolute as you have made the postion of box to be relative.
This is my code to achieve what I presume you want, a draggable button inside the box only. Hope this helps.