html写一个表白用的动态星空感的心形
时间: 2024-05-07 19:21:50 浏览: 139
HTML5/ CSS简约星空动态背景
5星 · 资源好评率100%
可以试试下面的代码实现:
```html
<!DOCTYPE html>
<html>
<head>
<title>动态星空感的心形表白</title>
<style type="text/css">
.heart {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 100px;
height: 90px;
background-color: #ff385c;
clip-path: polygon(50% 0, 82% 12%, 100% 44%, 82% 76%, 50% 90%, 18% 76%, 0 44%, 18% 12%);
animation: heartPulse 1.5s ease-in-out infinite;
animation-delay: 1s;
}
.heart:before,
.heart:after {
content: "";
position: absolute;
top: 0;
width: 100px;
height: 90px;
background-color: #ff385c;
border-radius: 50% 50% 0 0;
transform: rotate(45deg);
}
.heart:before {
left: -50px;
}
.heart:after {
left: 50px;
}
.star-wrapper {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
overflow: hidden;
z-index: -1;
}
.star {
position: absolute;
border-radius: 50%;
background-color: #fff;
opacity: 0.5;
animation: starPulse 1s ease-in-out infinite;
}
@keyframes heartPulse {
0% {
transform: scale(1);
}
50% {
transform: scale(1.2);
}
100% {
transform: scale(1);
}
}
@keyframes starPulse {
0% {
transform: scale(1) translate(-50%, -50%);
}
50% {
transform: scale(0.8) translate(-50%, -50%);
}
100% {
transform: scale(1) translate(-50%, -50%);
}
}
</style>
</head>
<body>
<div class="heart"></div>
<div class="star-wrapper">
<script type="text/javascript">
for (var i = 0; i < 100; i++) {
var star = document.createElement("div");
star.className = "star";
star.style.top = Math.floor(Math.random() * 100) + "%";
star.style.left = Math.floor(Math.random() * 100) + "%";
star.style.width = Math.floor(Math.random() * 5) + "px";
star.style.height = star.style.width;
document.body.appendChild(star);
}
</script>
</div>
</body>
</html>
```
其中,使用 clip-path 属性实现心形的裁剪,使用 animation 属性实现心形和星星的动画效果。在页面中添加了一个 div 元素作为星星的容器,通过 JavaScript 随机生成 100 个星星,并设置其位置和大小。
阅读全文