<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <span id="a" style="position:absolute;border: 1px solid blue"> 跟着我 </span> <script> function a(){ var div = document.getElementById("a"); if (!div) { return; } var intX = window.event.clientX; var intY = window.event.clientY; div.style.left = intX + "px"; div.style.top = intY + "px"; } document.onmousemove = a; </script> </body> </html>
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <span id="a" style="border: 1px solid blue"> 跟着我 </span> <script> function a(){ var div = document.getElementById("a"); div.style.position='absolute'; div.style.left = window.event.clientX+ "px"; div.style.top =window.event.clientY+ "px"; } document.onmousemove = a; </script> </body> </html>
超简单版的跟着鼠标移动