防止可拖动的div在拖动时离开容器div



我有一个id为mydiv的可拖动div嵌套在id为container的容器div中。当我拖动mydiv时,我希望它留在里面,而不是离开容器div。我该怎么做?

这是代码:

//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;
// 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;
}
}
#container {
border: 1px solid red;
height: 300px;
width: 400px;
}
#mydiv {
position: absolute;
z-index: 9;
background-color: #f1f1f1;
text-align: center;
border: 1px solid #d3d3d3;
}
#mydivheader {
padding: 10px;
cursor: move;
z-index: 10;
background-color: #2196F3;
color: #fff;
}
<h1>Draggable DIV Element</h1>
<p>Click and hold the mouse button down while moving the DIV element</p>
<div id="container">
<div id="mydiv">
<div id="mydivheader">Click here to move</div>
<p>Move</p>
<p>this</p>
<p>DIV</p>
</div>
</div>

我认为解决方案是在elementDrag(e)函数中添加一些if语句,但我不确定。我希望修改现有的代码,而不是一个全新的解决方案。请仅使用纯JavaScript。

以下是我解决它的方法。

我从DOM中获得#container,并将其保存到一个变量中

我给了容器一个position: relative声明,这样盒子就可以相对于容器定位

我将元素上的所有onevents更改为原生addEventListeners,并删除了所有这些变量(pos1, pos2, pose3, pos4),因为我不再使用它们

mouseup事件在文档上,mousemove在容器上,以确保盒子不会离开容器。

这些计算(e.clientY - containerY)(e.clientX - containerX)得到鼠标相对于容器的绝对位置。我们在容器元素上从getBoundingClientRect()得到containerXcontainerY

const container = document.getElementById('container');
dragElement(document.getElementById("mydiv"));
function dragElement(elmnt) {
const {
x: containerX,
y: containerY
} = container.getBoundingClientRect();
if (document.getElementById(elmnt.id + "header")) document.getElementById(elmnt.id + "header").addEventListener('mousedown', dragMouseDown);
else elmnt.addEventListener('mousedown', dragMouseDown);
function dragMouseDown(e) {
e = e || window.event;
e.preventDefault();
document.addEventListener('mouseup', closeDragElement);
container.addEventListener('mousemove', elementDrag);
}
function elementDrag(e) {
e = e || window.event;
e.preventDefault();
elmnt.style.top = (e.clientY - containerY) + "px";
elmnt.style.left = (e.clientX - containerX) + "px";
}
function closeDragElement() {
document.removeEventListener('mouseup', closeDragElement);
container.removeEventListener('mousemove', elementDrag);
}
}
#container {
position: relative; /* This is very important */
border: 1px solid red;
height: 300px;
width: 400px;
overflow: hidden;
}
#mydiv {
width: 144px; /* This is very important */
position: absolute;
z-index: 9;
background-color: #f1f1f1;
text-align: center;
border: 1px solid #d3d3d3;
}
#mydivheader {
padding: 10px;
cursor: move;
z-index: 10;
background-color: #2196F3;
color: #fff;
}
<h1>Draggable DIV Element</h1>
<p>Click and hold the mouse button down while moving the DIV element</p>
<div id="container">
<div id="mydiv">
<div id="mydivheader">Click here to move</div>
<p>Move</p>
<p>this</p>
<p>DIV</p>
</div>
</div>

最新更新