利用html和js生产一个星空背景,一颗行星,行星被行星环包裹着的html页面
时间: 2025-06-02 18:36:12 浏览: 9
为了创建一个HTML页面,包含星空背景、一颗移动的行星以及环绕行星的星环效果,我们可以使用HTML、CSS和JavaScript结合一些动画技术(比如`requestAnimationFrame`)。下面是一个简单的示例:
```html
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>星系宇宙</title>
<style>
body {
margin: 0;
overflow: hidden;
background-color: #000; /* 星空背景 */
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.planet-container {
position: relative;
width: 200px;
height: 200px;
border-radius: 50%;
background-color: #f0ad4e; /* 行星颜色 */
}
.ring {
position: absolute;
top: -50px;
left: -50px;
width: 400px;
height: 400px;
border-radius: 50%;
background-color: rgba(240, 173, 78, 0.6); /* 行星环颜色,半透明 */
transform-origin: bottom;
animation: ring-rotation 3s linear infinite;
}
@keyframes ring-rotation {
to { transform: rotate(360deg); }
}
.orbiting-planet {
position: absolute;
width: 100px;
height: 100px;
background-color: white;
border-radius: 50%;
animation: orbit 5s ease-in-out infinite;
}
@keyframes orbit {
0% { transform: translateX(-50px) translateY(-50px); }
50% { transform: translateX(200px) translateY(200px); }
100% { transform: translateX(-50px) translateY(-50px); }
}
</style>
</head>
<body>
<div class="planet-container">
<div class="orbiting-planet"></div>
<div class="ring"></div>
</div>
<script>
const planet = document.querySelector('.orbiting-planet');
function movePlanet() {
planet.style.transform = `translate(${Math.random() * 500}px, ${Math.random() * 500}px)`; // 随机位置
}
setInterval(movePlanet, 1000); // 每秒更新一次位置
</script>
</body>
</html>
```
这个示例创建了一个简单的页面,行星和星环围绕中心点做随机运动。你可以根据需要调整样式和动画效果。
阅读全文
相关推荐


















