用js写一个爱心动画
时间: 2023-03-01 17:46:18 浏览: 60
您好!可以这样写:
```
<html>
<head>
<style>
#heart {
width: 100px;
height: 100px;
position: relative;
margin: 100px auto;
}
.heart-shape {
width: 100%;
height: 100%;
background-color: #ff0066;
border-radius: 50% 50% 0;
transform: rotate(45deg);
position: absolute;
}
.left-half {
width: 50%;
height: 100%;
left: 0;
top: 0;
border-radius: 0 50% 50% 0;
position: absolute;
overflow: hidden;
}
.right-half {
width: 50%;
height: 100%;
right: 0;
top: 0;
border-radius: 50% 0 0 50%;
position: absolute;
overflow: hidden;
}
.left-half:before {
content: "";
width: 50%;
height: 100%;
background-color: #ff0066;
border-radius: 0 50% 50% 0;
position: absolute;
top: 0;
left: -25%;
transform: rotate(-45deg);
}
.right-half:before {
content: "";
width: 50%;
height: 100%;
background-color: #ff0066;
border-radius: 50% 0 0 50%;
position: absolute;
top: 0;
right: -25%;
transform: rotate(45deg);
}
</style>
</head>
<body>
<div id="heart">
<div class="left-half"></div>
<div class="right-half"></div>
</div>
</body>
</html>
```
如果您想要动画效果,可以添加一些 CSS 动画:
```
<html>
<head>
<style>
#heart {
width: 100px;
height: 100px;
position: relative;
margin: 100px auto;
}
.heart-shape {
width: 100%;
height: 100%;
background-color: #ff0066;
border-radius: 50% 50% 0;
transform: rotate(45deg);
position: absolute;
}
.left-half {
width: 50%;
height: 100%;
left: 0;
top: 0;
border-radius: 0 50% 50% 0;
position: absolute;
overflow: hidden;
}
.right-half {
width: 50%;
height: 100%;
right: 0;
top: 0;
border-radius: 50% 0 0 50%;
position: absolute;
overflow: hidden;
}
.left-half:before {