原始 w3schools 示例
您可以像链接的 w3schools 示例中提到的那样使用 position: absolute,但无需为绝对定位的元素定义 top、left 等,它们都具有相同的默认值:top: 0 和 left: 0。因此它们都出现在同一个位置,这导致彼此堆叠。
如果为每个绝对定位的元素为top、left 等定义不同的值,则可以防止堆叠。例如:
#mydiv2 {
top: 170px;
}
这是针对您最初发布的问题的解决方案,其中包含两个硬编码的可拖动元素,但它不适合动态添加的内容。
工作示例:
为简单起见,我省略了函数dragElement() 中的第一个if ... else 块,并将事件侦听器直接附加到整个元素:
elmnt.onmousedown = dragMouseDown;
//Make the DIV element draggagle:
dragElement(document.getElementById("mydiv2"));
dragElement(document.getElementById("mydiv"));
function dragElement(elmnt) {
var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
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;
}
}
#mydiv,
#mydiv2 {
position :absolute;
z-index: 9;
background-color: #f1f1f1;
text-align: center;
cursor: move;
border: 1px solid #d3d3d3;
}
#mydiv2 {
top: 170px;
}
#mydivheader,
#mydivheader2 {
padding: 10px;
z-index: 10;
background-color: #2196F3;
color: #fff;
}
<div class="container">
<div class="row">
<div id="mydiv" class="col-sm-10">
<div id="mydivheader">Click here to move</div>
<p>Move</p>
<p>this</p>
<p>DIV</p>
</div>
<div id="mydiv2" class="col-sm-10">
<div id="mydivheader2">Click here to move</div>
<p>Move</p>
<p>this</p>
<p>DIV</p>
</div>
</div>
</div>
使用transform: translate()
基本实现
对于动态添加的内容(或任何其他不使用top、left 等的原因),最好使用transform: translate() 并省略position: absolute,以便元素保持在原位并且可以不要堆积。
要实现这一点,您只需从旧的translate-values 中减去原始计算,然后用该减法的值覆盖(以保存它们以供下一个函数调用):
pos1 = pos1 - (pos3 - e.clientX);
pos2 = pos2 - (pos4 - e.clientY);
最后,您必须将出现样式的两行从 top 和 left 更改为带有 translate 的一行:
elmnt.style.transform = 'translate(' + (pos1) + "px, " + (pos2) + "px)";
工作示例:
我将过时的var 替换为let,因为变量会改变。
//Make the DIV element draggagle:
dragElement(document.getElementById("mydiv2"));
dragElement(document.getElementById("mydiv"));
function dragElement(elmnt) {
let pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
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 = pos1 - (pos3 - e.clientX);
pos2 = pos2 - (pos4 - e.clientY);
pos3 = e.clientX;
pos4 = e.clientY;
// set the element's new position:
elmnt.style.transform = 'translate(' + (pos1) + "px, " + (pos2) + "px)";
}
function closeDragElement() {
// stop moving when mouse button is released:
document.onmouseup = null;
document.onmousemove = null;
}
}
#mydiv,
#mydiv2 {
width: 160px;
background-color: #f1f1f1;
text-align: center;
cursor: move;
border: 1px solid #d3d3d3;
}
#mydivheader,
#mydivheader2 {
padding: 10px;
background-color: #2196F3;
color: #fff;
}
<div class="container">
<div class="row">
<div id="mydiv" class="col-sm-10">
<div id="mydivheader">Click here to move</div>
<p>Move</p>
<p>this</p>
<p>DIV</p>
</div>
<div id="mydiv2" class="col-sm-10">
<div id="mydivheader2">Click here to move</div>
<p>Move</p>
<p>this</p>
<p>DIV</p>
</div>
</div>
</div>
口语变量
w3schools 示例中的变量名称pos1, pos2, pos3, pos4 不是很直观。因此,应将它们重命名以便于理解它们的用途:translate = { x: 0, y: 0 } 和 client = { x: 0, y: 0 }。
工作示例:
//Make the DIV element draggagle:
dragElement(document.getElementById("mydiv2"));
dragElement(document.getElementById("mydiv"));
function dragElement(elmnt) {
let translate = { x: 0, y: 0 };
let client = { x: 0, y: 0 };
elmnt.onmousedown = dragMouseDown;
function dragMouseDown(e) {
e = e || window.event;
e.preventDefault();
// get the mouse cursor position at startup:
client.x = e.clientX;
client.y = 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:
translate.x = translate.x - (client.x - e.clientX);
translate.y = translate.y - (client.y - e.clientY);
client.x = e.clientX;
client.y = e.clientY;
// set the element's new position:
elmnt.style.transform = 'translate(' + (translate.x) + "px, " + (translate.y) + "px)";
}
function closeDragElement() {
// stop moving when mouse button is released:
document.onmouseup = null;
document.onmousemove = null;
}
}
#mydiv,
#mydiv2 {
width: 160px;
border: 1px solid #d3d3d3;
text-align: center;
cursor: move;
background-color: #f1f1f1;
}
#mydivheader,
#mydivheader2 {
padding: 10px;
background-color: #2196F3;
color: #fff;
}
<div class="container">
<div class="row">
<div id="mydiv" class="col-sm-10">
<div id="mydivheader">Click here to move</div>
<p>Move</p>
<p>this</p>
<p>DIV</p>
</div>
<div id="mydiv2" class="col-sm-10">
<div id="mydivheader2">Click here to move</div>
<p>Move</p>
<p>this</p>
<p>DIV</p>
</div>
</div>
</div>
最终实现
为了演示您后来添加的用于动态添加可拖动元素的函数的代码,这里是最终实现。我将过时的var 替换为const,因为变量不会改变。
工作示例:
const d = ["module1", "module2", "module3", "module4", "module5"];
for(i in d) {
append_module_name_to_drag_div(d[i]);
dragElement(document.getElementById(d[i]));
}
function append_module_name_to_drag_div(module_name) {
const mydiv = document.createElement("div");
mydiv.id = module_name;
mydiv.className = 'module col-sm-10';
const mydivmarker = document.createElement("div");
mydivmarker.id = module_name+"header";
mydivmarker.className = 'mydivheader';
const marker_image = new Image();
marker_image.src = "http://www.clker.com/cliparts/w/O/e/P/x/i/map-marker-hi.png";
marker_image.height = 45;
marker_image.width = 35;
mydivmarker.append(marker_image);
mydiv.innerHTML = module_name;
mydiv.append(mydivmarker);
document.getElementById("markers").appendChild(mydiv);
}
function dragElement(elmnt) {
let translate = { x: 0, y: 0 };
let client = { x: 0, y: 0 };
elmnt.onmousedown = dragMouseDown;
function dragMouseDown(e) {
e = e || window.event;
e.preventDefault();
// get the mouse cursor position at startup:
client.x = e.clientX;
client.y = 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:
translate.x = translate.x - (client.x - e.clientX);
translate.y = translate.y - (client.y - e.clientY);
client.x = e.clientX;
client.y = e.clientY;
// set the element's new position:
elmnt.style.transform = 'translate(' + (translate.x) + "px, " + (translate.y) + "px)";
}
function closeDragElement() {
// stop moving when mouse button is released:
document.onmouseup = null;
document.onmousemove = null;
}
}
.module {
width: 80px;
border: 1px solid #d3d3d3;
text-align: center;
cursor: move;
background-color: #f1f1f1;
}
.mydivheader {
padding: 10px;
background-color: #2196F3;
color: #fff;
}
<div class="container">
<div id="markers" class="row">
</div>
</div>