css3動(dòng)畫(huà)事件—webkitAnimationEnd與計(jì)時(shí)器time事件
發(fā)布時(shí)間:2013-01-31 17:12:49 作者:佚名
我要評(píng)論

用css3的animation完成一個(gè)動(dòng)畫(huà),當(dāng)只有這個(gè)動(dòng)畫(huà)完成時(shí)才執(zhí)行令一個(gè)事件,比如讓動(dòng)畫(huà)保持在終止的狀態(tài)或其他一些事件,關(guān)于這個(gè)問(wèn)題,本文給出詳細(xì)的解決方案,感興趣的朋友可以了解下或許對(duì)你有所幫助
用css3的animation完成一個(gè)動(dòng)畫(huà),當(dāng)只有這個(gè)動(dòng)畫(huà)完成時(shí)才執(zhí)行令一個(gè)事件,比如讓動(dòng)畫(huà)保持在終止的狀態(tài)或其他一些事件。我們?cè)撛趺崔k呢。
第一種方法:
用計(jì)時(shí)器,設(shè)定一個(gè)和動(dòng)畫(huà)時(shí)長(zhǎng)一樣的time,過(guò)time事件去執(zhí)行這個(gè)函數(shù)。
setTimeout(function(){ },time);
第二種方法:
當(dāng)-webkit-animation動(dòng)畫(huà)結(jié)束時(shí)有一個(gè)webkitAnimationEnd事件,只要監(jiān)聽(tīng)這個(gè)事件就可以了。
例子:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="@my_programmer">
<title>webkitAnimationEnd</title>
<meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/>
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="format-detection" content="telephone=no"/>
<style type="text/css">
#div{
width:200px;
height:200px;
background:#f60;
margin:100px auto;
-webkit-transition: all ease 1s;
}
.change{
-webkit-animation: transform 1s 2 ease;
}
@-webkit-keyframes transform {
% { -webkit-transform: scale(1)}
% { -webkit-transform: scale(2)}
% { -webkit-transform: scale(0.5)}
% { -webkit-transform: scale(1)}
}
</style>
</head>
<body>
<div id="div"></div>
<script type="text/javascript">
var tt = document.querySelector('#div');
tt.addEventListener("click", function(){
this.className = 'change';
}, false);
tt.addEventListener("webkitAnimationEnd", function(){ //動(dòng)畫(huà)結(jié)束時(shí)事件
this.className = this.className.replace('change', ' ');
console.log(2);
}, false);
</script>
</body>
</html>
拓展:
1、-webkit-animation動(dòng)畫(huà)其實(shí)有三個(gè)事件:
開(kāi)始事件 webkitAnimationStart
結(jié)束事件 webkitAnimationEnd
重復(fù)運(yùn)動(dòng)事件 webkitAnimationIteration
你可以在上個(gè)例子中測(cè)試一下這兩個(gè)事件
tt.addEventListener("webkitAnimationStart", function(){ //動(dòng)畫(huà)開(kāi)始時(shí)事件
console.log(1);//動(dòng)畫(huà)開(kāi)始時(shí),控制臺(tái)輸出1
}, false);
tt.addEventListener("webkitAnimationIteration", function(){ //動(dòng)畫(huà)重復(fù)運(yùn)動(dòng)時(shí)的事件
console.log(3);//第一遍動(dòng)作完成時(shí),控制臺(tái)輸出3
}, false);
2、css3的過(guò)渡屬性transition,在動(dòng)畫(huà)結(jié)束時(shí),也存在結(jié)束的事件:webkitTransitionEnd;
注意:transition,也僅僅有這一個(gè)事件。
第一種方法:
用計(jì)時(shí)器,設(shè)定一個(gè)和動(dòng)畫(huà)時(shí)長(zhǎng)一樣的time,過(guò)time事件去執(zhí)行這個(gè)函數(shù)。
setTimeout(function(){ },time);
第二種方法:
當(dāng)-webkit-animation動(dòng)畫(huà)結(jié)束時(shí)有一個(gè)webkitAnimationEnd事件,只要監(jiān)聽(tīng)這個(gè)事件就可以了。
例子:
復(fù)制代碼
代碼如下:<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="@my_programmer">
<title>webkitAnimationEnd</title>
<meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/>
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="format-detection" content="telephone=no"/>
<style type="text/css">
#div{
width:200px;
height:200px;
background:#f60;
margin:100px auto;
-webkit-transition: all ease 1s;
}
.change{
-webkit-animation: transform 1s 2 ease;
}
@-webkit-keyframes transform {
% { -webkit-transform: scale(1)}
% { -webkit-transform: scale(2)}
% { -webkit-transform: scale(0.5)}
% { -webkit-transform: scale(1)}
}
</style>
</head>
<body>
<div id="div"></div>
<script type="text/javascript">
var tt = document.querySelector('#div');
tt.addEventListener("click", function(){
this.className = 'change';
}, false);
tt.addEventListener("webkitAnimationEnd", function(){ //動(dòng)畫(huà)結(jié)束時(shí)事件
this.className = this.className.replace('change', ' ');
console.log(2);
}, false);
</script>
</body>
</html>
拓展:
1、-webkit-animation動(dòng)畫(huà)其實(shí)有三個(gè)事件:
開(kāi)始事件 webkitAnimationStart
結(jié)束事件 webkitAnimationEnd
重復(fù)運(yùn)動(dòng)事件 webkitAnimationIteration
你可以在上個(gè)例子中測(cè)試一下這兩個(gè)事件
復(fù)制代碼
代碼如下:tt.addEventListener("webkitAnimationStart", function(){ //動(dòng)畫(huà)開(kāi)始時(shí)事件
console.log(1);//動(dòng)畫(huà)開(kāi)始時(shí),控制臺(tái)輸出1
}, false);
tt.addEventListener("webkitAnimationIteration", function(){ //動(dòng)畫(huà)重復(fù)運(yùn)動(dòng)時(shí)的事件
console.log(3);//第一遍動(dòng)作完成時(shí),控制臺(tái)輸出3
}, false);
2、css3的過(guò)渡屬性transition,在動(dòng)畫(huà)結(jié)束時(shí),也存在結(jié)束的事件:webkitTransitionEnd;
注意:transition,也僅僅有這一個(gè)事件。
相關(guān)文章
CSS3+Animation實(shí)現(xiàn)鼠標(biāo)滑過(guò)按鈕背景動(dòng)畫(huà)特效源碼
CSS3+Animation實(shí)現(xiàn)鼠標(biāo)滑過(guò)按鈕背景動(dòng)畫(huà)特效源碼是一款當(dāng)鼠標(biāo)滑過(guò)按鈕時(shí),使用CSS3 animation來(lái)動(dòng)畫(huà)background-size和background-position屬性,來(lái)制作各種背景動(dòng)畫(huà)效果。2016-04-19CSS3 Animation 制作按鈕鼠標(biāo)滑過(guò)動(dòng)畫(huà)填充背景特效源碼(13種)
CSS3 Animation 制作按鈕鼠標(biāo)滑過(guò)動(dòng)畫(huà)填充背景特效源碼(13種)是一款共有13種動(dòng)畫(huà)填充背景效果,均由按鈕的偽元素和CSS3 animation來(lái)制作完成,效果非常棒,喜歡的朋友前來(lái)2016-04-12CSS3中利用animation屬性創(chuàng)建雪花飄落特效
在CSS3中我們可以使用animation屬性來(lái)創(chuàng)建復(fù)雜的動(dòng)畫(huà)效果,本文就要借助它實(shí)現(xiàn)雪花飄落特效,功能代碼如下,希望對(duì)大家學(xué)習(xí)css3有所幫助2014-05-14CSS3動(dòng)畫(huà)animation實(shí)現(xiàn)云彩向左滾動(dòng)
這篇文章主要介紹了CSS3動(dòng)畫(huà)animation如何實(shí)現(xiàn)云彩向左滾動(dòng)的效果,需要的朋友可以參考下2014-05-09css3 animation實(shí)現(xiàn)的loading動(dòng)畫(huà)加載進(jìn)度條效果
一款純css3 animation動(dòng)畫(huà)屬性頁(yè)面loading動(dòng)畫(huà)加載進(jìn)度條效果2014-04-18純CSS3 animation屬性實(shí)現(xiàn)的GIF圖片進(jìn)度加載效果
一款純CSS3 animation屬性實(shí)現(xiàn)GIF圖片進(jìn)度加載效果2014-01-22基于CSS3特效之動(dòng)畫(huà):animation的應(yīng)用
本篇文章對(duì)CSS3中的animation的使用進(jìn)行了詳細(xì)的介紹,需要的朋友可以參考下2013-05-09- 關(guān)鍵貞的動(dòng)畫(huà)效果如果一樣,可以將關(guān)鍵貞的百分比用逗號(hào)隔開(kāi),然后再寫(xiě)效果2012-05-10
CSS3 Animation 制作動(dòng)畫(huà)點(diǎn)擊波效果代碼
CSS3 Animation 制作動(dòng)畫(huà)點(diǎn)擊波效果代碼是一款使用CSS3 animation動(dòng)畫(huà)來(lái)制作點(diǎn)擊波效果,可以在按鈕和圖片等元素上制作點(diǎn)擊波特效。需要的朋友前來(lái)下載源碼2016-05-04