18event-demo.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> img { width: 120px; height: 120px; border-radius: 50%; position: absolute; } </style> </head> <body> <img src="img/cxy.jpeg" alt=""> <script> var oImg = document.querySelector("img"); document.onmousemove = function(e) { var e = e || event; var x = e.pageX; var y = e.pageY; // 设置图片的坐标 oImg.style.left = (x-60) + "px"; oImg.style.top = (y-60) + "px"; } </script> </body> </html>