图片跟随鼠标移动特效

2.上代码演示

<!DOCTYPE html>
<html>
   <head>
   	<meta charset="utf-8">
   	<title></title>
   </head>
   <style type="text/css">
   	* {
   		margin: 0px;
   		padding: 0px;
   	}

   	#move {
   		position: absolute;
   		left: -300px;
   		top: -300px;
   	}
   </style>
   <body>
   	<img id="move" src="https://i.loli.net/2020/04/18/OXz4M1h5VcxKCU3.png">
   </body>
   <script type="text/javascript">
   	var move = document.getElementById('move');
   	window.onmousemove = function(e) {
   		var event = e || window.event;

   		var x = event.clientX;
   		var y = event.clientY;
   		if (x <= 10 || y <= 10||x>(document.body.clientWidth-move.offsetWidth)) {
   			x = -300;
   		}
   		move.style.left = x + "px";
   		move.style.top = y + "px";

   	}
   </script>
</html>

猜你喜欢

转载自www.cnblogs.com/yaoliuyang/p/12728588.html