sách gpt4 ăn đã đi

使用 JavaScript 的 JavaScript 可移动 Div

In lại Tác giả: Walker 123 更新时间:2023-11-30 14:17:56 29 4
mua khóa gpt4 giày 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;
} khác {
/* 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;
}


Moveable Icon


它真的很好用,没有问题。如果我再添加一些 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

câu trả lời hay nhất

尝试使用 lớp học thay vì nhận dạng

您必须分别为每个元素调用该函数。您可以使用 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;
} khác {
/* 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;
}


Moveable Icon






Moveable Icon


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

29 4 0
Walker 123
Hồ sơ

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá taxi Didi miễn phí
Phiếu giảm giá taxi Didi
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress