在線演示
html
按下右側的「點擊預覽」按鈕在當前頁面預覽,點擊連接全屏預覽。git
https://codepen.io/zhang-ou/pen/erRzzRgithub
此視頻是能夠交互的,你能夠隨時暫停視頻,編輯視頻中的代碼。chrome
請用 chrome, safari, edge 打開觀看。segmentfault
https://scrimba.com/c/cwrJys7dom
請從 github 下載。flex
https://github.com/comehope/front-end-daily-challenges/tree/master/011-ripple-pulse-loader-animation優化
定義 dom,只有一個元素:動畫
<div class="circle"></div>
居中顯示:url
html, body, .circle { height: 100%; display: flex; align-items: center; justify-content: center; background-color: black; }
畫出中間的實心圓:
:root { --innerRadius: 2em; } .circle { width: calc(var(--innerRadius) * 2); height: calc(var(--innerRadius) * 2); background-color: lime; border-radius: 50%; }
畫出圓環:
.circle { box-shadow: 0 0 0 calc(var(--innerRadius) - 0.4em) black, 0 0 0 var(--innerRadius) lime; }
用僞元素 ::before 畫出動畫用到的圓環:
.circle::before { content: ''; position: absolute; width: calc(var(--innerRadius) * 2 * 2); height: calc(var(--innerRadius) * 2 * 2); border: 2px solid lime; border-radius: 50%; }
增長動畫效果:
.circle::before animation: pulse 2s linear infinite; } @keyframes pulse { from { transform: scale(1); } to { transform: scale(2); } }
優化動畫——增長漸淡和彈性效果:
.circle::before animation: pulse 2s ease-out infinite; } @keyframes pulse { from { filter: opacity(0.9); } to { filter: opacity(0); } }
最後,用僞元素 ::after 再畫出一個動的圓環:
.circle::after { content: ''; position: absolute; width: calc(var(--innerRadius) * 2 * 2); height: calc(var(--innerRadius) * 2 * 2); border: 2px solid lime; border-radius: 50%; animation: pulse 2s ease-out infinite; } .circle::after { animation-delay: 1s; }
大功告成!
原文地址:https://segmentfault.com/a/1190000014700727