使用CSS3模拟太阳、地球、月球旋转的特效

要使用CSS3模拟太阳、地球和月球的旋转,你可以利用CSS3的动画和转换功能。以下是一个简单的示例,展示了如何使用HTML和CSS来模拟这个系统。

HTML

<div class="solar-system">
    <div class="sun"></div>
    <div class="earth-orbit">
        <div class="earth">
            <div class="moon-orbit">
                <div class="moon"></div>
            </div>
        </div>
    </div>
</div>

CSS

.solar-system {
    position: relative;
    width: 800px;
    height: 800px;
    margin: 50px auto;
}

.sun {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 100px;
    height: 100px;
    margin-top: -50px;
    margin-left: -50px;
    background-color: gold;
    border-radius: 50%;
}

.earth-orbit {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 500px;
    height: 500px;
    margin-top: -250px;
    margin-left: -250px;
    border: 1px dashed #000; /* Orbit path, optional */
    animation: rotate 10s linear infinite;
}

.earth {
    position: absolute;
    top: 50%;
    left: 0;
    width: 100px;
    height: 100px;
    margin-top: -50px;
    background-color: blue;
    border-radius: 50%;
}

.moon-orbit {
    position: absolute;
    top: 50%;
    left: 150px; /* Adjust this to change moon's orbit radius */
    width: 200px;
    height: 200px;
    margin-top: -100px;
    border: 1px dashed #000; /* Orbit path, optional */
    animation: rotate 2.5s linear infinite; /* Faster rotation for moon */
}

.moon {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 50px;
    height: 50px;
    margin-top: -25px;
    margin-left: -25px;
    background-color: gray;
    border-radius: 50%;
}

/* CSS Animation for rotation */
@keyframes rotate {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

这个示例中,.solar-system 是整个太阳系的容器。.sun 代表太阳,位于容器的中心。.earth-orbit 代表地球的轨道,它是一个以太阳为中心的圆形路径。.earth 是地球,它沿着 .earth-orbit 旋转。同样,.moon-orbit 是月球的轨道,它以地球为中心,而 .moon 则沿着这个轨道旋转。

通过调整CSS中的动画时长、轨道大小等参数,你可以进一步自定义这个太阳系模型的动态行为。

posted @   王铁柱6  阅读(60)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· 单线程的Redis速度为什么快?
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
点击右上角即可分享
微信分享提示