js鼠标移动事件案例


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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM