需求:gift圖片的小動畫每隔2s執行一次。javascript
需求就一句話,咱們看一下實現過程。css
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> a { display: inline-block; background-color: #cc2222; text-decoration: none; color: #fff; font-size: 14px; padding: 10px 12px; width: 100px; position: relative; } .ico { position: absolute; width: 14px; height: 16px; background: url(images/ico.png) no-repeat center; background-size: 100%; position: absolute; top: 4px; right: 27px; } </style> </head> <body> <nav> <a href="javascript:;" class="box"> 一元奪寶 <div class="ico"></div> </a> </nav> </body> </html>
原始動畫效果爲:鼠標hover上去出現動畫。html
動畫樣式以下:java
/*動畫*/ .ico:hover{ -webkit-animation: Tada 1s both; -moz-animation: Tada 1s both; -ms-animation: Tada 1s both; animation: Tada 1s both } /*瀏覽器兼容性部分略過*/ @keyframes Tada { 0% { transform: scale(1); transform: scale(1) } 10%,20% { transform: scale(0.9) rotate(-3deg); transform: scale(0.9) rotate(-3deg) } 30%,50%,70%,90% { transform: scale(1.1) rotate(3deg); transform: scale(1.1) rotate(3deg) } 40%,60%,80% { transform: scale(1.1) rotate(-3deg); transform: scale(1.1) rotate(-3deg) } 100% { transform: scale(1) rotate(0); transform: scale(1) rotate(0) } }
效果:鼠標hover上去gift圖片會動一動。css3
需求變更,要求再也不是hover上去展現動畫,而是每隔2s展現一次動畫。web
思路:不須要hover上去出現動畫,就把hover去掉,每隔2s顯示一次動畫,很容易想到延遲2s,而後動畫一直執行。瀏覽器
此時相關樣式變成:ide
.ico{ -webkit-animation: Tada 1s 2s both infinite; -moz-animation: Tada 1s 2s both infinite; -ms-animation: Tada 1s 2s both infinite; animation: Tada 1s 2s both infinite; }
可是顯示的效果是:頁面加載第一次出現動畫會延遲2s,後面的動畫將再也不有延遲。以下,這是不符合需求的。學習
爲了看出效果,下圖爲延遲6s的效果。動畫
此時換種思路,不要延遲執行動畫,而是動畫的效果自己就是前2s元素不動,後1s是元素動,而後一直循環執行。 這樣在視覺上就會看起來是延遲2s執行1s動畫。
計算一下,原來的百分比節點變成了多少。
將動畫總時長變成3s,用計算出的百分比替換原來的百分比,代碼以下:
.ico{ -webkit-animation: Tada 3s both infinite; -moz-animation: Tada 3s both infinite; -ms-animation: Tada 3s both infinite; animation: Tada 3s both infinite; } @keyframes Tada { 0% { transform: scale(1); transform: scale(1) } 70%,73%{ transform: scale(0.9) rotate(-3deg); transform: scale(0.9) rotate(-3deg) } 77%,83%,90%,97% { transform: scale(1.1) rotate(3deg); transform: scale(1.1) rotate(3deg) } 80%,87%,93%{ transform: scale(1.1) rotate(-3deg); transform: scale(1.1) rotate(-3deg) } 100% { transform: scale(1) rotate(0); transform: scale(1) rotate(0) } }
效果就是咱們指望的了。
完整代碼以下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>demo of starof</title> <style> a { display: inline-block; background-color: #cc2222; text-decoration: none; color: #fff; font-size: 14px; padding: 10px 12px; width: 100px; position: relative; } .ico { position: absolute; width: 14px; height: 16px; background: url(images/ico.png) no-repeat center; background-size: 100%; position: absolute; top: 4px; right: 27px; } /*動畫*/ .ico{ -webkit-animation: Tada 3s both infinite; -moz-animation: Tada 3s both infinite; -ms-animation: Tada 3s both infinite; animation: Tada 3s both infinite; } @-webkit-keyframes Tada { 0% { -webkit-transform: scale(1); transform: scale(1) } 70%,73% { -webkit-transform: scale(0.9) rotate(-3deg); transform: scale(0.9) rotate(-3deg) } 77%,83%,90%,97% { -webkit-transform: scale(1.1) rotate(3deg); transform: scale(1.1) rotate(3deg) } 80%,87%,93% { -webkit-transform: scale(1.1) rotate(-3deg); transform: scale(1.1) rotate(-3deg) } 100% { -webkit-transform: scale(1) rotate(0); transform: scale(1) rotate(0) } } @-moz-keyframes Tada { 0% { -moz-transform: scale(1); transform: scale(1) } 70%,73% { -moz-transform: scale(0.9) rotate(-3deg); transform: scale(0.9) rotate(-3deg) } 77%,83%,90%,97% { -moz-transform: scale(1.1) rotate(3deg); transform: scale(1.1) rotate(3deg) } 80%,87%,93%{ -moz-transform: scale(1.1) rotate(-3deg); transform: scale(1.1) rotate(-3deg) } 100% { -moz-transform: scale(1) rotate(0); transform: scale(1) rotate(0) } } @-ms-keyframes Tada { 0% { -ms-transform: scale(1); transform: scale(1) } 70%,73% { -ms-transform: scale(0.9) rotate(-3deg); transform: scale(0.9) rotate(-3deg) } 77%,83%,90%,97% { -ms-transform: scale(1.1) rotate(3deg); transform: scale(1.1) rotate(3deg) } 80%,87%,93% { -ms-transform: scale(1.1) rotate(-3deg); transform: scale(1.1) rotate(-3deg) } 100% { -ms-transform: scale(1) rotate(0); transform: scale(1) rotate(0) } } @keyframes Tada { 0% { transform: scale(1); transform: scale(1) } 70%,73%{ transform: scale(0.9) rotate(-3deg); transform: scale(0.9) rotate(-3deg) } 77%,83%,90%,97% { transform: scale(1.1) rotate(3deg); transform: scale(1.1) rotate(3deg) } 80%,87%,93%{ transform: scale(1.1) rotate(-3deg); transform: scale(1.1) rotate(-3deg) } 100% { transform: scale(1) rotate(0); transform: scale(1) rotate(0) } } </style> </head> <body> <nav> <a href="javascript:;" class="box"> 一元奪寶 <div class="ico"></div> </a> </nav> </body> </html>
本文只是介紹一種思路,關於動畫各個參數詳情可參考:
本文做者starof,因知識自己在變化,做者也在不斷學習成長,文章內容也不定時更新,爲避免誤導讀者,方便追根溯源,請諸位轉載註明出處:http://www.cnblogs.com/starof/p/5443445.html有問題歡迎與我討論,共同進步。