HTML 代码实现动态流星雨效果 - 简单易懂教程
<!DOCTYPE html>
<html>
<head>
<title>动态流星雨 - HTML 代码示例</title>
<style>
body {
margin: 0;
overflow: hidden;
}
.star {
position: absolute;
width: 2px;
height: 10px;
background-color: #fff;
opacity: 0;
pointer-events: none;
animation: shooting-star 1s linear infinite;
}
@keyframes shooting-star {
0% {
opacity: 1;
transform: translate(-200px, -200px);
}
100% {
opacity: 0;
transform: translate(300px, 300px);
}
}
</style>
</head>
<body>
<script>
function createStar() {
const star = document.createElement("div");
star.className = "star";
star.style.left = Math.random() * window.innerWidth + "px";
star.style.top = Math.random() * window.innerHeight + "px";
document.body.appendChild(star);
}
<pre><code> function createStars(num) {
for (let i = 0; i < num; i++) {
setTimeout(createStar, i * 100);
}
}
createStars(50); // 创建50个流星
</script>
</code></pre>
</body>
</html>
这段代码会在页面上创建50个流星,流星会从左上角开始斜向右下角移动,当移动到右下角后消失。你可以根据需要调整流星的数量和动画效果。
原文地址: https://www.cveoy.top/t/topic/pnRm 著作权归作者所有。请勿转载和采集!