🎄 用三行代码实现圣诞树?别逗了!让我们来真的!
🌟 圣诞节的正确打开方式
圣诞节快到了,是不是感觉家里缺了点什么?🎅 对,就是那棵 bling bling 的圣诞树!但是买真树太麻烦,买假树又没灵魂?没关系,今天我就教你用HTML+CSS+JS打造一棵属于你的「代码圣诞树」,让你的电脑屏幕充满节日气息!🎁
🛠️ 准备工作
在开始之前,我们需要准备:
- 一颗想搞事情的心 💡
- 一个文本编辑器(记事本也行,但我劝你用 VS Code)
- 一点 HTML+CSS+JS 基础
- 还有满脑子的圣诞精神 🎄
🎨 开始制作圣诞树
第一步:搭建骨架(HTML)
首先,我们需要给圣诞树搭个骨架。就像盖房子一样,先打地基!
html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>我的代码圣诞树 🎄</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="container">
<h1>🎅 Merry Christmas! 🎄</h1>
<div class="tree">
<!-- 圣诞树的树干 -->
<div class="trunk"></div>
<!-- 圣诞树的树冠,用三个三角形组成 -->
<div class="leaves leaves-1"></div>
<div class="leaves leaves-2"></div>
<div class="leaves leaves-3"></div>
<!-- 圣诞树上的装饰品 -->
<div class="decorations"></div>
<!-- 树顶星星 -->
<div class="star"></div>
</div>
<!-- 雪花效果 -->
<div class="snow"></div>
<!-- 礼物盒 -->
<div class="gifts"></div>
</div>
<script src="script.js"></script>
</body>
</html>
第二步:化妆打扮(CSS)
现在,我们需要给圣诞树穿上漂亮的衣服!这一步就像女朋友化妆,要细心!💄
css
/* 全局样式 */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
background: linear-gradient(to bottom, #1a1a2e 0%, #16213e 100%);
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
overflow: hidden;
font-family: "Arial", sans-serif;
}
.container {
text-align: center;
position: relative;
}
/* 标题样式 */
h1 {
color: #fff;
margin-bottom: 30px;
font-size: 2.5rem;
text-shadow: 0 0 10px #ff0, 0 0 20px #ff0, 0 0 30px #ff0;
animation: glow 2s ease-in-out infinite alternate;
}
/* 标题发光动画 */
@keyframes glow {
from {
text-shadow: 0 0 10px #ff0, 0 0 20px #ff0, 0 0 30px #ff0;
}
to {
text-shadow: 0 0 20px #ff0, 0 0 30px #ff0, 0 0 40px #ff0;
}
}
/* 圣诞树容器 */
.tree {
position: relative;
display: inline-block;
}
/* 树干样式 */
.trunk {
width: 40px;
height: 60px;
background-color: #8b4513;
position: absolute;
bottom: -60px;
left: 50%;
transform: translateX(-50%);
border-radius: 0 0 10px 10px;
}
/* 树冠样式 - 三个三角形叠加 */
.leaves {
width: 0;
height: 0;
border-left: transparent solid;
border-right: transparent solid;
border-bottom: green solid;
position: absolute;
left: 50%;
transform: translateX(-50%);
}
/* 第一层树冠 */
.leaves-1 {
border-left-width: 150px;
border-right-width: 150px;
border-bottom-width: 200px;
bottom: 0;
background: linear-gradient(to bottom, #228b22 0%, #006400 100%);
border-radius: 50% 50% 0 0;
}
/* 第二层树冠 */
.leaves-2 {
border-left-width: 120px;
border-right-width: 120px;
border-bottom-width: 160px;
bottom: 70px;
background: linear-gradient(to bottom, #228b22 0%, #006400 100%);
border-radius: 50% 50% 0 0;
}
/* 第三层树冠 */
.leaves-3 {
border-left-width: 90px;
border-right-width: 90px;
border-bottom-width: 120px;
bottom: 140px;
background: linear-gradient(to bottom, #228b22 0%, #006400 100%);
border-radius: 50% 50% 0 0;
}
/* 树顶星星 */
.star {
width: 0;
height: 0;
border-left: 25px solid transparent;
border-right: 25px solid transparent;
border-bottom: 43px solid #ffd700;
position: absolute;
left: 50%;
transform: translateX(-50%);
bottom: 250px;
animation: twinkle 1s ease-in-out infinite alternate;
}
/* 星星闪烁动画 */
@keyframes twinkle {
from {
transform: translateX(-50%) scale(1);
opacity: 0.8;
}
to {
transform: translateX(-50%) scale(1.1);
opacity: 1;
box-shadow: 0 0 20px #ffd700;
}
}
/* 星星的五个角 */
.star::before,
.star::after {
content: "";
width: 0;
height: 0;
border-left: 25px solid transparent;
border-right: 25px solid transparent;
border-bottom: 43px solid #ffd700;
position: absolute;
top: 0;
left: -25px;
}
.star::before {
transform: rotate(72deg);
}
.star::after {
transform: rotate(144deg);
}
/* 装饰品基础样式 */
.decoration {
width: 20px;
height: 20px;
border-radius: 50%;
position: absolute;
animation: blink 1.5s ease-in-out infinite alternate;
}
/* 装饰品闪烁动画 */
@keyframes blink {
from {
transform: scale(1);
opacity: 0.8;
}
to {
transform: scale(1.2);
opacity: 1;
box-shadow: 0 0 10px currentColor;
}
}
/* 不同颜色的装饰品 */
.decoration.red {
background-color: #ff0000;
box-shadow: 0 0 10px #ff0000;
}
.decoration.blue {
background-color: #0000ff;
box-shadow: 0 0 10px #0000ff;
}
.decoration.yellow {
background-color: #ffff00;
box-shadow: 0 0 10px #ffff00;
}
.decoration.pink {
background-color: #ff1493;
box-shadow: 0 0 10px #ff1493;
}
/* 雪花样式 */
.snowflake {
position: absolute;
background-color: #fff;
border-radius: 50%;
animation: fall linear infinite;
opacity: 0.8;
}
/* 雪花下落动画 */
@keyframes fall {
from {
transform: translateY(-100px) rotate(0deg);
opacity: 0;
}
10% {
opacity: 0.8;
}
to {
transform: translateY(100vh) rotate(360deg);
opacity: 0;
}
}
/* 礼物盒容器 */
.gifts {
position: absolute;
bottom: -100px;
left: 50%;
transform: translateX(-50%);
display: flex;
gap: 20px;
}
/* 礼物盒样式 */
.gift {
width: 60px;
height: 60px;
position: relative;
animation: bounce 2s ease-in-out infinite;
}
/* 礼物盒弹跳动画 */
@keyframes bounce {
0%,
100% {
transform: translateY(0);
}
50% {
transform: translateY(-10px);
}
}
/* 不同颜色的礼物盒 */
.gift.red {
background-color: #ff0000;
}
.gift.green {
background-color: #008000;
}
.gift.blue {
background-color: #0000ff;
}
.gift.yellow {
background-color: #ffff00;
}
/* 礼物盒丝带 */
.gift::before,
.gift::after {
content: "";
position: absolute;
background-color: #fff;
}
.gift::before {
width: 100%;
height: 10px;
top: 50%;
transform: translateY(-50%);
}
.gift::after {
width: 10px;
height: 100%;
left: 50%;
transform: translateX(-50%);
}
第三步:让它动起来(JS)
现在,我们的圣诞树还只是个「静态美人」,让我们用 JavaScript 给它注入灵魂!✨
javascript
// 圣诞树装饰品生成
function createDecorations() {
const decorationsContainer = document.querySelector(".decorations");
const colors = ["red", "blue", "yellow", "pink"];
const count = 20;
for (let i = 0; i < count; i++) {
const decoration = document.createElement("div");
decoration.className = `decoration ${
colors[Math.floor(Math.random() * colors.length)]
}`;
// 随机位置(在树冠范围内)
const angle = Math.random() * Math.PI * 2;
const radius = Math.random() * 120 + 30;
const x = Math.cos(angle) * radius;
const y = Math.sin(angle) * radius - 100;
decoration.style.left = `calc(50% + ${x}px)`;
decoration.style.bottom = `${y}px`;
decoration.style.animationDelay = `${Math.random() * 2}s`;
decorationsContainer.appendChild(decoration);
}
}
// 雪花生成器
function createSnow() {
const snowContainer = document.querySelector(".snow");
const snowflakeCount = 100;
for (let i = 0; i < snowflakeCount; i++) {
const snowflake = document.createElement("div");
snowflake.className = "snowflake";
// 随机大小
const size = Math.random() * 8 + 2;
snowflake.style.width = `${size}px`;
snowflake.style.height = `${size}px`;
// 随机位置
snowflake.style.left = `${Math.random() * 100}vw`;
// 随机下落速度
const duration = Math.random() * 10 + 5;
snowflake.style.animationDuration = `${duration}s`;
// 随机延迟
snowflake.style.animationDelay = `${Math.random() * 5}s`;
snowContainer.appendChild(snowflake);
}
}
// 礼物盒生成
function createGifts() {
const giftsContainer = document.querySelector(".gifts");
const colors = ["red", "green", "blue", "yellow"];
const count = 4;
for (let i = 0; i < count; i++) {
const gift = document.createElement("div");
gift.className = `gift ${
colors[Math.floor(Math.random() * colors.length)]
}`;
gift.style.animationDelay = `${i * 0.5}s`;
giftsContainer.appendChild(gift);
}
}
// 页面加载完成后执行
window.addEventListener("DOMContentLoaded", () => {
createDecorations();
createSnow();
createGifts();
});
🎉 让圣诞树跑起来
现在,让我们把所有代码合并到一个完整的 HTML 文件中,你可以直接复制下面的代码保存为 christmas-tree.html,然后用浏览器打开它,就能看到你的圣诞树了!🎄
html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>🎄 我的代码圣诞树</title>
<style>
/* 全局样式 */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
background: linear-gradient(to bottom, #1a1a2e 0%, #16213e 100%);
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
overflow: hidden;
font-family: "Arial", sans-serif;
margin: 0;
padding: 0;
}
.container {
text-align: center;
position: relative;
height: 500px;
width: 600px;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
margin: 0 auto;
}
/* 标题样式 */
h1 {
color: #fff;
margin-bottom: 100px;
font-size: 2.5rem;
text-shadow: 0 0 10px #ff0, 0 0 20px #ff0, 0 0 30px #ff0;
animation: glow 2s ease-in-out infinite alternate;
z-index: 20;
position: relative;
}
/* 标题发光动画 */
@keyframes glow {
from {
text-shadow: 0 0 10px #ff0, 0 0 20px #ff0, 0 0 30px #ff0;
}
to {
text-shadow: 0 0 20px #ff0, 0 0 30px #ff0, 0 0 40px #ff0;
}
}
/* 圣诞树容器 */
.tree {
position: relative;
display: inline-block;
}
/* 树干样式 */
.trunk {
width: 40px;
height: 60px;
background-color: #8b4513;
position: absolute;
bottom: -60px;
left: 50%;
transform: translateX(-50%);
border-radius: 0 0 10px 10px;
}
/* 树冠样式 - 三个三角形叠加 */
.leaves {
width: 0;
height: 0;
position: absolute;
left: 50%;
transform: translateX(-50%);
filter: drop-shadow(0 0 10px rgba(0, 255, 0, 0.3));
}
/* 第一层树冠 */
.leaves-1 {
border-left: 150px solid transparent;
border-right: 150px solid transparent;
border-bottom: 200px solid #2e8b57;
bottom: 0;
animation: sway 3s ease-in-out infinite alternate;
}
/* 第二层树冠 */
.leaves-2 {
border-left: 120px solid transparent;
border-right: 120px solid transparent;
border-bottom: 160px solid #3cb371;
bottom: 70px;
animation: sway 3s ease-in-out infinite alternate-reverse;
}
/* 第三层树冠 */
.leaves-3 {
border-left: 90px solid transparent;
border-right: 90px solid transparent;
border-bottom: 120px solid #228b22;
bottom: 140px;
animation: sway 3s ease-in-out infinite alternate;
}
/* 树摇摆动画 */
@keyframes sway {
from {
transform: translateX(-50%) rotate(-1deg);
}
to {
transform: translateX(-50%) rotate(1deg);
}
}
/* 树顶星星 - 使用更简单的方式实现 */
.star {
width: 50px;
height: 50px;
background-color: #ffd700;
clip-path: polygon(
50% 0%,
61% 35%,
98% 35%,
68% 57%,
79% 91%,
50% 70%,
21% 91%,
32% 57%,
2% 35%,
39% 35%
);
position: absolute;
left: 50%;
transform: translateX(-50%);
bottom: 250px;
animation: twinkle 1s ease-in-out infinite alternate;
z-index: 10;
}
/* 星星闪烁动画 */
@keyframes twinkle {
from {
transform: translateX(-50%) scale(1);
opacity: 0.8;
}
to {
transform: translateX(-50%) scale(1.1);
opacity: 1;
box-shadow: 0 0 20px #ffd700;
}
}
/* 装饰品基础样式 */
.decoration {
width: 20px;
height: 20px;
border-radius: 50%;
position: absolute;
animation: blink 1.5s ease-in-out infinite alternate;
box-shadow: 0 0 10px currentColor;
}
/* 装饰品闪烁动画 */
@keyframes blink {
from {
transform: scale(1) rotate(0deg);
opacity: 0.8;
}
to {
transform: scale(1.3) rotate(360deg);
opacity: 1;
box-shadow: 0 0 20px currentColor, 0 0 30px currentColor;
}
}
/* 不同颜色的装饰品,增加发光效果 */
.decoration.red {
background-color: #ff0000;
box-shadow: 0 0 15px #ff0000, inset 0 0 5px rgba(255, 255, 255, 0.5);
}
.decoration.blue {
background-color: #0000ff;
box-shadow: 0 0 15px #0000ff, inset 0 0 5px rgba(255, 255, 255, 0.5);
}
.decoration.yellow {
background-color: #ffff00;
box-shadow: 0 0 15px #ffff00, inset 0 0 5px rgba(255, 255, 255, 0.5);
}
.decoration.pink {
background-color: #ff1493;
box-shadow: 0 0 15px #ff1493, inset 0 0 5px rgba(255, 255, 255, 0.5);
}
/* 添加一些不同大小的装饰品 */
.decoration.large {
width: 25px;
height: 25px;
}
.decoration.small {
width: 15px;
height: 15px;
animation-duration: 2s;
}
/* 雪花样式 */
.snowflake {
position: absolute;
background-color: #fff;
border-radius: 50%;
animation: fall linear infinite;
opacity: 0.8;
}
/* 雪花下落动画 */
@keyframes fall {
from {
transform: translateY(-100px) rotate(0deg);
opacity: 0;
}
10% {
opacity: 0.8;
}
to {
transform: translateY(100vh) rotate(360deg);
opacity: 0;
}
}
/* 礼物盒容器 */
.gifts {
position: absolute;
bottom: -80px;
left: 50%;
transform: translateX(-50%);
display: flex;
gap: 25px;
z-index: 5;
}
/* 礼物盒样式 - 立体效果 */
.gift {
width: 50px;
height: 40px;
position: relative;
animation: bounce 2s ease-in-out infinite;
border-radius: 3px;
box-shadow: 0 5px 10px rgba(0, 0, 0, 0.3);
}
/* 礼物盒弹跳动画 - 更自然的效果 */
@keyframes bounce {
0%,
100% {
transform: translateY(0) scale(1);
}
50% {
transform: translateY(-15px) scale(1.05);
}
}
/* 不同颜色的礼物盒,添加渐变和立体效果 */
.gift.red {
background: linear-gradient(135deg, #ff0000 0%, #cc0000 100%);
}
.gift.green {
background: linear-gradient(135deg, #008000 0%, #006400 100%);
}
.gift.blue {
background: linear-gradient(135deg, #0000ff 0%, #0000cc 100%);
}
.gift.yellow {
background: linear-gradient(135deg, #ffff00 0%, #cccc00 100%);
}
/* 礼物盒盖子 - 立体效果 */
.gift::before {
content: "";
position: absolute;
top: -8px;
left: 0;
right: 0;
height: 8px;
background: linear-gradient(
135deg,
rgba(255, 255, 255, 0.3) 0%,
rgba(255, 255, 255, 0.1) 100%
);
border-radius: 2px 2px 0 0;
box-shadow: 0 -2px 5px rgba(0, 0, 0, 0.2);
}
/* 礼物盒丝带 - 更美观的设计 */
.gift::after {
content: "";
position: absolute;
background-color: #fff;
width: 8px;
height: 100%;
left: 50%;
transform: translateX(-50%);
box-shadow: 0 0 3px rgba(0, 0, 0, 0.3);
}
/* 礼物盒底部丝带 */
.gift {
position: relative;
}
/* 礼物盒丝带装饰 */
.gift span {
position: absolute;
background-color: #fff;
width: 100%;
height: 8px;
top: 50%;
transform: translateY(-50%);
box-shadow: 0 0 3px rgba(0, 0, 0, 0.3);
}
</style>
</head>
<body>
<div class="container">
<h1>🎅 Merry Christmas! 🎄</h1>
<div class="tree">
<!-- 圣诞树的树干 -->
<div class="trunk"></div>
<!-- 圣诞树的树冠,用三个三角形组成 -->
<div class="leaves leaves-1"></div>
<div class="leaves leaves-2"></div>
<div class="leaves leaves-3"></div>
<!-- 圣诞树上的装饰品 -->
<div class="decorations"></div>
<!-- 树顶星星 -->
<div class="star"></div>
</div>
<!-- 雪花效果 -->
<div class="snow"></div>
<!-- 礼物盒 -->
<div class="gifts"></div>
</div>
<script>
// 圣诞树装饰品生成
function createDecorations() {
const decorationsContainer = document.querySelector(".decorations");
const colors = ["red", "blue", "yellow", "pink"];
const sizes = ["", "large", "small"];
const count = 25; // 增加数量,让树更丰富
for (let i = 0; i < count; i++) {
const decoration = document.createElement("div");
decoration.className = `decoration ${
colors[Math.floor(Math.random() * colors.length)]
} ${sizes[Math.floor(Math.random() * sizes.length)]}`;
// 简单的随机位置,确保在树内部
const x = Math.random() * 200 - 100; // -100到100之间
const y = Math.random() * 180; // 0到180之间
// 确保在三角形树冠范围内
const distanceFromCenter = Math.abs(x);
const maxWidthAtHeight = 150 - (y / 180) * 100;
if (distanceFromCenter < maxWidthAtHeight) {
decoration.style.left = `calc(50% + ${x}px)`;
decoration.style.bottom = `${y}px`;
decoration.style.animationDelay = `${Math.random() * 2}s`;
decoration.style.zIndex = 2;
decorationsContainer.appendChild(decoration);
}
}
}
// 雪花生成器
function createSnow() {
const snowContainer = document.querySelector(".snow");
const snowflakeCount = 100;
for (let i = 0; i < snowflakeCount; i++) {
const snowflake = document.createElement("div");
snowflake.className = "snowflake";
// 随机大小
const size = Math.random() * 8 + 2;
snowflake.style.width = `${size}px`;
snowflake.style.height = `${size}px`;
// 随机位置
snowflake.style.left = `${Math.random() * 100}vw`;
// 随机下落速度
const duration = Math.random() * 10 + 5;
snowflake.style.animationDuration = `${duration}s`;
// 随机延迟
snowflake.style.animationDelay = `${Math.random() * 5}s`;
snowContainer.appendChild(snowflake);
}
}
// 礼物盒生成
function createGifts() {
const giftsContainer = document.querySelector(".gifts");
const colors = ["red", "green", "blue", "yellow"];
const count = 5; // 增加一个礼物盒
for (let i = 0; i < count; i++) {
const gift = document.createElement("div");
gift.className = `gift ${
colors[Math.floor(Math.random() * colors.length)]
}`;
gift.style.animationDelay = `${i * 0.3}s`;
// 添加丝带装饰
const ribbon = document.createElement("span");
gift.appendChild(ribbon);
giftsContainer.appendChild(gift);
}
}
// 页面加载完成后执行
window.addEventListener("DOMContentLoaded", () => {
createDecorations();
createSnow();
createGifts();
});
</script>
</body>
</html>
🎨 代码解析
1. 圣诞树的结构 🏗️
圣诞树的结构其实很简单:
- 树干:一个棕色的长方形
- 树冠:三个大小不一的三角形叠加在一起
- 树顶星星:一个金色的五角星(用 CSS 边框实现)
- 装饰品:彩色的小圆点,随机分布在树冠上
- 雪花:白色的小圆点,从天上飘落
- 礼物盒:彩色的正方形,带有白色丝带
2. CSS 的魔法 ✨
- 渐变背景:让树干和树冠看起来更有层次感
- 动画效果 :
- 标题发光动画
glow - 星星闪烁动画
twinkle - 装饰品闪烁动画
blink - 雪花下落动画
fall - 礼物盒弹跳动画
bounce
- 标题发光动画
- 定位技巧 :使用
position: absolute和transform: translateX(-50%)让元素居中
3. JavaScript 的灵魂 🧠
- 动态生成装饰品:随机位置、随机颜色、随机闪烁延迟
- 雪花生成器:100 片雪花,随机大小、随机速度、随机位置
- 礼物盒生成:4 个不同颜色的礼物盒,带有弹跳效果
🎁 扩展功能
如果你觉得这个圣诞树还不够炫酷,你可以尝试:
- 添加音乐 :用 HTML5 的
audio标签添加圣诞歌曲 🎵 - 交互效果:点击圣诞树会下雪或播放音乐 🎶
- 3D 效果:使用 CSS 3D 变换让圣诞树旋转 🌀
- 更多装饰品:添加彩灯、铃铛、袜子等 🧦
🤣 程序员的圣诞节
作为一个程序员,我们的圣诞节是这样的:
- 别人在装饰圣诞树,我们在装饰代码
- 别人在拆礼物,我们在拆 bug
- 别人在吃火鸡,我们在吃外卖
- 别人在看春晚,我们在看技术文档
但是没关系,我们有属于自己的快乐!当看到自己写的圣诞树在屏幕上闪闪发光时,那种成就感是无法言喻的!🌟
🎄 结语
好了,今天的圣诞树教程就到这里了!希望你能喜欢这个代码圣诞树,也希望你能在圣诞节收获满满的快乐和幸福!🎅
记住,生活就像圣诞树,需要我们用心去装饰,才能变得更加美好!✨
最后,祝大家:
- 圣诞快乐!🎄
- 代码无 bug!🐛❌
- 工资涨不停!💰
- 永远不脱发!👨💻👩💻
Merry Christmas and Happy New Year! 🎉
💡 小贴士:如果你觉得这个圣诞树不错,别忘了分享给你的朋友,让他们也感受一下程序员的圣诞浪漫!😂