目標是做一個太陽系八大行星的公轉動畫:不畫衛星,所有行星繞著太陽轉,行星本身先用純色表示,暫不做自轉。
靜態成果如下:

動畫包含三個部分:太陽與各行星、運行軌道、行星的公轉動畫。動手前先畫好草圖,定下各自的大小與位置,並依真實公轉週期換算出動畫應該跑多久。
最外層用一個 class 為 solarsys 的 div 當太陽系容器,position 設為 relative;軌道與行星都用 div,position 設為 absolute。容器 relative、內部 absolute 是最直白的搭配,優點是想得通、寫得快,缺點是尺寸被寫死,難以自適應。
<div class="solarsys">
<!-- 太陽 -->
<div class='sun'></div>
<!-- 水星軌道 -->
<div class='mercuryOrbit'></div>
<!-- 水星 -->
<div class='mercury'></div>
<!-- 金星軌道 -->
<div class='venusOrbit'></div>
<!-- 金星 -->
<div class='venus'></div>
<!-- 其餘行星依同一規則往下排 -->
</div>
固定寬高、relative 定位,並用 margin: 0 auto 在頁面中水平居中。
.solarsys{
width: 800px;
height: 800px;
position: relative;
margin: 0 auto;
background-color: #000000;
padding: 0;
transform: scale(1);
}
依設計設定寬高與 left、top,填色,再用 border-radius: 50% 把正方形變成正圓;最後用四層 box-shadow 做出光暈。
.sun {
left: 357px;
top: 357px;
height: 90px;
width: 90px;
background-color: rgb(248, 107, 35);
border-radius: 50%;
box-shadow: 5px 5px 10px rgb(248,107,35),
-5px -5px 10px rgb(248,107,35),
5px -5px 10px rgb(248,107,35),
-5px 5px 10px rgb(248,107,35);
position: absolute;
margin: 0;
}
設定寬高與位置,背景透明,border-radius: 50% 轉圓形,框線樣式改為 dashed、顏色灰色、粗細 1px。
.mercuryOrbit {
left: 342.5px;
top: 342.5px;
height: 115px;
width: 115px;
background-color: transparent;
border-radius: 50%;
border-style: dashed;
border-color: gray;
border-width: 1px;
position: absolute;
margin: 0px;
padding: 0px;
}
依設計設定寬高與位置,填色並轉為正圓。關鍵在 transform-origin:把它設成「該行星左上角相對於太陽系容器中心的橫向與縱向偏移量」,加上旋轉動畫後,行星便會繞著中心公转。最後用 animation 引用 rotate 關鍵影格,線性、無限播放,時長依公轉週期換算。
.mercury {
left: 337.5px;
top: 395px;
height: 10px;
width: 10px;
background-color: rgb(166, 138, 56);
border-radius: 50%;
position: absolute;
transform-origin: 62.5px 5px;
animation: rotate 1.5s infinite linear;
}
負角度代表逆時針旋轉。
@keyframes rotate {
100% {
transform: rotate(-360deg);
}
}
結構完全相同,只要替換數值即可。以下依原始程式整理:
| 行星 | 尺寸 | left / top | 顏色 | transform-origin | 公轉週期 |
|---|---|---|---|---|---|
| 水星 | 10×10 | 337.5 / 395 | rgb(166,138,56) | 62.5px 5px | 1.5s |
| 金星 | 22×22 | 309 / 389 | rgb(246,157,97) | 91px 11px | 3.84s |
| 地球 | 18×18 | 266.5 / 391 | rgb(115,114,174) | 134px 9px | 6.25s |
| 火星 | 15×15 | 222.5 / 392.5 | rgb(140,119,63) | 177.5px 7.5px | 11.75s |
| 木星 | 42×42 | 134 / 379 | rgb(156,164,143) | 266px 21px | 74.04s |
| 土星 | 26×26 | 92 / 387 | rgb(215,171,68) | 308px 13px | 183.92s |
| 天王星 | 27×27 | 41.5 / 386.5 | rgb(164,192,206) | 358.5px 13.5px | 524.46s |
| 海王星 | 20×20 | 10 / 390 | rgb(133,136,180) | 390px 10px | 1028.76s |
| 軌道 | 尺寸 | left / top |
|---|---|---|
| 水星軌道 | 115×115 | 342.5 / 342.5 |
| 金星軌道 | 160×160 | 320 / 320 |
| 地球軌道 | 250×250 | 275 / 275 |
| 火星軌道 | 340×340 | 230 / 230 |
| 木星軌道 | 490×490 | 155 / 155 |
| 土星軌道 | 590×590 | 105 / 105 |
| 天王星軌道 | 690×690 | 55 / 55 |
| 海王星軌道 | 760×760 | 20 / 20 |
可以注意到一個規律:行星的 top 都在 400 附近(也就是容器中心偏下),而 transform-origin 的橫向值等於「容器中心 400 減去行星 left 再加半個寬」,縱向值則是行星半高。抓準這個換算關係,要自訂任何尺寸與軌道半徑都不成問題。
原始範例仅在 Chrome 中測試過。若要跨瀏覽器,建議加上 -webkit- 前綴,並留意 prefers-reduced-motion——對偏好減少動態效果的使用者,可提供靜態版本或降低動畫速度,這在無障礙設計上是相當基本的一環。