JQ浮动元素跟随鼠标移动


<!DOCTYPE html>
<html lang="zh-CN">
	<head>
		<meta charset="utf-8" />
		<style type="text/css">
			body{margin:0;}
			#aa{width:1100px;height:700px;border:1px solid #000;background:#000;postion:absolute;float:left;}
			#bb{float:left;}#cc{float:left;}
			.pp{width:6px;height:6px;background:#fff;position:absolute;}
		</style>
		<script src="jquery.js"></script>
	</head>
	<body>
		<div id="aa"></div>
		<div id="bb"></div>
		<div id="cc"></div>
		<script>
			var num = 100;	//元素的总个数
			var floatSpeed = 300;	//浮动速度
			var floatCycle = 500;	//浮动周期	(浮动速度的2倍)
			var $xx = 100;	//鼠标 x 坐标
			var $yy = 100;	//鼠标 y 坐标
			var moveTime = 600;		//移动时间
			var moveDistance = 20;	//移动距离
			var randMoveDistanceTop = 80;	//随机距离上限
			var randMoveDistanceDown = 5;	//随机距离下限
			var moveDistance2 = moveDistance;
			var moveDistance3 = moveDistance;
			var arrDistance = [];
			var addOrLess = null;	//随机符号
			var addOrLess2 = null;
			var arrAddOrLess = ["+", "-"];
			var intvalEleFloat = null;
			var intvalMouse = null;		//监视鼠标是否静止
			var intvalStartMouse = null;
			var fx = 0;		//标记鼠标是否移动
			var fy = 0;

			//创建元素
			for(var i=0;i < num;i++){$("#aa").append("<div id=\"p" + i + "\" class=\"pp\"></div>");}
			//初始化元素位置
			randPosition();
			//开始浮动
			goFloat();
			//监视鼠标是否静止
			intvalMouse = setInterval(function(){
				//若鼠标发生移动
				if(fx != $xx || fy != $yy){
					//清空动画列队,执行完现有动画
					$(".pp").stop(true, false);
					fx = $xx;
					fy = $yy;
					$("#cc").text("");
					//元素移动到鼠标位置
					follow();
					$("#bb").text("xx:" + $xx + "...yy:" + $yy + "...fx:"+fx+"...fy:"+fy);
				};
			}, 400);
			
			$("#aa").mousemove(function (e) {
				$xx = e.originalEvent.x || e.originalEvent.layerX || 0;
				$yy = e.originalEvent.y || e.originalEvent.layerY || 0;
				$("#bb").text("xx:" + $xx + "...yy:" + $yy + "...fx:"+fx+"...fy:"+fy);
				$(".pp").stop(true, false);
			});
			function randPosition(){
				$(".pp").each(function(){
					randDistance();
					$(this).animate({left:($xx + (arrDistance[2]))+"px", top:($yy + (arrDistance[3]))+"px"}, moveTime);
					$("#cc").append("<p>2:"+moveDistance2+"...3:"+moveDistance3+"...x:"+($xx + (moveDistance2))+"...y:"+($yy + (moveDistance3))+"</p>");
				});
			}
			function follow(){
				$(".pp").each(function(){
					randDistance();
					$(this).animate({left:($xx + (arrDistance[2]))+"px", top:($yy + (arrDistance[3]))+"px"}, moveTime);
				});
			}
			//移动随机距离
			function randDistance(){
					moveDistance = parseInt(Math.random() * (randMoveDistanceTop - randMoveDistanceDown + 1) + randMoveDistanceDown);
					addOrLess = arrAddOrLess[Math.floor(Math.random() * arrAddOrLess.length)];
					addOrLess2 = addOrLess;
					if(addOrLess == "-"){moveDistance2 = -moveDistance;}else{moveDistance2 = moveDistance;};
					moveDistance = parseInt(Math.random() * (randMoveDistanceTop - randMoveDistanceDown + 1) + randMoveDistanceDown);
					addOrLess = arrAddOrLess[Math.floor(Math.random() * arrAddOrLess.length)];
					if(addOrLess == "-"){moveDistance3 = -moveDistance;}else{moveDistance3 = moveDistance;};
					arrDistance[0] = moveDistance;
					moveDistance = parseInt(Math.random() * (randMoveDistanceTop - randMoveDistanceDown + 1) + randMoveDistanceDown);
					arrDistance[1] = moveDistance;
					arrDistance[2] = moveDistance2;
					arrDistance[3] = moveDistance3;
			}
			function goFloat(){
				intvalEleFloat = setInterval(eleFloat, floatCycle);
			}
			function eleFloat(){
				$(".pp").each(function(){
					randDistance();
					$(this).animate({left:addOrLess+"="+arrDistance[0]+"px", top:addOrLess2+"="+arrDistance[1]+"px"}, floatSpeed);
					if(addOrLess == "-"){addOrLess = "+";}else{addOrLess = "-";};
					if(addOrLess2 == "-"){addOrLess2 = "+";}else{addOrLess2 = "-";};
					$(this).animate({left:addOrLess+"="+arrDistance[0]+"px", top:addOrLess2+"="+arrDistance[1]+"px"}, floatSpeed);
				});
			}

		</script>
	</body>
</html>


另一版本详见GitHub : https://github.com/yotcap/someGadgets/tree/master/Elements-float-follow-the-mouse


JQ是指jQuery,是一个流行的JavaScript库,用于简化网页开发中的DOM操作。要实现鼠标移入图片放大,并且跟随鼠标移动,可以通过以下步骤: 1. 在HTML中添加一个图片元素,并为其添加一个唯一的ID,以便后续操作。 2. 使用jQuery的.hover()方法来监测鼠标移入和移出图片的事件。 3. 在鼠标移入事件的处理函数中,使用jQuery的.animate()方法来逐渐增大图片的尺寸。 4. 在鼠标移动事件的处理函数中,获取鼠标的当前位置,然后使用jQuery的.css()方法来动态调整图片的位置。 具体步骤可如下所示: HTML: ``` <img id="myImage" src="图片的URL" alt="图片"> ``` JavaScript: ``` $(document).ready(function(){ $("#myImage").hover( function(){ // 鼠标移入事件处理函数 $(this).animate({ width: '+=50px', // 图片宽度增加50像素 height: '+=50px' // 图片高度增加50像素 }, 200); // 动画持续时间为200毫秒 }, function(){ // 鼠标移出事件处理函数 $(this).animate({ width: '-=50px', // 图片宽度减少50像素 height: '-=50px' // 图片高度减少50像素 }, 200); // 动画持续时间为200毫秒 } ); $("#myImage").mousemove(function(event){ // 鼠标移动事件处理函数 var imagePosition = $(this).offset(); // 获取图片在页面中的位置 var mouseX = event.pageX - imagePosition.left; // 获取鼠标相对于图片的水平位置 var mouseY = event.pageY - imagePosition.top; // 获取鼠标相对于图片的垂直位置 $(this).css({left: mouseX, top: mouseY}); // 动态设置图片的位置 }); }); ``` 以上代码将在页面加载完成后绑定鼠标移入、移出以及移动事件,当鼠标移入图片时,图片逐渐放大,当鼠标移出图片时,图片逐渐恢复原始尺寸;同时,当鼠标在图片上移动时,图片将跟随鼠标的位置移动。注意,需要将代码中的"图片的URL"替换为实际图片的URL。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值