jQuery小球点击发射动画

今天花了两个小时使用jQuery写了一个小动画游戏,如下图所示,通过鼠标点击,发射球。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style>
			#main {
				width: 500px;
				height: 650px;
				border: 3px solid #efefef;
				margin: 30px auto;
				position: relative;
			}

			#fireSpace {
				width: 100%;
				height: 400px;
				position: absolute;
				top: 0;
				left: 0;

			}

			#gun {
				display: block;
				width: 60px;
				height: 60px;
				position: absolute;
				bottom: 20px;
				left: 50%;
				transform: translate(-50%, 0);
			}
		</style>
	</head>
	<body>
		<div id="main">
			<div id="fireSpace">

			</div>
			<img src="./gun.png" id="gun">
		</div>
	</body>
</html>
<script src="./jquery.js"></script>
<script>
	let initX = 0,
		initY = 300,
		initDeg = 90,
		thenDeg, gunX, gunY, boo, x = 0,
		y = 300,
		nx, ny, dg = 90,
		ndg, rdg, isLeft0, isLeft;
	document.getElementById("fireSpace").onmousemove = function(e) {
		if (e.offsetX - 220 >= 0) {
			// nx = e.offsetX - 220;
			// ny = 600-e.offsetY;
			gunX = e.offsetX - 220;
			isLeft = false;
		} else if (e.offsetX - 220 <= 0) {
			gunX = 220 - e.offsetX;
			isLeft = true;
		}
		gunY = 650 - e.offsetY;
		if (e.offsetX - 220 == 0) {
			thenDeg = 90;
		} else {
			thenDeg = gunY - gunX >= 0 ? (90 - Math.asin(gunX / gunY) * 180 / Math.PI) : (Math.asin(gunY / gunX) *
				180 / Math.PI);
			// thenDeg = Math.asin(gunY / gunX) * 180 / Math.PI;
		}
		if (initX - 220 == 0) {
			initDeg = 90;
		} else {
			initDeg = initY - initX >= 0 ? (90 - Math.asin(initX / initY) * 180 / Math.PI) : (Math.asin(initY /
					initX) *
				180 / Math.PI);
		}
		if (initY <= 3) {
			initDeg = 0;
		}
		if (gunY <= 3) {
			thenDeg = 0;
		}
		if (!isLeft0 && isLeft) {
			rdg = -(180 - initDeg - thenDeg);
		} else if (isLeft0 && !isLeft) {
			rdg = 180 - initDeg - thenDeg;
		} else if (isLeft0 && isLeft) {
			rdg = (thenDeg - initDeg)
		} else if (!isLeft0 && !isLeft) {
			rdg = (initDeg - thenDeg)
		}
		document.getElementById("gun").style.transform = "translate(-50%, 0) rotate(" + rdg + "deg)";
		x = nx;
		y = ny;
		isLeft0 = isLeft;
	}
	let fireX,fireY,iX=0,iY=0
	document.getElementById("fireSpace").onclick = function(e) {
		fireX = e.offsetX;
		fireY = e.offsetY;
		let boll = document.createElement("img");
		boll.style.width = "50px";
		boll.style.height = "50px";
		boll.setAttribute("src", "./boll.png");
		boll.style.position = "absolute";
		boll.style.bottom = "0";
		boll.style.left = "50%";
		boll.style.transform = "translate(-40%,0)";
		boll.style.zIndex = "-1";
		document.getElementById("main").appendChild(boll);
		$(boll).animate({
			top: fireY,
			left: fireX
		}, 1000);
		setTimeout(function() {
			boll.parentNode.removeChild(boll);
		}, 1000);
	}
</script>

图片素材:
在这里插入图片描述
在这里插入图片描述
感兴趣的的小伙伴可以去试试。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前后端杂货铺

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值