动态的爱心树
时间: 2023-09-07 11:14:51 浏览: 132
动态树
4星 · 用户满意度95%
如果你想要制作一个动态的HTML爱心树,可以使用JavaScript和CSS3动画来实现。以下是一个简单的示例:
```html
<!DOCTYPE html>
<html>
<head>
<title>动态的HTML爱心树</title>
<style>
body {
background-color: #F3EFEF;
}
.tree {
position: relative;
margin: auto;
width: 0;
height: 0;
border-left: 50px solid transparent;
border-right: 50px solid transparent;
border-bottom: 100px solid green;
animation: grow 5s ease-in-out infinite;
}
.tree:before {
content: "";
position: absolute;
top: -50px;
left: -50px;
width: 0;
height: 0;
border-left: 50px solid transparent;
border-right: 50px solid transparent;
border-bottom: 100px solid green;
transform: rotate(180deg);
animation: grow 5s ease-in-out infinite;
}
.heart {
position: absolute;
top: -70px;
left: -10px;
width: 100px;
height: 100px;
transform: rotate(-45deg);
background-color: pink;
border-radius: 50%;
box-shadow: inset -20px 0px 0px white;
animation: pulse 1s ease-in-out infinite;
}
.heart:before, .heart:after {
content: "";
position: absolute;
top: 0px;
width: 100px;
height: 100px;
border-radius: 50%;
background-color: pink;
}
.heart:before {
left: -50px;
}
.heart:after {
left: 50px;
}
.text {
position: absolute;
top: 70px;
left: -50px;
width: 100px;
text-align: center;
font-size: 24px;
font-weight: bold;
color: white;
text-shadow: 2px 2px #555;
animation: pulse 1s ease-in-out infinite;
}
@keyframes grow {
0% {
width: 0;
height: 0;
}
50% {
width: 50px;
height: 100px;
}
100% {
width: 100px;
height: 200px;
}
}
@keyframes pulse {
0% {
transform: scale(1);
}
50% {
transform: scale(1.1);
}
100% {
transform: scale(1);
}
}
</style>
</head>
<body>
<div class="tree">
<div class="heart"></div>
<div class="text">Love</div>
</div>
<script>
// 每5秒更换一次背景颜色
setInterval(function() {
var colors = ['#F3EFEF', '#E3F9C7', '#FCE38A', '#F9D5E5'];
var color = colors[Math.floor(Math.random() * colors.length)];
document.body.style.backgroundColor = color;
}, 5000);
</script>
</body>
</html>
```
在上面的示例中,我们使用了CSS3动画来制作树和爱心的生长效果和心跳效果,并使用JavaScript定时器来更换背景颜色。你可以将上述代码复制到文本编辑器中,将文件保存为.html格式,然后在浏览器中打开,即可看到一个动态的HTML爱心树。
阅读全文