<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title></title> <style>
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title></title> <style> #div1{ width: 100px; height: 100px; background-color:darkseagreen; border-radius: 20px; position: absolute; } </style> <script> window.onload=function(){ var oDiv = document.getElementById("div1"); var i = 0; document.onmousemove=function(ev){ var scrollTop = document.documentElement.scrollTop||document.body.scrollTop; var ev = ev||event; oDiv.style.left = ev.clientX + 'px'; oDiv.style.top = ev.clientY + scrollTop +'px'; } } </script></head><body> <div id="div1"> </div></body></html>
绿色的div会跟随鼠标移动,并且不受滚动条的影响