<!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>