=====================================================================css
安利一下個人開源項目: 前端監控系統 html
=====================================================================前端
因爲CSS3動畫對低版本的瀏覽器的支持效果並非很好,特別是IE9及如下版本,更是沒法支持。 因此有時候一些簡單的動畫效果,還只是用js代碼來實現,可是效率偏低,並且效果有些偏生硬,不夠順滑。 畢竟用js代碼來實現動畫並不是正確之道。ios
雖然說css實現動畫效果看起來更順滑一些,可是也只是相對而言,由於css3動畫的也是一幀一幀的執行,因爲多種因素的影響,細看之下也未必是真的順滑,因此只要把動畫設計讓眼睛感受到是順滑的就能達到完美的效果。css3
在css3中,咱們能夠經過@keyframes
建立關鍵幀動畫效果。咱們須要將@keyframes
綁定到選擇器中,不然不會有效果出現。同時,咱們還需定義動畫時長和動畫名稱web
語法(@keyframes):windows
@keyframes animationname {keyframes-selector {css-styles;}}
值 | 描述 |
animationname | 必需,定義動畫的名稱 |
keyframes-selector | 必需,動畫運行過程當中的百分比 |
在css3中,咱們以百分比來規定改變發生的時間,或者經過關鍵詞 "from
" 和 "to
",等價於 0% 和 100%。其中,0% 是動畫的開始時間,100% 動畫的結束時間。瀏覽器
百分比分能夠分解成無限多個,分解的越多,軌跡越複雜,達到的效果越好,這個須要在設計的時候細細琢磨css3動畫
@keyframes myfirst
{ from {background: red;} to {background: yellow;} } @-moz-keyframes myfirst /* Firefox */ { from {background: red;} to {background: yellow;} } @-webkit-keyframes myfirst /* Safari 和 Chrome */ { from {background: red;} to {background: yellow;} } @-o-keyframes myfirst /* Opera */ { from {background: red;} to {background: yellow;} }
//或者用(%) 來指定運行過程當中的行爲
@keyframes myfirst
{
0% {background: red;}
25% {background: yellow;}
50% {background: blue;}
100% {background: green;}
}
@-moz-keyframes myfirst /* Firefox */
{
0% {background: red;}
25% {background: yellow;}
50% {background: blue;}
100% {background: green;}
}
@-webkit-keyframes myfirst /* Safari 和 Chrome */ { 0% {background: red;} 25% {background: yellow;} 50% {background: blue;} 100% {background: green;} } @-o-keyframes myfirst /* Opera */ { 0% {background: red;} 25% {background: yellow;} 50% {background: blue;} 100% {background: green;} }
語法(animation)ide
animation
屬性是一個簡寫屬性,用於設置動畫的屬性:
根據以上各類屬性來設置動畫在運行過程的各類狀態來達到想要的效果。
代碼示例:
運行名爲 myfirst 的動畫,其中設置了全部動畫屬性: div { animation-name: myfirst; animation-duration: 5s; animation-timing-function: linear; animation-delay: 2s; animation-iteration-count: infinite; animation-direction: alternate; animation-play-state: running; /* Firefox: */ -moz-animation-name: myfirst; -moz-animation-duration: 5s; -moz-animation-timing-function: linear; -moz-animation-delay: 2s; -moz-animation-iteration-count: infinite; -moz-animation-direction: alternate; -moz-animation-play-state: running; /* Safari 和 Chrome: */ -webkit-animation-name: myfirst; -webkit-animation-duration: 5s; -webkit-animation-timing-function: linear; -webkit-animation-delay: 2s; -webkit-animation-iteration-count: infinite; -webkit-animation-direction: alternate; -webkit-animation-play-state: running; /* Opera: */ -o-animation-name: myfirst; -o-animation-duration: 5s; -o-animation-timing-function: linear; -o-animation-delay: 2s; -o-animation-iteration-count: infinite; -o-animation-direction: alternate; -o-animation-play-state: running; } 與上面的動畫相同,可是使用了簡寫的動畫 animation 屬性:(兩者用其一便可) div { animation: myfirst 5s linear 2s infinite alternate; /* Firefox: */ -moz-animation: myfirst 5s linear 2s infinite alternate; /* Safari 和 Chrome: */ -webkit-animation: myfirst 5s linear 2s infinite alternate; /* Opera: */ -o-animation: myfirst 5s linear 2s infinite alternate; }
動畫效果雖然建立出來了,可是還須要配合js代碼才能在合適的時機執行動畫效果
調用動畫的方式有兩種:
//方式一,給元素style的WebkitAnimation 、animation 添加行爲
var x = document.getElementById("myDIV"); // 使用 JavaScript 開始動畫 function myFunction() { x.style.WebkitAnimation = "myfirst 5s linear 2s infinite alternate";
// Chrome, Safari 和 Opera 代碼
x.style.animation = "myfirst 5s linear 2s infinite alternate";
}
//方式二, 給元素添加上包含動畫的類名(class) ,在動畫結束是remove掉。
//爲動畫監聽結束事件 *** 這裏有一個比較嚴重的問題, 在咱們用的時候, 有可能會屢次執行下邊的代碼,就是爲 //元素重複綁定監聽事件,會致使動畫出現卡頓現象,必定要避免
if(x){
x.addEventListener("webkitAnimationEnd", function(){ //動畫結束時事件
eListenerEnd(bgflag,bgflag2);
}, false);
x.addEventListener("animationend", function(){ //動畫結束時事件
eListenerEnd(bgflag,bgflag2);
}, false);
}
兼容性問題一
動畫執行的很漂亮,可是在Safari中表現的極爲差勁, 在蘋果的系統上9.0版本以上的Safari表現正常,可是8.0及如下的版本,動畫連動都不動,樣式錯亂,各類猜想,方法各類試,各類搜索,都不盡如人意,實在頭大,最後不得不用排除法找到問題的所在
代碼如斯:
.slide-mask2{ animation-name: myfirst; animation-duration: 0.65s; animation-timing-function: linear; animation-delay: 0s; animation-iteration-count: 1; animation-direction: normal; animation-play-state: running; animation-fill-mode: forwards; } @keyframes myfirst { 0% {left:0px; top:0%;height: 0%;} 40% {left:0px; top:-15%; height: 85%;} 60% {left:0px; top:20%; height: 70%;} 100% {left:0px; top:100%; height: 0%;} } @-moz-keyframes myfirst /* Firefox */ { 0% {left:0px; top:0%;height: 0%;} 40% {left:0px; top:-15%; height: 85%;} 60% {left:0px; top:20%; height: 70%;} 100% {left:0px; top:100%; height: 0%;} } @-webkit-keyframes myfirst /* Safari 和 Chrome */ { 0% {left:0px; top:0%;height: 0%;} 40% {left:0px; top:-15%; height: 85%;} 60% {left:0px; top:20%; height: 70%;} 100% {left:0px; top:100%; height: 0%;} } @-o-keyframes myfirst /* Opera */ { 0% {left:0px; top:0%;height: 0%;} 40% {left:0px; top:-15%; height: 85%;} 60% {left:0px; top:20%; height: 70%;} 100% {left:0px; top:100%; height: 0%;} }
修改如斯:
.slide-mask2{ animation: myfirst 0.65s linear 0s 1 normal forwards; -moz-animation: myfirst 0.65s linear 0s 1 normal forwards; /* Firefox */ -webkit-animation: myfirst 0.65s linear 0s 1 normal forwards; /* Safari 和 Chrome */ -o-animation: myfirst 0.65s linear 0s 1 normal forwards; /* Opera */ } @keyframes myfirst { 0% {left:0px; top:0%;height: 0%;} 40% {left:0px; top:-15%; height: 85%;} 60% {left:0px; top:20%; height: 70%;} 100% {left:0px; top:100%; height: 0%;} } @-moz-keyframes myfirst /* Firefox */ { 0% {left:0px; top:0%;height: 0%;} 40% {left:0px; top:-15%; height: 85%;} 60% {left:0px; top:20%; height: 70%;} 100% {left:0px; top:100%; height: 0%;} } @-webkit-keyframes myfirst /* Safari 和 Chrome */ { 0% {left:0px; top:0%;height: 0%;} 40% {left:0px; top:-15%; height: 85%;} 60% {left:0px; top:20%; height: 70%;} 100% {left:0px; top:100%; height: 0%;} } @-o-keyframes myfirst /* Opera */ { 0% {left:0px; top:0%;height: 0%;} 40% {left:0px; top:-15%; height: 85%;} 60% {left:0px; top:20%; height: 70%;} 100% {left:0px; top:100%; height: 0%;} }
緣由總結: 樣式中的行爲,要包含兼容各類瀏覽器的行爲, keyframes中也要設置兼容瀏覽器的行爲, 最後, 8.0及如下版本的Safari不支持 animation-play-state: running; 具體緣由我不能描述清楚,至此,此兼容性問題解決。
ps: 雖然動畫執行了,可是在windows系統下的Safari瀏覽器中,動畫執行的效果出現卡頓現象, 在ios系統下則很順暢,因此我覺得這是windows系統下Safari瀏覽器的問題,因此並再也不繼續作處理。
參考文章:js 動態添加、修改css3 @keyframes
參考連接: W3School之css動畫教程