標籤:理解 gif css ... bottom anim over 階段 key
今天,閑來無事去逛論壇,忽然發現了一個有意思的效果,果斷
我擦嘞,以前聽說過這種外掛程式,但是感覺寫個這個幹嘛要廢那麼多事,but,這傢伙明顯不是用的js啊,開啟代碼一看
what??? steps()...這是個什麼鬼?
animation-timing-function是主要畫面格動畫的動畫速率函數,這我知道,但是從來沒聽說過steps()啊,
上網一搜,steps函數是指定一個階躍函數,腦子就開始暈了,後面越看越暈,實在不行,直接動手寫,這一寫問題出來了.
我原本以為steps()中的第一個參數,是指定主要畫面格動畫一共分幾步完成,實際的情況:他妹的每個間隔分幾步完成.
不懂我的意思?沒關係,上代碼...
<div class="box"> <p class="p0"></p></div>
.box{ box-sizing: content-box; border: 1px solid #000; display: inline-block; margin: 50px 50px; width: 100px;}.p0{ width: 0px; height: 30px; background-color: #f00; animation: wid 5s infinite; animation-timing-function: steps(3,start);}@keyframes wid{ 0%{ width: 0px; } 100%{ width: 100px; }}
初看,沒有什麼問題,但是現在我們把其中的主要畫面格動畫的設定換一下
@keyframes wid{ 0%{ width: 0px; } 60%{ width: 100px; } 100%{ width: 0px; }}
效果如
這麼看來,就能知道,steps()的第一個參數,並不是把主要畫面格整個動畫分成幾步來執行,而是,每個階段都分步執行.知道了這些,接下來就好理解了.
第二個參數可選,接受 start 和 end 兩個值,指定在每個間隔的起點或是終點發生階躍變化,預設為 end,通俗點來講:
step-start在變化過程中,都是以下一幀的顯示效果來填充間隔動畫,
step-end與上面相反,都是以上一幀的顯示效果來填充間隔動畫,
即2個參數都會選擇性的跳過前後部分,start跳過0%,end跳過100%
講解部分,就這麼多,最關鍵,最核心的部分就是:timing-function 作用於每兩個主要畫面格之間,而不是整個動畫.
理解了上面內容,我也模仿著寫了一些小demo
demo 1:
.p0{ white-space: nowrap; overflow: hidden; width: 8rem; margin: 20px auto; animation: wid 10s infinite,typing 1s infinite; animation-timing-function: steps(8,end),steps(2,end); /*font-family: Consolas, Monaco, monospace;*/ font-size: 1rem; border-right: 1px solid transparent;}@keyframes wid{ 0%{ width: 0rem; } 50%{ width: 8rem; } 100%{ width: 8rem; }}@keyframes typing{ 0%{ border-right: 1px solid transparent; } 50%{ border-right: 1px solid blue; } 100%{ border-right: 1px solid transparent; }}
<p class="p0">一二三四五六七八</p>
效果如下:
demo 2:
.p0{ margin: 50px 50px; width: 90px; padding-left: 10px; height: 24px; border: 1px solid blueviolet; background-color: blueviolet; color: #fff; font-size: 14px; line-height: 24px; /*text-align: center;*/}.p0 span{ vertical-align: bottom; overflow: hidden; white-space: nowrap; width: 14px; display: inline-block; animation: wid 2s infinite; animation-timing-function: steps(4,end);}@keyframes wid{ 0%{ width: 0px; } 100%{ width: 14px; }}
<p class="p0">載入中<span>…</span></p>
其實,這些結合等寬字型效果更好,但是...
以後有時間了,再補上.
探究css幀動畫setps()用處