CSS 3動畫介紹

來源:互聯網
上載者:User

標籤:des   style   blog   http   io   ar   color   使用   sp   

原文:A Beginner’s Introduction to CSS Animation

譯文:一個初學者對CSS動畫的介紹

譯者:dwqs

現在,越來越多的網站使用了動畫,並且形式多樣,如GIF、SVG、WebGL、背景視頻等等。當在web中恰當使用動畫時,它可以給網站注入活力和良好的互動性,給使用者提供額外的一層反饋和體驗。

在這篇文章,我將向你介紹CSS動畫,隨著瀏覽器對動畫支援的改善,一種高效率的做事方式變得越來越流行。考慮到基礎知識,我將快速建立一個從正方形變成圓形的動畫案例。

先看看效果:http://jsfiddle.net/Web_Code/tchoafyh/embedded/result/

關於@keyframes和動畫的介紹

CSS動畫主要的組件是@keyframes,這個規則就是用來建立動畫的。將@keyframes當作是時間軸的不同階段,在其內部,你可以自訂時間軸的不同階段,每個階段有不同的CSS聲明。

然後,為了使CSS動畫生效,需要將@keyframes和一個選取器綁定。最後將會逐漸解析@keyframes內的全部代碼,以階段為劃分,慢慢改變把最初的樣式變成新的樣式。

@keyframes元素

首先,定義動畫的分隔。@keyframes的屬性如下:

1、選擇一個名字(在案例我選擇tutsFade)

2、階段劃分:0%–100%,從0%到100%

3、CSS樣式:你想要在每一個階段用到的樣式

例如:

@keyframe tutsFade{    0%{        opacity:1;    }    100%{        opacity:0;    }}

 

          或者:
@keyframe tutsFade{     from{           opacity:1;     }     to{           opacity:0;     }}

 

還有一種簡寫形式:

@keyframe tutsFade{     to{           opacity:0;     }}

 

上述代碼將對元素的透明度應用一個過渡效果:從1到0,三種方式最終的效果相同。

動畫

Animation作為一個選取器去調用@keyframes。Animation有很多的屬性:

1、animation-name:@keyframes的名字(例如tutsFade)

2、animation-duration:動畫持續的時間

3、animation-timing-function:設定動畫的速度特效,可以選擇linear/ease-in/ease/ease-out/ease-in-out/cubic-bezier

4、animation-delay:動畫開始之前的時間延遲

5、animation-iteration-count:動畫迴圈的次數

6、animation-direction:規定動畫是否反向輪播,normal是預設值,正常播放;alternate表示動畫反向輪播

7、animation-fill-mode:規定動畫在播放之前或之後,其動畫效果是否可見(none/forwards/backwards/both)

    例如:

.element {  animation-name: tutsFade;  animation-duration: 4s;  animation-delay: 1s;  animation-iteration-count: infinite;  animation-timing-function: linear;  animation-direction: alternate;}

 

            簡寫:
.element {  animation: tutsFade 4s 1s infinite linear alternate;}
     添加私人首碼            需要添加特定瀏覽器的私人首碼以確保最好的瀏覽器支援:chrome&Safari:-webkit-;Firefox:-moz-;Opera:-o-;IE:-ms-            修改如下:
.element {    -webkit-animation: tutsFade 4s 1s infinite linear alternate;    -moz-animation: tutsFade 4s 1s infinite linear alternate;    -ms-animation: tutsFade 4s 1s infinite linear alternate;    -o-animation: tutsFade 4s 1s infinite linear alternate;    animation: tutsFade 4s 1s infinite linear alternate;}

 

          @keyframes也一樣
@-webkit-keyframes tutsFade { /* your style */ }@-moz-keyframes tutsFade { /* your style */ }@-ms-keyframes tutsFade { /* your style */ }@-o-keyframes tutsFade { /* your style */ }@keyframes tutsFade { /* your style */ }

 

為了得到更多瀏覽器供應商的私人首碼,你可以去http://css3please.com/,尋找,上面提供了非常豐富的資源。

多動畫

可以添加多個動畫,各個動畫之間用逗號分隔。

.element {  animation: tutsFade 4s 1s infinite linear alternate,             tutsRotate 4s 1s infinite linear alternate;}@keyframes tutsFade {  to {    opacity: 0;  }}@keyframes tutsRotate {  to {    transform: rotate(180deg);  }}

 

     方形到圓形的動畫教程             利用上面的規則,我將建立一個簡單的圖形動畫。總共會有5個階段,並且在每個階段都會對元素定義不同的Border-radius,rotation和background-color。            1、基本元素
div {  width: 200px;  height: 200px;  background-color: coral;}

 

2、聲明Keyframes

建立一個名為square-to-circle的keyframe元素,包含5個階段

@keyframes square-to-circle {  0%  {    border-radius:0 0 0 0;    background:coral;    transform:rotate(0deg);  }  25%  {    border-radius:50% 0 0 0;    background:darksalmon;    transform:rotate(45deg);  }  50%  {    border-radius:50% 50% 0 0;    background:indianred;    transform:rotate(90deg);  }  75%  {    border-radius:50% 50% 50% 0;    background:lightcoral;    transform:rotate(135deg);  }  100% {     border-radius:50%;    background:darksalmon;    transform:rotate(180deg);  }}

3、應用動畫

將定義的動畫應用之前的div

div {  width: 200px;  height: 200px;  background-color: coral;  animation: square-to-circle 2s 1s infinite alternate; }

 

4、使用時間函數和添加私人首碼

最後要添加的一個動畫屬性是animation-timing-function,它對動畫元素的速度、加速和減速進行定義。一個類似的工具是:CSS Easing Animation Tool,可以使用它來計算時間函數。

在案例中,我給動畫添加了一個cubic-bezier函數。

div {  width: 200px;  height: 200px;  background-color: coral;  animation: square-to-circle 2s 1s infinite cubic-bezier(1,.015,.295,1.225) alternate; }

 

為了保證最好的瀏覽器支援,還必須添加私人首碼(沒有添加首碼的代碼如下)

div {  width: 200px;  height: 200px;  background-color: coral;  animation: square-to-circle 2s .5s infinite cubic-bezier(1,.015,.295,1.225) alternate;} @keyframes square-to-circle {  0%  {    border-radius:0 0 0 0;    background:coral;    transform:rotate(0deg);  }  25%  {    border-radius:50% 0 0 0;    background:darksalmon;    transform:rotate(45deg);  }  50%  {    border-radius:50% 50% 0 0;    background:indianred;    transform:rotate(90deg);  }  75%  {    border-radius:50% 50% 50% 0;    background:lightcoral;    transform:rotate(135deg);  }  100% {     border-radius:50%;    background:darksalmon;    transform:rotate(180deg);  }}

 

這個在FireFox顯示會有點異常,為了在FireFox有絕佳的顯示效果,可以給div添加如下樣式

outline: 1px solid transparent;

下一篇:【譯】利用HTML 5中的Menu和Menuitem元素快速建立菜單

CSS 3動畫介紹

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.