HTML圣诞树代码(动态音效)
时间: 2023-12-25 22:29:14 浏览: 161
以下是一个使用HTML和CSS创建动态音效的圣诞树代码的例子:
```html
<!DOCTYPE html>
<html>
<head>
<style>
@keyframes blink {
0% { opacity: 1; }
50% { opacity: 0; }
100% { opacity: 1; }
}
.tree {
position: relative;
width: 200px;
height: 300px;
margin: 0 auto;
}
.tree:before {
content: "";
position: absolute;
top: 0;
left: 50%;
width: 0;
height: 0;
border-left: 100px solid transparent;
border-right: 100px solid transparent;
border-bottom: 150px solid green;
transform: translateX(-50%);
}
.tree:after {
content: "";
position: absolute;
top: 150px;
left: 50%;
width: 0;
height: 0;
border-left: 50px solid transparent;
border-right: 50px solid transparent;
border-top: 75px solid green;
transform: translateX(-50%);
animation: blink 1s infinite;
}
.star {
position: absolute;
top: -20px;
left: 50%;
width: 0;
height: 0;
border-left: 20px solid transparent;
border-right: 20px solid transparent;
border-bottom: 30px solid yellow;
transform: translateX(-50%);
}
.balls {
position: absolute;
top: 100px;
left: 50%;
transform: translateX(-50%);
}
.ball {
display: inline-block;
width: 20px;
height: 20px;
border-radius: 50%;
background-color: red;
margin: 5px;
animation: blink 1s infinite;
}
.sound {
position: absolute;
top: 250px;
left: 50%;
transform: translateX(-50%);
}
</style>
</head>
<body>
<div class="tree">
<div class="star"></div>
<div class="balls">
<div class="ball"></div>
<div class="ball"></div>
<div class="ball"></div>
<div class="ball"></div>
<div class="ball"></div>
</div>
<audio class="sound" autoplay loop>
<source src="christmas_music.mp3" type="audio/mpeg">
</audio>
</div>
</body>
</html>
```
这段代码使用了HTML和CSS来创建一个圣诞树,并添加了动态效果和音效。圣诞树的形状使用CSS的伪元素:before和:after来实现,动态效果使用了CSS的@keyframes和animation属性。音效使用了HTML5的audio元素,并通过设置autoplay和loop属性来实现自动播放和循环播放。
请注意,为了使音效正常播放,你需要将名为"christmas_music.mp3"的音频文件放在与HTML文件相同的目录下。
阅读全文