html圣诞树动态音效
时间: 2023-12-18 16:03:57 浏览: 32
以下是一个HTML圣诞树动态音效的例子:
```html
<!DOCTYPE html>
<html>
<head>
<title>Christmas Tree with Music</title>
<style>
body {
background-color: #000;
}
.tree {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 200px;
height: 200px;
background-color: #006400;
border-radius: 0 0 50% 50%;
overflow: hidden;
}
.tree:before {
content: "";
position: absolute;
top: -50%;
left: 0;
width: 200px;
height: 200px;
background-color: #006400;
border-radius: 50%;
}
.tree:after {
content: "";
position: absolute;
top: 50%;
left: -50%;
width: 200px;
height: 200px;
background-color: #006400;
border-radius: 50%;
}
.tree .star {
position: absolute;
top: -25%;
left: 50%;
transform: translate(-50%, -50%);
width: 50px;
height: 50px;
background-color: #ffd700;
border-radius: 50%;
}
.tree .trunk {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, 0);
width: 50px;
height: 100px;
background-color: #8b4513;
border-radius: 0 0 10px 10px;
}
.tree .trunk:before {
content: "";
position: absolute;
top: 0;
left: -25%;
width: 50%;
height: 50%;
background-color: #8b4513;
border-radius: 50%;
}
.tree .trunk:after {
content: "";
position: absolute;
bottom: 0;
left: -25%;
width: 50%;
height: 50%;
background-color: #8b4513;
border-radius: 50%;
}
.tree .lights {
position: absolute;
top: 25%;
left: 50%;
transform: translate(-50%, 0);
width: 100%;
height: 50%;
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: center;
}
.tree .lights .light {
width: 10%;
height: 10%;
background-color: #fff;
border-radius: 50%;
animation: blink 1s infinite;
}
@keyframes blink {
0% {
opacity: 1;
}
50% {
opacity: 0.5;
}
100% {
opacity: 1;
}
}
</style>
</head>
<body>
<audio id="music" src="music.mp3" autoplay loop></audio>
<div class="tree">
<div class="star"></div>
<div class="trunk"></div>
<div class="lights">
<div class="light"></div>
<div class="light"></div>
<div class="light"></div>
<div class="light"></div>
<div class="light"></div>
<div class="light"></div>
<div class="light"></div>
<div class="light"></div>
<div class="light"></div>
<div class="light"></div>
</div>
</div>
</body>
</html>
```
这个例子中,我们使用HTML和CSS创建了一个圣诞树,并使用JavaScript播放了背景音乐。在HTML中,我们使用`<audio>`标签来嵌入音频文件,并使用`autoplay`和`loop`属性来自动播放和循环播放音乐。在CSS中,我们使用伪元素和`border-radius`属性来创建圣诞树的形状,并使用`overflow`属性来隐藏超出容器的部分。我们还使用`animation`属性来创建闪烁的灯光效果。在JavaScript中,我们使用`play()`方法来播放音乐。