gpt4 book ai didi

javascript - 如何防止div被拖动时链接打开?

转载 作者:行者123 更新时间:2023-12-02 22:59:38 25 4
gpt4 key购买 nike

我有一个可拖动的 div,它也将充当链接。目前,拖动 div 后释放鼠标后,它会打开链接。我希望链接仅在单击时打开,而不是在拖动和释放时打开

有人有什么建议吗?

<div id="draggable"><div id="draggableimg"><a href="events.html"><img src="Events.png"/></a></div></div>

#draggable {
position: absolute;
top: 100px;
left: 100px;
}

#draggableimg img {
width: 300px;
}


//Make the DIV element draggable:
dragElement(document.getElementById("draggable"));

function dragElement(elmnt) {
var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
if (document.getElementById(elmnt.id + "img")) {
/* if present, the header is where you move the DIV from:*/
document.getElementById(elmnt.id + "img").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;
}
}

最佳答案

当您开始拖动到所选对象时,您只需添加类即可。这将禁用 anchor 标记的操作。我在当前示例中使用 dragstart

停止拖动后,您需要删除该类。以下是您的示例:

<html>
<head>
<title></title>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<header>
<div id="draggable"><div id="draggableimg"><a href="https://stackoverflow.com/questions/57839524/how-to-prevent-link-opening-while-div-is-dragged/57921413"><img src="Events.png"/></a></div></div>




<style>
#draggable {
position: absolute;
top: 100px;
left: 100px;
}
#draggableimg {border:solid 1px #999; display:inline-block;}
#draggableimg img {
width: 300px;
}
.dragstart a{pointer-events: none;}
</style>




<script>

//Make the DIV element draggable:
dragElement(document.getElementById("draggable"));
var _curDragEle = "";
function dragElement(elmnt) {
var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
if (document.getElementById(elmnt.id + "img")) {
/* if present, the header is where you move the DIV from:*/
document.getElementById(elmnt.id + "img").onmousedown = dragMouseDown;
} else {
/* otherwise, move the DIV from anywhere inside the DIV:*/
elmnt.onmousedown = dragMouseDown;
}

function dragMouseDown(e) {
e = e || window.event;
e.preventDefault();
_curDragEle = this;
// 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) {
_curDragEle.classList.remove("dragstart")
_curDragEle.className = _curDragEle.className + " dragstart";
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() {
var k = document.querySelectorAll(".dragstart")
for(var i=0; i < k.length;i++){
k[0].classList.remove("dragstart")
}
/* stop moving when mouse button is released:*/
document.onmouseup = null;
document.onmousemove = null;
}
}
</script>
</body>
</html>

JSFiddle link

关于javascript - 如何防止div被拖动时链接打开?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57839524/

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