您好,登錄后才能下訂單哦!
小編給大家分享一下CSS3如何實現彈跳的小球動畫,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
這個案例關鍵點在于小球彈跳的節奏感和布局定位。
一、案例知識點
1、相對和絕對定位
2、多個animation動畫列隊
二、主體代碼
1、HTML代碼
<div id="wrap"> <div class="tu1"><img src="images/1.png" /></div> <div class="tu2"><img src="images/2.png" /></div> <div class="tu3"><img src="images/3.png" /></div> </div>
2、CSS部分代碼
#wrap{ position:absolute; left:0; right:0; top:0; bottom:0; width:580px; height:143px; margin:auto; } #wrap img{ width:160px; } #wrap div{ float:left; margin-right:50px;} #wrap div:last-child{ margin-right:0;} .tu1,.tu2,.tu3{ position:absolute; left:50%; margin-left:-80px; } .tu1{ z-index:1; animation:tiantiao1 0.5s ease-in 1 forwards,tiantiao2 0.2s ease-out 0.5s 1 forwards,tiantiao3 0.2s ease-in 0.7s 1 forwards,tiantiao4 0.15s ease-out 0.9s 1 forwards,tiantiao5 0.15s ease-in 1.05s 1 forwards,leftMove 0.4s ease-out 1.2s 1 forwards,rotate 1s linear 1.6s infinite; } .tu2{ z-index:2; animation:tiantiao1 0.5s ease-in 1 forwards,tiantiao2 0.2s ease-out 0.5s 1 forwards,tiantiao3 0.2s ease-in 0.7s 1 forwards,tiantiao4 0.15s ease-out 0.9s 1 forwards,tiantiao5 0.15s ease-in 1.05s 1 forwards,middle 0.4s ease-out 1.2s 1 forwards; } .tu3{ z-index:3; animation:tiantiao1 0.5s ease-in 1 forwards,tiantiao2 0.2s ease-out 0.5s 1 forwards,tiantiao3 0.2s ease-in 0.7s 1 forwards,tiantiao4 0.15s ease-out 0.9s 1 forwards,tiantiao5 0.15s ease-in 1.05s 1 forwards,rightMove 0.4s ease-out 1.2s 1 forwards; } @keyframes tiantiao1{ 0%{ transform:translateY(-500px); } 100%{ transform:translateY(0);} } @keyframes tiantiao2{ 0%{ transform:translateY(0);} 100%{ transform:translateY(-100px);}} @keyframes tiantiao3{ 0%{ transform:translateY(-100px);} 100%{ transform:translateY(0);}} @keyframes tiantiao4{ 0%{ transform:translateY(0px);} 100%{ transform:translateY(-50px);}} @keyframes tiantiao5{ 0%{ transform:translateY(-50px);} 100%{ transform:translateY(0);} } @keyframes leftMove{ 0%{ transform:translateX(0);} 100%{ transform:translateX(-300px) scale(1.6); }} @keyframes rightMove{ 0%{ transform:translateX(0);} 100%{ transform:translateX(300px) scale(1.6); }} @keyframes middle{ 0%{ transform:translateX(0); } 100%{ transform:translateX(0) scale(1.6); }}
多個隊列的動畫要注意動畫的延遲。上一個隊列的動畫執行完畢后才執行下一個隊列的動畫。
完整頁面代碼
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>小球掉落依次排列動畫</title> <style type="text/css"> body,div,footer,p{ margin:0; padding:0;} body{ font:1em "microsoft Yahei"; background-color:#eee;} #wrap{ position:absolute; left:0; right:0; top:0; bottom:0; width:580px; height:143px; margin:auto; } #wrap img{ width:160px; } #wrap div{ float:left; margin-right:50px;} #wrap div:last-child{ margin-right:0;} .tu1,.tu2,.tu3{ position:absolute; left:50%; margin-left:-80px; } .tu1{ z-index:1; animation:tiantiao1 0.5s ease-in 1 forwards,tiantiao2 0.2s ease-out 0.5s 1 forwards,tiantiao3 0.2s ease-in 0.7s 1 forwards,tiantiao4 0.15s ease-out 0.9s 1 forwards,tiantiao5 0.15s ease-in 1.05s 1 forwards,leftMove 0.4s ease-out 1.2s 1 forwards,rotate 1s linear 1.6s infinite; } .tu2{ z-index:2; animation:tiantiao1 0.5s ease-in 1 forwards,tiantiao2 0.2s ease-out 0.5s 1 forwards,tiantiao3 0.2s ease-in 0.7s 1 forwards,tiantiao4 0.15s ease-out 0.9s 1 forwards,tiantiao5 0.15s ease-in 1.05s 1 forwards,middle 0.4s ease-out 1.2s 1 forwards; } .tu3{ z-index:3; animation:tiantiao1 0.5s ease-in 1 forwards,tiantiao2 0.2s ease-out 0.5s 1 forwards,tiantiao3 0.2s ease-in 0.7s 1 forwards,tiantiao4 0.15s ease-out 0.9s 1 forwards,tiantiao5 0.15s ease-in 1.05s 1 forwards,rightMove 0.4s ease-out 1.2s 1 forwards;} footer{ position:absolute; bottom:20px; left:50%; margin-left:-104px; } footer p{ text-align:center; margin-bottom:.7em;} footer a{ color:#666; text-decoration:none;} footer a:hover{ color:#333;} @keyframes tiantiao1{ 0%{ transform:translateY(-500px); } 100%{ transform:translateY(0);} } @keyframes tiantiao2{ 0%{ transform:translateY(0);} 100%{ transform:translateY(-100px);}} @keyframes tiantiao3{ 0%{ transform:translateY(-100px);} 100%{ transform:translateY(0);}} @keyframes tiantiao4{ 0%{ transform:translateY(0px);} 100%{ transform:translateY(-50px);}} @keyframes tiantiao5{ 0%{ transform:translateY(-50px);} 100%{ transform:translateY(0);} } @keyframes leftMove{ 0%{ transform:translateX(0);} 100%{ transform:translateX(-300px) scale(1.6); }} @keyframes rightMove{ 0%{ transform:translateX(0);} 100%{ transform:translateX(300px) scale(1.6); }} @keyframes middle{ 0%{ transform:translateX(0); } 100%{ transform:translateX(0) scale(1.6); }} </style> </head> <body> <div id="wrap"> <div class="tu1"><img src="images/1.png" /></div> <div class="tu2"><img src="images/2.png" /></div> <div class="tu3"><img src="images/3.png" /></div> </div> <footer> <p>億速云</p> <p><a href="http://www.neiyidaogou.com" target="_blank">www.neiyidaogou.com</a></p> </footer> </body> </html>
以上是“CSS3如何實現彈跳的小球動畫”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。