css3中,可利用“animation-fill-mode”屬性實現(xiàn)動畫結(jié)束不消失效果,該屬性可規(guī)定動畫不播放時元素的樣式,當(dāng)屬性設(shè)置為forwards時,動畫效果不消失,語法為“animation-fill-mode:forwards”。
css3怎么實現(xiàn)動畫結(jié)束不消失效果
animation-fill-mode 屬性規(guī)定當(dāng)動畫不播放時(當(dāng)動畫完成時,或當(dāng)動畫有一個延遲未開始播放時),要應(yīng)用到元素的樣式。
默認(rèn)情況下,CSS 動畫在第一個關(guān)鍵幀播放完之前不會影響元素,在最后一個關(guān)鍵幀完成后停止影響元素。animation-fill-mode 屬性可重寫該行為。
CSS 語法
animation-fill-mode: none|forwards|backwards|both|initial|inherit;
none 默認(rèn)值。動畫在動畫執(zhí)行之前和之后不會應(yīng)用任何樣式到目標(biāo)元素。
forwards 在動畫結(jié)束后(由 animation-iteration-count 決定),動畫將應(yīng)用該屬性值。
backwards 動畫將應(yīng)用在 animation-delay 定義期間啟動動畫的第一次迭代的關(guān)鍵幀中定義的屬性值。這些都是 from 關(guān)鍵幀中的值(當(dāng) animation-direction 為 "normal" 或 "alternate" 時)或 to 關(guān)鍵幀中的值(當(dāng) animation-direction 為 "reverse" 或 "alternate-reverse" 時)。
both 動畫遵循 forwards 和 backwards 的規(guī)則。也就是說,動畫會在兩個方向上擴(kuò)展動畫屬性。
initial 設(shè)置該屬性為它的默認(rèn)值。
inherit 從父元素繼承該屬性。
示例如下:
<html> <head> <meta charset="utf-8"> <title>123</title> <style> div { width:100px; height:100px; background:red; position:relative; animation:mymove 3s; animation-iteration-count:2; animation-fill-mode:forwards; /* Safari 和 Chrome */ -webkit-animation:mymove 3s; -webkit-animation-iteration-count:2; -webkit-animation-fill-mode:forwards; } @keyframes mymove { from {top:0px;} to {top:200px;} } @-webkit-keyframes mymove /* Safari 和 Chrome */ { from {top:0px;} to {top:200px;} } </style> </head> <body> <p><strong>注意:</strong>Internet Explorer 9 及其之前的版本不支持 animation-fill-mode 屬性。</p> <div></div> </body> </html>
輸出結(jié)果: