设计特点
-
视觉效果:
- 飘落雪花: 使用纯CSS动画创建的逼真雪花效果。
- 圣诞主题: 经典的红、绿、白配色方案。
- 发光文字: 使用CSS
text-shadow实现的霓虹灯效果。 - 3D礼物盒: 使用CSS 3D变换创建的立体礼物盒,可点击。
-
交互功能:
- 点击礼物: 点击礼物盒会有动画反馈,并弹出一个温馨的祝福。
- 播放音乐: 点击“播放/暂停”按钮可以控制背景音乐(需要替换成你自己的音乐文件)。
- 动态祝福: 祝福语会以打字机效果逐字显示。
-
结构清晰:
- 使用了语义化的HTML5标签 (
<header>,<main>,<footer>)。 - CSS代码组织在
<style>标签中,易于阅读和修改。 - JavaScript代码组织在
<script>标签中,负责处理交互逻辑。
- 使用了语义化的HTML5标签 (
完整代码
将以下所有代码复制并粘贴到一个新的文本文件中,将其命名为 christmas.html,然后用浏览器打开即可。
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">圣诞快乐!</title>
<style>
/* --- 全局样式 --- */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Georgia', serif;
background: linear-gradient(to bottom, #0c1445, #1a237e);
color: #fff;
height: 100vh;
overflow: hidden;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
position: relative;
}
/* --- 雪花效果 --- */
.snowflake {
position: absolute;
top: -10px;
color: #fff;
user-select: none;
cursor: default;
animation: fall linear infinite;
font-size: 1em;
opacity: 0.8;
}
@keyframes fall {
to {
transform: translateY(100vh);
}
}
/* --- 主容器 --- */
.container {
text-align: center;
z-index: 10;
padding: 20px;
}
/* --- 标题 --- */
h1 {
font-size: 4rem;
margin-bottom: 1rem;
color: #ff5252;
text-shadow: 0 0 10px #ff5252, 0 0 20px #ff5252, 0 0 30px #ff5252, 0 0 40px #ff5252;
animation: glow 2s ease-in-out infinite alternate;
}
@keyframes glow {
from { text-shadow: 0 0 10px #ff5252, 0 0 20px #ff5252, 0 0 30px #ff5252, 0 0 40px #ff5252; }
to { text-shadow: 0 0 20px #ff5252, 0 0 30px #ff5252, 0 0 40px #ff5252, 0 0 50px #ff5252; }
}
/* --- 圣诞树 --- */
.christmas-tree {
width: 0;
height: 0;
margin: 2rem auto;
border-left: 50px solid transparent;
border-right: 50px solid transparent;
border-bottom: 100px solid #2e7d32;
position: relative;
}
.tree-top {
width: 0;
height: 0;
border-left: 30px solid transparent;
border-right: 30px solid transparent;
border-bottom: 60px solid #1b5e20;
position: absolute;
top: -50px;
left: -30px;
}
.tree-star {
position: absolute;
top: -70px;
left: -15px;
color: #ffd600;
font-size: 2rem;
}
/* --- 礼物盒 --- */
.gifts {
margin: 2rem 0;
display: flex;
gap: 3rem;
justify-content: center;
}
.gift {
width: 80px;
height: 80px;
position: relative;
cursor: pointer;
transform-style: preserve-3d;
transition: transform 0.6s;
}
.gift:hover {
transform: rotateY(15deg) rotateX(-15deg);
}
.gift-box {
width: 100%;
height: 100%;
background-color: #d32f2f;
position: relative;
}
.gift-lid {
width: 100px;
height: 20px;
background-color: #b71c1c;
position: absolute;
top: -10px;
left: -10px;
transform-origin: left;
transition: transform 0.5s;
}
.gift-box.opened .gift-lid {
transform: rotateX(-120deg);
}
.gift-ribbon-v {
width: 15px;
height: 100%;
background-color: #ffd600;
position: absolute;
left: 50%;
transform: translateX(-50%);
}
.gift-ribbon-h {
width: 100%;
height: 15px;
background-color: #ffd600;
position: absolute;
top: 50%;
transform: translateY(-50%);
}
.gift-bow {
width: 30px;
height: 30px;
background-color: #ffd600;
border-radius: 50%;
position: absolute;
top: -25px;
left: 50%;
transform: translateX(-50%);
}
/* --- 祝福语 --- */
.message {
font-size: 1.5rem;
margin-top: 2rem;
min-height: 2em;
color: #81c784;
}
/* --- 音乐控制 --- */
.music-control {
position: absolute;
top: 20px;
right: 20px;
background: rgba(255, 255, 255, 0.2);
border: 2px solid #fff;
border-radius: 50%;
width: 50px;
height: 50px;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
transition: background 0.3s;
}
.music-control:hover {
background: rgba(255, 255, 255, 0.3);
}
/* --- 弹出祝福 --- */
.popup {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%) scale(0);
background: rgba(0, 0, 0, 0.9);
color: #fff;
padding: 2rem 3rem;
border-radius: 15px;
border: 3px solid #ff5252;
z-index: 100;
text-align: center;
transition: transform 0.3s ease-out;
}
.popup.show {
transform: translate(-50%, -50%) scale(1);
}
.popup h2 {
color: #ffd600;
margin-bottom: 1rem;
}
.popup-close {
margin-top: 1rem;
padding: 0.5rem 1.5rem;
background: #ff5252;
border: none;
color: #fff;
border-radius: 5px;
cursor: pointer;
font-size: 1rem;
}
.popup-close:hover {
background: #d32f2f;
}
/* --- 页脚 --- */
footer {
position: absolute;
bottom: 20px;
width: 100%;
text-align: center;
color: #aaa;
font-size: 0.9rem;
}
</style>
</head>
<body>
<!-- 音乐控制按钮 -->
<div class="music-control" id="musicControl" title="播放/暂停音乐">
<span id="musicIcon">🎵</span>
</div>
<!-- 主内容区 -->
<header class="container">
<h1>圣诞快乐!</h1>
<p>Merry Christmas!</p>
</header>
<main class="container">
<div class="christmas-tree">
<div class="tree-star">⭐</div>
<div class="tree-top"></div>
</div>
<div class="gifts">
<div class="gift" data-message="愿你的每一天都充满阳光和欢笑!">
<div class="gift-box">
<div class="gift-lid"></div>
<div class="gift-ribbon-v"></div>
<div class="gift-ribbon-h"></div>
<div class="gift-bow"></div>
</div>
</div>
<div class="gift" data-message="愿所有美好的事情都围绕在你身边!">
<div class="gift-box">
<div class="gift-lid"></div>
<div class="gift-ribbon-v"></div>
<div class="gift-ribbon-h"></div>
<div class="gift-bow"></div>
</div>
</div>
<div class="gift" data-message="祝你新年新气象,万事如意!">
<div class="gift-box">
<div class="gift-lid"></div>
<div class="gift-ribbon-v"></div>
<div class="gift-ribbon-h"></div>
<div class="gift-bow"></div>
</div>
</div>
</div>
<div class="message" id="dynamicMessage"></div>
</main>
<footer>
<p>© 2025 圣诞快乐 | Made with ❤️</p>
</footer>
<!-- 弹出祝福框 -->
<div class="popup" id="popup">
<h2>🎁 来自圣诞老人的礼物 🎁</h2>
<p id="popupMessage"></p>
<button class="popup-close" onclick="closePopup()">收下礼物</button>
</div>
<!-- 背景音乐 (请替换成你自己的音乐文件) -->
<audio id="bgMusic" loop>
<!-- 示例音乐,请替换为你的文件路径 -->
<source src="christmas_music.mp3" type="audio/mpeg">
您的浏览器不支持音频元素。
</audio>
<script>
// --- 雪花生成 ---
function createSnowflakes() {
const snowflakes = ['❄', '❅', '❆'];
const body = document.body;
const maxFlakes = 100;
for (let i = 0; i < maxFlakes; i++) {
const snowflake = document.createElement('div');
snowflake.className = 'snowflake';
snowflake.innerHTML = snowflakes[Math.floor(Math.random() * snowflakes.length)];
snowflake.style.left = Math.random() * 100 + 'vw';
snowflake.style.animationDuration = Math.random() * 3 + 2 + 's';
snowflake.style.opacity = Math.random();
snowflake.style.fontSize = Math.random() * 10 + 10 + 'px';
body.appendChild(snowflake);
// 动画结束后移除雪花,避免内存泄漏
snowflake.addEventListener('animationend', () => {
snowflake.remove();
});
}
}
createSnowflakes();
// 持续生成雪花
setInterval(createSnowflakes, 500);
// --- 音乐控制 ---
const musicControl = document.getElementById('musicControl');
const bgMusic = document.getElementById('bgMusic');
const musicIcon = document.getElementById('musicIcon');
let isPlaying = false;
musicControl.addEventListener('click', () => {
if (isPlaying) {
bgMusic.pause();
musicIcon.textContent = '🎵';
} else {
bgMusic.play().catch(e => console.log("自动播放被阻止,请用户手动点击。"));
musicIcon.textContent = '🔇';
}
isPlaying = !isPlaying;
});
// --- 礼物点击事件 ---
const gifts = document.querySelectorAll('.gift');
const dynamicMessageEl = document.getElementById('dynamicMessage');
const popup = document.getElementById('popup');
const popupMessageEl = document.getElementById('popupMessage');
gifts.forEach(gift => {
gift.addEventListener('click', () => {
const giftBox = gift.querySelector('.gift-box');
giftBox.classList.add('opened');
const message = gift.getAttribute('data-message');
popupMessageEl.textContent = message;
popup.classList.add('show');
// 防止重复点击
gift.style.pointerEvents = 'none';
});
});
function closePopup() {
popup.classList.remove('show');
}
// --- 动态祝福语 ---
const messages = [
"愿圣诞的钟声,带给你安宁与祥和。",
"愿你的世界,充满温暖与光明。",
"愿新的一年,所有的梦想都能实现。"
];
let messageIndex = 0;
let charIndex = 0;
let isDeleting = false;
const typingSpeed = 100;
const deletingSpeed = 50;
const pauseTime = 2000;
function typeWriter() {
const currentMessage = messages[messageIndex];
if (isDeleting) {
dynamicMessageEl.textContent = currentMessage.substring(0, charIndex - 1);
charIndex--;
} else {
dynamicMessageEl.textContent = currentMessage.substring(0, charIndex + 1);
charIndex++;
}
let speed = isDeleting ? deletingSpeed : typingSpeed;
if (!isDeleting && charIndex === currentMessage.length) {
speed = pauseTime;
isDeleting = true;
} else if (isDeleting && charIndex === 0) {
isDeleting = false;
messageIndex = (messageIndex + 1) % messages.length;
speed = 500; // 换行后短暂停顿
}
setTimeout(typeWriter, speed);
}
typeWriter();
</script>
</body>
</html>
如何使用和自定义
- 保存文件: 将上面的代码保存为
christmas.html。 - 添加背景音乐:
- 准备一个圣诞主题的MP3音乐文件,
christmas_music.mp3。 - 将这个音乐文件和
christmas.html放在同一个文件夹里。 - 代码中已经包含了音频标签,它会自动寻找并播放这个文件。
- 准备一个圣诞主题的MP3音乐文件,
- :
- 修改
<h1>标签里的文字。 - 祝福语: 修改
const messages数组里的文字,或者修改每个礼物盒data-message属性里的文字。 - 颜色: 修改CSS中的颜色值(
#ff5252是红色,#2e7d32是绿色)。 - 雪花数量: 修改
maxFlakes的值可以改变雪花的密度。
- 修改
这个设计结合了现代前端技术,创造了一个充满节日气氛和互动乐趣的网页,非常适合在圣诞节期间分享给朋友和家人。
