<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
body {
height: 1000px;
}
#img {
position: absolute; /*脱离文档流*/
}
</style>
</head>
<body>
<img src="images/honeybee.gif" alt="" id="img">
<script>
var img = document.getElementById('img');
document.onmousemove = function (e) {
e = e || window.event;
img.style.left = e.clientX -10 + 'px'; // -10让鼠标跟图片有10px的间距
img.style.top = e.clientY -10 + 'px'; // body设置高度出现滚动条后 图片距离鼠标远
// img.style.left = e.pageX + 'px';
// img.style.top = e.pageY + 'px';
};
</script>
</body>
</html>
原生js跟着鼠标飞的图片e.clientX、e.clientY
最新推荐文章于 2024-08-30 20:29:15 发布