gpt4 book ai didi

javascript - 如何防止可拖动元素移出边界?

转载 作者:行者123 更新时间:2023-11-28 02:23:04 25 4
gpt4 key购买 nike

我通过使用与 w3Schools 中的示例大致相同的代码制作了一个可拖动元素:

//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;
// 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;
// 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;
}
}

问题是我需要将元素限制在视口(viewport)内并加上少量填充。我有一些解决方案,您只需检查元素位置是否小于 0,是否将其设置回 0,或者是否大于宽度/高度,将其设置为宽度/高度。这还不够好,因为它会产生一种我不想要的 janky 反弹效果。

所以我想知道是否有更明显的解决方案来解决这个问题?也不使用 jQuery。

最佳答案

您需要跟踪可用的视口(viewport)(调整窗口大小可能会改变它)并确保元素不会越过边界。如果该元素将越过边界,您应该什么也不做,而不是让它越过然后再将其放回去。下面是一个工作示例。

请注意,来自 w3schools 的代码示例在其风格和功能使用方面非常过时 - 在现代 HTML 中有更优雅的方式来支持拖动。

var PADDING = 8;

var rect;
var viewport = {
bottom: 0,
left: 0,
right: 0,
top: 0
}

//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;
// get the mouse cursor position at startup:
pos3 = e.clientX;
pos4 = e.clientY;

// store the current viewport and element dimensions when a drag starts
rect = elmnt.getBoundingClientRect();
viewport.bottom = window.innerHeight - PADDING;
viewport.left = PADDING;
viewport.right = window.innerWidth - PADDING;
viewport.top = PADDING;

document.onmouseup = closeDragElement;
// call a function whenever the cursor moves:
document.onmousemove = elementDrag;
}

function elementDrag(e) {
e = e || window.event;
// calculate the new cursor position:
pos1 = pos3 - e.clientX;
pos2 = pos4 - e.clientY;
pos3 = e.clientX;
pos4 = e.clientY;

// check to make sure the element will be within our viewport boundary
var newLeft = elmnt.offsetLeft - pos1;
var newTop = elmnt.offsetTop - pos2;

if (newLeft < viewport.left
|| newTop < viewport.top
|| newLeft + rect.width > viewport.right
|| newTop + rect.height > viewport.bottom
) {
// the element will hit the boundary, do nothing...
} else {
// 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 {
border: 1px solid black;
height: 100px;
left: 8px;
position: absolute;
top: 8px;
width: 100px;
}
<div id="mydiv"></div>

关于javascript - 如何防止可拖动元素移出边界?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48097791/

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