代码:
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
img {
width: 200px;
position: absolute;
}
body {
transition: 1s;
}
</style>
</head>
<body>
<img src="https://s21.ax1x.com/2024/04/08/pFLZm6A.png" alt="" />
<script>
let img = document.querySelector("img");
let imgtype = false;
img.onclick = function () {
imgtype = !imgtype;
if (imgtype) {
document.onmousemove = function (e) {
img.style.top = e.clientY - img.offsetHeight/2+ "px";
img.style.left = e.clientX - img.offsetWidth/2 + "px";
};
} else {
document.onmousemove = "";
}
};
// setInterval(()=>{
// img.style.top = Math.floor(Math.random()*500)+'px'
// img.style.left = Math.floor(Math.random()*1000)+'px'
// },100)
</script>
</body>
</html>
效果图: