<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
img {
position: absolute;
top: 2px;
}
</style>
</head>
<body>
<img src="images/angel.gif" alt="">
<script>
var pic=document.querySelector('img');
document.addEventListener('mousemove', function (e) {
//1.mousemove只要我们鼠标移动1px 就会触发这个事情
// console.log(1);
//2.核心原理:每次鼠标移动,我们都会获得最新的鼠标坐标,把这个x和y坐标作为图片的top和left值
var x = e.pageX;
var y = e.pageY;
console.log('x坐标是'+x,'y坐标是'+y);
//千万不要忘记left添加单位
pic.style.left = x - 50 + 'px';
pic.style.top = x - 40 + 'px';
})
</script>
</body>
</html>
图片跟随鼠标移动
猜你喜欢
转载自blog.csdn.net/qq_55061257/article/details/120673460
今日推荐
周排行