gpt4 book ai didi

使用 JavaScript 的 JavaScript 可移动 Div

转载 作者:行者123 更新时间:2023-11-30 14:17:56 29 4
gpt4 key购买 nike

我想制作一些有趣的可移动 div。我有以下代码:

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>

它真的很好用,没有问题。如果我再添加一些 div,脚本就不再起作用了,因为它只是为了一个而不是更多。但是我想制作多个可移动的div。

我是 JavaScript 编程的新手。有什么想法吗?


感谢您提供的想法和脚本。这不是我关于这个脚本的最后一个问题:是否有可能使,如果用户放下一个div,位置应该设置为百像素?

例如,如果用户将div放在坐标(120/105)上,它应该得到移动到 (100/100)。另一个例子:(170/355) -> (200/400)。

如果可能的话,如果一个用户可以改变就好了,他会想要像以前那样的一百个 corrds 或 cutsom。

2018 年 11 月 12 日更新:
我发现现在可以检查坐标。但只有当坐标精确到 100 而不是 105 时才会放置它。有什么想法吗?演示在这里: http://next.plnkr.co/edit/fBWlF0B3t4XbjuSW

12.11.2018 稍后更新...
我现在发现了一个可能性。对于那些想要相同的人:http://next.plnkr.co/edit/fBWlF0B3t4XbjuSW

最佳答案

尝试使用 class 而不是 id

您必须分别为每个元素调用该函数。您可以使用 Document.querySelectorAll()获取表示文档元素列表的静态(非实时)NodeList。然后使用 Array.prototype.forEach()为每个元素调用函数:

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>

关于使用 JavaScript 的 JavaScript 可移动 Div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53247852/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com