只用 CSS,不寫一行 JavaScript,就能做出一顆有體積感、會落地回彈、還會壓扁變形的小球。這個範例的關鍵在於三層結構:球體本身、球體上的高光,以及地面上的影子,三者用同一組動畫節奏驅動,看起來就像真的有重力。
建議使用 Chrome 或 Firefox 開啟預覽:點此查看效果。原始碼中額外寫入了 IE 條件式註解,只讓非 IE 瀏覽器顯示動畫區塊,因此舊版 IE 下會看不到小球。關於條件樣式的寫法,可參考 這篇說明 與 補充範例。
條件樣式的用法很短,就藏在頁面最上方:
<!--[if IE]>
<style>
.main-keleyi-com { display: none; }
#ballWrapper_keleyi_com { display: none; }
</style>
<![endif]-->
<section class="main-keleyi-com">
<div id="ballWrapper_keleyi_com">
<div id="ball"></div>
<div id="ballShadow"></div>
</div>
</section>
最外層的 wrapper 負責定位與「點擊縮小」的互動,#ball 是球體,#ballShadow 是地面投影。
一顆平面的圓要變成球,靠的是「上亮下暗」的線性漸層,加上兩層 inset 內陰影補上邊緣的明暗過渡。為了兼顧舊瀏覽器,這裡同時準備了 -moz-、-webkit-、-o-、-ms- 前綴與一份 base64 內嵌的 SVG 漸層當作後備。
#ball {
width: 140px;
height: 140px;
border-radius: 70px;
background: rgb(187, 187, 187);
background: linear-gradient(top, rgba(187,187,187,1) 0%, rgba(119,119,119,1) 99%);
box-shadow:
inset 0 -5px 15px rgba(255, 255, 255, 0.4),
inset -2px -1px 40px rgba(0, 0, 0, 0.4),
0 0 1px #000;
position: absolute;
top: 0;
z-index: 11;
animation: jump 1s infinite;
}
接著用偽元素 ::after 在球體左上方貼一塊橢圓形亮斑,模擬光源反射的高光。這塊亮斑使用上深下透明的漸層,並以 border-radius: 40px / 20px 拉成扁橢圓:
#ball::after {
content: "";
width: 80px;
height: 40px;
position: absolute;
left: 30px;
top: 10px;
z-index: 10;
background: linear-gradient(top,
rgba(232,232,232,1) 0%,
rgba(232,232,232,1) 1%,
rgba(255,255,255,0) 100%);
border-radius: 40px / 20px;
}
影子一開始被 scaleY(.3) 壓扁,並用動畫 shrink 讓它在小球落地那一刻縮小、加深,離地時再放大、變淡。
#ballShadow {
position: absolute;
left: 50%;
bottom: 0;
z-index: 10;
margin-left: -30px;
width: 60px;
height: 75px;
background: rgba(20, 20, 20, .1);
box-shadow: 0px 0 20px 35px rgba(20, 20, 20, .1);
border-radius: 30px / 40px;
transform: scaleY(.3);
animation: shrink 1s infinite;
}
jump 的設計巧思在於「關鍵時刻切換 timing-function」:上升段用 ease-in 模擬重力減速,接近地面時改成 ease-out 加速下落;50% 觸地、55% 壓扁、65% 反彈,細節都寫在百分比裡。
@keyframes jump {
0% { top: 0; animation-timing-function: ease-in; }
50% { top: 140px; height: 140px; animation-timing-function: ease-out; }
55% { top: 160px; height: 120px; border-radius: 70px / 60px;
animation-timing-function: ease-in; }
65% { top: 120px; height: 140px; border-radius: 70px;
animation-timing-function: ease-out; }
95% { top: 0; animation-timing-function: ease-in; }
100% { top: 0; animation-timing-function: ease-in; }
}
@keyframes shrink {
0% {
bottom: 0; margin-left: -30px;
width: 60px; height: 75px;
background: rgba(20, 20, 20, .1);
box-shadow: 0px 0 20px 35px rgba(20, 20, 20, .1);
border-radius: 30px / 40px;
animation-timing-function: ease-in;
}
50% {
bottom: 30px; margin-left: -10px;
width: 20px; height: 5px;
background: rgba(20, 20, 20, .3);
box-shadow: 0px 0 20px 35px rgba(20, 20, 20, .3);
border-radius: 20px / 20px;
animation-timing-function: ease-out;
}
100% {
bottom: 0; margin-left: -30px;
width: 60px; height: 75px;
background: rgba(20, 20, 20, .1);
box-shadow: 0px 0 20px 35px rgba(20, 20, 20, .1);
border-radius: 30px / 40px;
animation-timing-function: ease-in;
}
}
實作時別忘了為每個 keyframes 準備 @-webkit-keyframes、@-moz-keyframes、@-o-keyframes、@-ms-keyframes 版本,內容完全相同,只是前綴不同。
wrapper 上掛了 5 秒的 transition,配合 :active 把 scale 推到 0,按下滑鼠小球就會慢慢縮上不見,是個便宜又有效的小彩蛋。
#ballWrapper_keleyi_com {
width: 140px;
height: 300px;
position: fixed;
left: 50%;
top: 35%;
margin-left: -70px;
z-index: 100;
transform: scale(1);
transition: all 5s linear 0s;
cursor: pointer;
}
#ballWrapper_keleyi_com:active {
transform: scale(0);
}
這個範例沒有任何圖片素材,立體感完全來自漸層、內陰影與偽元素高光的組合;真實感則來自「球體與影子共用 1 秒週期、在 50% 精準對齊」這件事。想改成自己的版本,只要調整 animation 的時間、位移距離與壓扁比例即可。