想畫一顆看起來有體積感的球,徑向漸層(radial-gradient)是最直覺的工具;再搭配 @keyframes 與 border-radius,一顆會轉的球就完成了。
線上展示:https://hovertree.com/texiao/css/17/
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>旋轉的球體</title>
<style type="text/css">
/* 動畫定義 */
@keyframes spin {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
@-webkit-keyframes spin {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
/* 旋轉 + 徑向漸層上色 */
#advanced {
width: 200px;
height: 200px;
background-image: -moz-radial-gradient(45px 45px 45deg, circle cover, yellow 0%, orange 100%, red 95%);
background-image: -webkit-radial-gradient(45px 45px, circle cover, yellow, orange);
background-image: radial-gradient(45px 45px 45deg, circle cover, yellow 0%, orange 100%, red 95%);
animation: spin 2s cubic-bezier(0.9,1,0.2,1) infinite;
-webkit-animation: spin 2s cubic-bezier(0.9,1,0.2,1) infinite;
border-radius: 50%;
}
</style>
</head>
<body>
<div id="advanced"></div>
<a href="https://hovertree.com/">何問起</a>
</body>
</html>
漸層中心設在 45px 45px,也就是偏離正中央的位置,模擬光源從左上方打過來的效果,球的立體感就是這樣來的。顏色從中心的黃色过渡到橙、再到邊緣的紅,明暗變化越明顯,球體看起來越飽滿。
border-radius: 50% 把方形裁成正圓,是球體輪廓的基礎;cubic-bezier(0.9,1,0.2,1) 這條自訂曲線則讓旋轉的加減速不完全線性,看起來比 linear 更有生命力。