js生成浪漫爱心的代码
时间: 2023-11-24 16:53:13 浏览: 97
以下是使用JS和CSS生成动态爱心的代码:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JS生成浪漫爱心</title>
<style>
body {
background-color: #000;
}
.heart {
position: absolute;
width: 100px;
height: 90px;
transform: rotate(-45deg);
top: 50%;
left: 50%;
margin-top: -45px;
margin-left: -50px;
}
.heart:before,
.heart:after {
position: absolute; content: "";
left: 50px;
top: 0;
width: 50px;
height: 80px;
background-color: red;
border-radius: 50px 50px 0 0;
transform: rotate(-45deg);
transform-origin: 0 100%;
}
.heart:after {
left: 0; transform: rotate(45deg);
transform-origin: 100% 100%;
}
</style>
</head>
<body>
<div class="heart"></div>
<script>
// 生成多个爱心
function createHeart() {
var heart = document.createElement("div");
heart.classList.add("heart");
heart.style.left = Math.random() * 100 + "%";
heart.style.animationDuration = Math.random() * 2 + 3 + "s";
document.body.appendChild(heart);
setTimeout(function() {
heart.remove();
}, 5000);
}
setInterval(createHeart, 300);
</script>
</body>
</html>
```
阅读全文