gpt4 book ai didi

Javascript 工具提示不随鼠标移动而移动

转载 作者:行者123 更新时间:2023-12-03 00:49:28 24 4
gpt4 key购买 nike

如何修复此代码,现在工具提示部分工作,工具提示显示但不跟随鼠标。 ON jsfiddle 工作没有问题。

来源在这里https://jsfiddle.net/mgbv5tzx/212/HTML-Tooltip position relative to mouse pointer

<!DOCTYPE html>
<html>
<head>
<title>Tooltip</title>
<meta charset="UTF-8">
<script>
function showTooltip(e) {
var tooltip = e.target.classList.contains("coupontooltip")
? e.target
: e.target.querySelector(":scope .coupontooltip");
tooltip.style.left =
(e.pageX + tooltip.clientWidth + 10 < document.body.clientWidth)
? (e.pageX + 10 + "px")
: (document.body.clientWidth + 5 - tooltip.clientWidth + "px");
tooltip.style.top =
(e.pageY + tooltip.clientHeight + 10 < document.body.clientHeight)
? (e.pageY + 10 + "px")
: (document.body.clientHeight + 5 - tooltip.clientHeight + "px");
}

var tooltips = document.querySelectorAll('.couponcode');
for(var i = 0; i < tooltips.length; i++) {
tooltips[i].addEventListener('mousemove', showTooltip);
}
</script>
<style type="text/css">
.couponcode {
color: red;
cursor: pointer;
}

.couponcode:hover .coupontooltip {
display: block;
}

.coupontooltip {
position: absolute;
white-space: nowrap;
display: none;
background: #ffffcc;
border: 1px solid black;
color: black;
padding: 5px;
z-index: 1000;
}
</style>

</head>
<body>
Lorem ipsum dolor sit amet, <span class="couponcode">consectetur
adipiscing<span class="coupontooltip" style="overflow: hidden;">This is a tooltip</span></span>
elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi
ut aliquip ex ea commodo consequat. Duis aute irure dolor in <span
class="couponcode">reprehenderit<span class="coupontooltip">This is
another tooltip</span></span> in voluptate velit esse cillum dolore eu
fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident,
sunt in culpa qui officia deserunt mollit anim id est <span
class="couponcode">laborum<span class="coupontooltip">This is yet
another tooltip</span></span>.
</body>
</html>

感谢帮助

最佳答案

您应该引用如何将javascript放入html文件来了解:

https://www.w3schools.com/js/js_whereto.asp

我编辑了您的示例,您可以引用解决方案1:我使用onload调用脚本后将脚本放在head中

<!DOCTYPE html>
<html>
<head>
<title>Tooltip</title>
<meta charset="UTF-8">
<script>

function showTooltip(e) {
var tooltip = e.target.classList.contains("coupontooltip")
? e.target
: e.target.querySelector(":scope .coupontooltip");
tooltip.style.left =
(e.pageX + tooltip.clientWidth + 10 < document.body.clientWidth)
? (e.pageX + 10 + "px")
: (document.body.clientWidth + 5 - tooltip.clientWidth + "px");
tooltip.style.top =
(e.pageY + tooltip.clientHeight + 10 < document.body.clientHeight)
? (e.pageY + 10 + "px")
: (document.body.clientHeight + 5 - tooltip.clientHeight + "px");
}

function init() {
var tooltips = document.querySelectorAll('.couponcode');
for(var i = 0; i < tooltips.length; i++) {
tooltips[i].addEventListener('mousemove', showTooltip);
}
}
</script>
<style type="text/css">
.couponcode {
color: red;
cursor: pointer;
}

.couponcode:hover .coupontooltip {
display: block;
}

.coupontooltip {
position: absolute;
white-space: nowrap;
display: none;
background: #ffffcc;
border: 1px solid black;
color: black;
padding: 5px;
z-index: 1000;
}
</style>

</head>
<body onload="init();">
Lorem ipsum dolor sit amet, <span class="couponcode">consectetur
adipiscing<span class="coupontooltip" style="overflow: hidden;">This is a tooltip</span></span>
elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi
ut aliquip ex ea commodo consequat. Duis aute irure dolor in <span
class="couponcode">reprehenderit<span class="coupontooltip">This is
another tooltip</span></span> in voluptate velit esse cillum dolore eu
fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident,
sunt in culpa qui officia deserunt mollit anim id est <span
class="couponcode">laborum<span class="coupontooltip">This is yet
another tooltip</span></span>.
</body>
</html>

解决方案 2:我将脚本放在正文中

<!DOCTYPE html>
<html>
<head>
<title>Tooltip</title>
<meta charset="UTF-8">

<style type="text/css">
.couponcode {
color: red;
cursor: pointer;
}

.couponcode:hover .coupontooltip {
display: block;
}

.coupontooltip {
position: absolute;
white-space: nowrap;
display: none;
background: #ffffcc;
border: 1px solid black;
color: black;
padding: 5px;
z-index: 1000;
}
</style>

</head>
<body>
Lorem ipsum dolor sit amet, <span class="couponcode">consectetur
adipiscing<span class="coupontooltip" style="overflow: hidden;">This is a tooltip</span></span>
elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi
ut aliquip ex ea commodo consequat. Duis aute irure dolor in <span
class="couponcode">reprehenderit<span class="coupontooltip">This is
another tooltip</span></span> in voluptate velit esse cillum dolore eu
fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident,
sunt in culpa qui officia deserunt mollit anim id est <span
class="couponcode">laborum<span class="coupontooltip">This is yet
another tooltip</span></span>.
<script>

function showTooltip(e) {
var tooltip = e.target.classList.contains("coupontooltip")
? e.target
: e.target.querySelector(":scope .coupontooltip");
tooltip.style.left =
(e.pageX + tooltip.clientWidth + 10 < document.body.clientWidth)
? (e.pageX + 10 + "px")
: (document.body.clientWidth + 5 - tooltip.clientWidth + "px");
tooltip.style.top =
(e.pageY + tooltip.clientHeight + 10 < document.body.clientHeight)
? (e.pageY + 10 + "px")
: (document.body.clientHeight + 5 - tooltip.clientHeight + "px");
}


var tooltips = document.querySelectorAll('.couponcode');
for(var i = 0; i < tooltips.length; i++) {
tooltips[i].addEventListener('mousemove', showTooltip);
}

</script>
</body>
</html>

关于Javascript 工具提示不随鼠标移动而移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53115258/

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