css3動畫屬性之Transitions屬性與Animations屬性的功能實現

來源:互聯網
上載者:User
本篇文章給大家帶來的內容是關於css3動畫屬性之Transitions屬性與Animations屬性的功能實現 ,有一定的參考價值,有需要的朋友可以參考一下,希望對你有所協助。

1 Transitions功能

(1)瀏覽器支援:

到目前為止:Safari3.1以上、Chrome8以上、Firefox4以上、Opera10以上、IE11以上瀏覽器支援該功能。

(2)功能

在CSS3中,Transitions功能通過將元素的某個屬性從一個屬性值在指定的時間內平滑過渡到另一個屬性值來實現動畫功能。

(3)使用方法

transition:property duration timing-function

property:表示對哪個屬性進行平滑過渡。

duration:表示在多久時間內完成屬性值得平滑過渡。

timing-function:表示通過什麼方法進行平滑過渡。

div{background-color:#ffff00;transition:background-color 1s linear;//在1秒內讓div元素的背景色從黃色平滑過渡到淺藍色。}div{background-color:#00ffff;}

(4)另一種使用方法

transition-property:background-color;transition-duration:1;transition-timing-function:linear;

(5)transition-delay屬性

指定變換動畫特效延遲多久後開始執行。可以用秒單位或毫秒單位指定屬性值。

transition-delay:1s;//或 transition:background-color 1s linear 2s;(在第四個參數中書寫延遲時間)

(6)使用Transitions功能同時平滑過渡多個屬性值

transition:background-color 1s linear,color 1s linear,width 1s linear;

(7)移動、旋轉等動畫效果

img{position:absolute;top:70px;left:0;transform:rotate(0deg);transition:left 1s linear,transform 1s linear;} img:hover{left:30px;transform:rotate(720deg);}

(8)缺點

只能指定屬性的開始值與終點值,然後再這兩個屬性值之間實現平滑過渡,不能實現更為複雜的動畫效果。

2 Animations功能

(1)瀏覽器支援:

到目前為止:Safari4以上、Chrome2以上、Firefox20以上、Opera18以上、IE11以上瀏覽器支援該功能。

(2)功能

與Transitions功能相同,都是通過改變元素的屬性值來實現動畫效果。

區別:Animations功能通過定義多個主要畫面格以及定義每個主要畫面格中元素的屬性值來實現更為複雜的動畫效果。

(3)建立主要畫面格的集合

@keyframes 主要畫面格集合名{ 建立主要畫面格的代碼 }

(4)建立主要畫面格的代碼(類似如下)

40%{ 本主要畫面格中的樣式代碼 }

(40%表示改幀位於整個動畫過程中的40%處,開始幀為0%,結束幀為100%)

@keyframes mycolor{0%{background-color:red;}40%{background-color:darkblue;}70%{background-color:yellow;}100%{background-color:red;}}

(5)在元素的樣式中使用該主要畫面格的集合

div{animation-name:my-color; //指定主要畫面格集合的名稱animation-duration:5s; //指定完成整個動畫所花費的時間animation-timing-function:linear; //指定實現動畫的方法}

(6)其他屬性

animation-delay:用於指定延遲多少秒或毫秒後開始執行動畫。

animation-iteration-count:用於指定動畫的執行次數,可指定為infinite(無限次)。

animation-direction:用於指定動畫的執行方向。可指定屬性值包括:

  • normal:初始值(動畫執行完畢後返回初始狀態)

  • alternate:交替更換動畫的執行方向

  • reverse:反方向執行動畫

  • alternate-reverse:從反方向開始交替更改動畫的執行方向

(7)在一行樣式代碼中定義animation動畫時採用如下所示的書寫方式

animation:keyframe的名稱 動畫的執行時間長度 動畫的實現方法 延遲多少秒後開始執行動畫 動畫的執行次數 動畫的執行方向;

(8)實現多個屬性值同時改變的動畫

只需只在各主要畫面格中同時指定這些屬性值就可以了。

3 實現動畫的方法

方法 屬性值的變化速度
linear 在動畫開始時與結束時以同樣速度進行改變
ease-in 動畫開始時速度很慢,然後速度沿曲線值進行加快
ease-out 動畫開始時速度很快,然後速度沿曲線值進行放慢
ease 動畫開始時速度很慢,然後速度沿曲線值進行加快,然後再沿曲線值進行放慢
ease-in-out 動畫開始時速度很慢,然後速度沿曲線值進行加快,然後再沿曲線值進行放慢

4 實現網頁的淡入效果

通過在開始幀與結束幀中改變頁面的opacity屬性的屬性值來實現頁面的淡入效果。

@keyframes fadein{0%{opacity:0;background-color:white;}100%{opacity:1;background-color:white;}body{animation-name: fadein; animation-duration:5s; animation-timing-function:linear; animation-iteration-count:1;}
相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.