html
复制代码
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>红包雨小游戏</title>
<style>
body {
margin: 0;
overflow: hidden;
background-color: #dff9fb; /* 设置背景颜色 */
font-family: Arial, sans-serif;
}
#container {
position: relative;
width: 80vw; /* 将宽度设置为80% */
height: 80vh; /* 将高度设置为80% */
overflow: hidden;
margin: 10vh auto; /* 垂直居中 */
}
#score {
position: fixed;
top: 20px;
left: 20px;
font-size: 24px;
color: #333;
background-color: rgba(255, 255, 255, 0.8); /* 半透明白色背景 */
padding: 10px 15px;
border-radius: 5px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}
.red-envelope {
position: absolute;
width: 50px;
height: 70px;
color: white;
font-size: 20px;
text-align: center;
line-height: 70px;
border-radius: 5px;
cursor: pointer;
user-select: none;
font-family: "Microsoft YaHei", Arial, sans-serif;
}
.red {
background-color: #ff4d4f; /* 红色 */
}
.gold {
background-color: #ffcc00; /* 金色 */
}
.blue {
background-color: #007bff; /* 蓝色 */
}
.green {
background-color: #28a745; /* 绿色 */
}
@keyframes fall {
0% {
transform: translateY(0);
}
100% {
transform: translateY(100vh);
}
}
.fall {
animation: fall linear forwards;
}
#reward {
position: fixed;
top: 100px;
left: 50%;
transform: translateX(-50%);
font-size: 30px;
font-weight: bold;
color: #fff;
background: linear-gradient(135deg, #ff7e5f, #feb47b); /* 渐变色 */
padding: 20px 40px;
border-radius: 10px;
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.5);
display: none; /* 初始隐藏 */
z-index: 10; /* 确保在最上层 */
animation: bounce 1s ease-out infinite; /* 添加弹跳动画 */
}
@keyframes bounce {
0%, 20%, 50%, 80%, 100% {
transform: translateY(0);
}
40% {
transform: translateY(-10px);
}
60% {
transform: translateY(-5px);
}
}
.reward-text {
font-size: 36px;
font-weight: bold;
color: #fff;
}
</style>
</head>
<body>
<div id="score">获得红包数量: <span id="count">0</span></div>
<div id="reward">
<span class="reward-text">恭喜你!获得额外奖励!</span>
</div>
<div id="container"></div>
<script>
const container = document.getElementById('container');
const countDisplay = document.getElementById('count'); // 用于显示红包数量
const rewardDisplay = document.getElementById('reward'); // 用于显示奖励消息
const colors = ['red', 'gold', 'blue', 'green']; // 红包颜色数组
let score = 0; // 记录获得的红包数量
function createRedEnvelope() {
const envelope = document.createElement('div');
envelope.className = 'red-envelope ' + colors[Math.floor(Math.random() * colors.length)]; // 随机选择颜色
envelope.textContent = '红包';
envelope.style.left = Math.random() * (container.clientWidth - 50) + 'px'; // 随机位置,确保在画布范围内
envelope.style.top = '-70px'; // 初始位置在屏幕上方
// 添加动画类
envelope.classList.add('fall');
// 点击事件
envelope.addEventListener('click', () => {
score++; // 增加红包数量
countDisplay.textContent = score; // 更新显示数量
// 每获得 5 个红包,显示奖励
if (score % 5 === 0) {
showReward();
}
container.removeChild(envelope); // 移除红包
});
// 设置动画持续时间
envelope.style.animationDuration = Math.random() * 2 + 3 + 's'; // 随机时长
// 添加到容器
container.appendChild(envelope);
// 动画结束后移除红包
envelope.addEventListener('animationend', () => {
container.removeChild(envelope);
});
}
function showReward() {
rewardDisplay.style.display = 'block'; // 显示奖励消息
setTimeout(() => {
rewardDisplay.style.display = 'none'; // 3秒后隐藏奖励消息
}, 3000);
}
// 每500毫秒生成一个红包(增加红包数量)
setInterval(createRedEnvelope, 500);
</script>
</body>
</html>