javascript判斷css3動畫結束 css3動畫結束的回撥函式
阿新 • • 發佈:2019-02-20
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>suface js判斷css動畫是否結束</title>
</head>
<body>
<p>一旦動畫或變換結束,回撥函式就會觸發。不再需要大型類庫支援。<br> </p>
<style type="text/css">
.sample {
width: 200px;
height: 200px;
border: 1px solid green;
background: lightgreen;
opacity: 1;
margin-bottom: 20px;
transition-property: opacity;
/*transition-duration: .5s;*/
transition-duration:3s;
}
.sample.hide {
opacity: 0;
}
</style>
<div class="sample">css3動畫過度慢慢隱藏(transition-duration:3s;)</div>
<p><button onclick="this.style.display='none';startFade();">慢慢消退,檢測結束事件</button></p>
<script>
(function() {
var e = document.getElementsByClassName('sample')[0];
function whichTransitionEvent(){
var t;
var el = document.createElement('fakeelement');
var transitions = {
'transition':'transitionend',
'OTransition':'oTransitionEnd',
'MozTransition':'transitionend',
'WebkitTransition':'webkitTransitionEnd'
}
for(t in transitions){
if( el.style[t] !== undefined ){
return transitions[t];
}
}
}
var transitionEvent = whichTransitionEvent();
transitionEvent && e.addEventListener(transitionEvent, function() {
alert('css3運動結束!我是回撥函式,沒有使用第三方類庫!');
});
startFade = function() {
e.className+= ' hide';
}
})();<br></script>
</body>
</html>