jqeury 跟随鼠标飘动的div

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>follow mouse with a div</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" ></script>
<script type="text/javascript">
	var $mouseX = 0, $mouseY = 0;
	var $xp = 0, $yp =0;
	$(document).mousemove(function(e){
	    $mouseX = e.pageX;
	    $mouseY = e.pageY;    
	});
	$(document).ready(    
		setInterval(function(){
			$xp += ($mouseX-$xp)/20;
			$yp += ($mouseY-$yp)/20;
			$("#floatdiv").css({left:$xp, top:$yp});  
		}, 20)
	)
</script>
<style type="text/css">
.fdiv{position: absolute;width: 180px;height: 90px;background-color: gray;z-index: 300px;
font-size: small;}
</style>
</head>
<body>
	<div id="floatdiv" class="fdiv">样式设置成absolute。不用setInterval,直接move事件时改变div的left和top值一样</div>
</body>
</html>

你可能感兴趣的:(跟随鼠标飘动的div)