H5练手小游戏

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Play Game</title>
	<style>
		body, html {
			margin: 0;
			padding: 0;
			width: 100%;
			height: 100%;
			overflow: hidden;
			background-color: skyblue; /* 添加背景颜色 */
		}

		#counter {
			position: absolute;
			top: 15px;
			left: 10px;
			font-size: 2em;
		}
		.yellowDiv {
			position: absolute;
			display: flex; /* 添加flex布局 */
			justify-content: center; /* 水平居中 */
			align-items: center; /* 垂直居中 */
			background-color: yellow;
			width: 50px;
			height: 50px;
			transition: all 0.5s ease;
		}
		.orangeDiv {
			background-color: orange;
			width: 25px; /* 调整尺寸以适应 */
			height: 25px; /* 调整尺寸以适应 */
		}
		.disappearing {
			top: 10px !important;
			left: 10px !important;
			transition: all 0.5s ease;
		}
	</style>
</head>
<body>
	<div id="head">请为24届大一新生收集阳光</div>
	<div id="counter">0</div>

	<script>
		document.addEventListener('DOMContentLoaded', function() {
			const counter = document.getElementById('counter');
			let count = 0;

			function createYellowDiv() {
				const div = document.createElement('div');
				div.className = 'yellowDiv';
				const boundary = window.innerWidth * 0.1;		//控制范围
				div.style.left = Math.floor(Math.random() * (window.innerWidth - boundary * 2)) + boundary + 'px';

				// 创建橙色div
				const orangeDiv = document.createElement('div');
				orangeDiv.className = 'orangeDiv';
				div.appendChild(orangeDiv);

				div.addEventListener('click', () => {
					div.classList.add('disappearing');
					setTimeout(() => {
						div.remove();
						count++;
						counter.textContent = count;
					}, 500);
				});

				document.body.appendChild(div);
				animateFall(div);
			}

			function animateFall(div) {
				let currentTop = 0;
				const intervalId = setInterval(() => {
					currentTop += 10;
					div.style.top = currentTop + 'px';
					if (currentTop > window.innerHeight) {
						clearInterval(intervalId);
						div.remove();
					}
				}, 100);
			}

			setInterval(createYellowDiv, 1000);
		});
	</script>
</body>
</html>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值