gpt4 book ai didi

javascript - 显示可拖动 div 悬停的标签内容

转载 作者:行者123 更新时间:2023-11-28 03:38:22 25 4
gpt4 key购买 nike

我有一个可移动的 div(单击并拖动)。我想将鼠标悬停在 HTML 中的任何文本上,并在控制台上给出悬停内容的输出。

举个例子,我有一个 <p> Text </p> 。如果我将可移动对象悬停在该段落上,控制台会说..“文本”

//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;
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 {
position: absolute;
z-index: 9;
background-color: #f7f7f7;
text-align: center;
border: 1px solid #d3d3d3;
opacity: 0.5;
}

#mydivheader {
padding: 10px;
cursor: move;
z-index: 10;
background-color: #2196F3;
color: #fff;
}
<p>text</p>

<div id="mydiv">
<div id="mydivheader">Click here to move</div>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
</div>

最佳答案

正如评论中提到的,您可以使用 document.elementFromPoint(e.clientX, e.clientY) 来获取光标下的元素,并使用 .textContent 来获取其内容为文本。

另一件事:你必须设置 pointer-events在调用 elementFromPoint 之前,将可拖动的 div 更改为 "none",否则将返回拖动的元素。

一个完整的例子:

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

elmnt.style.pointerEvents = "none";
let hoveredElt = document.elementFromPoint(e.clientX, e.clientY);
if (hoveredElt.tagName === "P") {
console.log(hoveredElt.textContent);
}
elmnt.style.pointerEvents = "auto";
}

function closeDragElement() {
/* stop moving when mouse button is released:*/
document.onmouseup = null;
document.onmousemove = null;
}
}
#mydiv {
position: absolute;
z-index: 9;
background-color: #f7f7f7;
text-align: center;
border: 1px solid #d3d3d3;
opacity: 0.5;
}

#mydivheader {
padding: 10px;
cursor: move;
z-index: 10;
background-color: #2196F3;
color: #fff;
}
<p>text</p>

<div id="mydiv">
<div id="mydivheader">Click here to move</div>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
</div>

关于javascript - 显示可拖动 div 悬停的标签内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57509739/

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