JavaScript Judge CSS3 Animation end CSS3 Animation end of callback function _javascript tips

Source: Internet
Author: User

CSS3 of the Times, css3--animation is all possible;

The traditional JS can judge whether the animation is finished by the callback function, even if the animation effect is generated by CSS technology, JavaScript can still capture the animated or transformed end event.

Transitionend Event and Animationend event standard browser events, but in WebKit browsers you still need to use the WebKit prefix, so we have to detect events based on various browsers

Copy Code code as follows:

var transitions = {
' Transition ': ' Transitionend ',
' Otransition ': ' Otransitionend ',
' Moztransition ': ' Transitionend ',
' Webkittransition ': ' Webkittransitionend '
}

The source code is attached below:

Copy Code code as follows:

<!doctype html>
<meta charset= "Utf-8" >
<title>suface JS to determine whether the CSS animation is over </title>
<body>
<p> once the animation or transformation is finished, the callback function is triggered. Large class library support is no longer required. <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 animation too slowly hide (transition-duration:3s;) </div>
<p><button onclick= "this.style.display= ' None"; Startfade (); " > Slowly fade, Detect end Event </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 campaign is over! I am the callback function, not using the third party class library! ');
});
Startfade = function () {
e.classname+= ' hide ';
}
}) ();<br></script>
</body>

This is the method described in this article about JavaScript judgment CSS3 animation end, I hope you can enjoy

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.