js实现跟随鼠标移动的小球
程序员文章站
2024-02-02 09:38:40
本文实例为大家分享了js实现跟随鼠标移动的具体代码,供大家参考,具体内容如下
本文实例为大家分享了js实现跟随鼠标移动的具体代码,供大家参考,具体内容如下
<!doctype html> <html> <head> <title>follow mouse</title> </head> <style type="text/css"> *{ margin: 0; padding:0; } #div1{width: 50px;height: 50px;background: red;color:#fff;text-align: center;line-height: 50px; position: absolute;transition: all 0.5s; } #div2{width: 50px;height: 50px;background: green;color:#fff;text-align: center;line-height: 50px;position: absolute;transition: all 0.5s; } .box1{ width: 400px; height: 400px; border: 1px solid #ccc; } .box2{ width: 400px; height: 400px; border: 1px solid #ccc; } </style> <body> <div class="box1" onmousemove="b1()"> <div id="div1" >1</div> </div> <div class="box2" onmousemove="b2()"> <div id="div2">2</div> </div> </body> <script type="text/javascript"> function b1(ev) { var oevent = ev || event; var odiv = document.getelementbyid('div1'); if( oevent.clientx<350&&oevent.clienty<350){ odiv.style.left = oevent.clientx + 'px'; odiv.style.top = oevent.clienty + 'px'; } } function b2(ev){ var oevent=ev||event; var odiv=document.getelementbyid('div2'); if( oevent.clientx<350&&oevent.clienty<750){ odiv.style.left=oevent.clientx+'px'; odiv.style.top=oevent.clienty+'px'; } } </script> </html>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。