<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<style type="text/css">
*{
margin:0;
padding: 0;
}
#two{
background-color: black;
position: absolute;
color: white;
}
</style>
<body>
<div id="one"></div>
<div id="two">你点不到我</div>
</body>
<script type="text/javascript">
two=document.getElementById('two');
one=document.getElementById('one');
//鼠标移上去随机出现
two.onmousemove=function(){
//限定范围
w=Math.random()*window.innerWidth-two.offsetWidth;
h=Math.random()*window.innerHeight-two.offsetHeight
if (w<=0) {
w=0;
}
if (h<=0) {
h=0;
}
//移动
two.style.top=h+"px";
two.style.left=w+"px";
}
</script>
</html>
js鼠标移动事件案例
最新推荐文章于 2024-09-12 16:31:59 发布