效果靜態圖:
動畫中包括:太陽及各行星,運行軌道,行星公轉動畫。
先畫好草圖,設計好大小和位置,根據公轉周期計算好動畫執行的時間。
html的結構:
一個class為solarsys的div,作為太陽系容器元素,該div的position為relative。
行星軌道和行星都用div,position為absolute。
容器用relative和內部元素采用absolute的定位方式,比較簡單的能實現效果,缺點就是大小是固定的。
太陽系容器div的css:
定寬,定高,relative定位,頁面內劇中對齊。
.solarsys{ width: 800px; height: 800px;; position: relative; margin: 0 auto; background-color: #000000; padding: 0; transform: scale(1); }
太陽div的css:
按照設計的大小和位置,設定寬高,left,top。
設定顏色。
通過把boder-radius生成50%,把一個正方形變成圓形。
通過box-shadow的4層顏色設置實現太陽光暈。
.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; }
行星軌道div的css:
假設是水星軌道。
按照設計的大小和位置,設定寬高,left,top。
背景色透明。
通過把boder-radius生成50%,把一個正方形變成圓形。
boder的類型設成虛線。
boder的顏色設成灰色。
寬度設1。
.mercuryOrbit { left:342.5px; top:342.5px; height: 115px; width: 115px; background-color: transparent; border-radius: 50%; border-style: dashed; border-color: gray; position: absolute; border-width: 1px; margin: 0px; padding: 0px; }
行星div的css:
假設是水星。
按照設計的大小和位置,設定寬高,left,top。
設置顏色。
通過把boder-radius生成50%,把一個正方形變成圓形。
將transfrom-origin設定成當前div的左上角相對于整個太陽系容器的中心點的橫向和縱向的偏移量。加上旋轉動畫后就是圍繞著中心點旋轉效果。
做一個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; }
rotate關鍵幀動畫:
逆時針旋轉。
@keyframes rotate { 100% { transform: rotate(-360deg); } }
基本結構完成。
僅在chrome中測試過。
全部代碼:
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com