<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
.box{width: 300px;height: 40px;background: #ccc;margin: 20px auto;position: relative;}
.box *{margin: 0;}
.box h2{line-height: 40px;}
.box p{width: 200px;background: #eee;font-size: 14px;position: absolute;left:0;top:0;display: none;z-index: 1;}
#box{width: 100px;height: 100px;background: red;display: block}
#box:hover{display: none;}
</style>
</head>
<body>
<div class="box">
<h2>这是一个标题1</h2>
<p>这是标题1对应的详细文本这是标题1对应的详细文本这是标题1
对应的详细文本这是标题1对应的详细文本这是标题1
对应的详细文本这是标题1对应的详细文本这是标题1</p>
</div>
<div class="box">
<h2>这是一个标题2</h2>
<p>这是标题2对应的详细文本题2对应的详细文本题2对应的详细文本题2
对应的详细文本题2对应的详细文本题2对应的详细文本题2
对应的详细文本题2对应的详细文本题2</p>
</div>
<div id="box"></div>
</body>
<script>
var ah2 = document.querySelectorAll(".box h2");
for(var i=0;i<ah2.length;i++){
ah2[i].onmouseover = function(){
// console.log(this);
this.nextElementSibling.style.display = "block";
}
ah2[i].onmousemove = function(eve){
// console.log(this);
var e = eve || window.event;
// 将h2后面的p的定位位置,设置成鼠标的坐标,稍微往后一点,防止阻挡鼠标放在事件源身上
this.nextElementSibling.style.left = e.offsetX + 5 + "px";
this.nextElementSibling.style.top = e.offsetY + 5 + "px";
}
ah2[i].onmouseout = function(){
// console.log(this);
this.nextElementSibling.style.display = "none";
}
}
</script>
</html>
js实现跟随鼠标移动的提示框
最新推荐文章于 2023-03-09 20:43:42 发布
本文介绍如何利用JavaScript、CSS和HTML创建一个跟随鼠标移动的提示框。通过监听鼠标移动事件,动态更新提示框的位置,并展示相应的内容,为用户提供实时反馈。
部署运行你感兴趣的模型镜像
您可能感兴趣的与本文相关的镜像
EmotiVoice
AI应用
EmotiVoice是由网易有道AI算法团队开源的一块国产TTS语音合成引擎,支持中英文双语,包含2000多种不同的音色,以及特色的情感合成功能,支持合成包含快乐、兴奋、悲伤、愤怒等广泛情感的语音。
1712

被折叠的 条评论
为什么被折叠?



