Native CSS animation callback event

Source: Internet
Author: User

 
In 2014, there was a hard-to-Admit fact for my web development career, that is, the traditional JavaScript tool library was almost dead. We have been relying on tool libraries for development for many years, but now JavaScript and CSS have provided the vast majority of features we need. if we calm down and study these native functions, we can reduce the use of the JavaScript tool library. I often hear that the reason for sticking to the tool library is that CSS animation does not provide callback.

Wrong. False. Incorrect. Duplicate Que? JavaScript does provide the function of triggering callback after JS animation and transition.

Click here to view the example!


JavaScript code
The only reason that this JavaScript segment is involved is the browser prefix. The transitionend event and the animationend event are used by the standard browser, but the webkit-based browser still relies on the prefix. Therefore, we must first determine the prefix of the event before calling:
/* From Modernizr */function whichTransitionEvent () {var t; var el = document. createElement ('fakeelement'); var transitions = {'transition ': 'transitionend', 'otransition': 'otransitionend', 'offline transition ': 'transitionend', 'webkittransition ': 'webkittransitionend', 'mstransition ': 'mstransitionend'} for (t in transitions) {if (el. style [t]! = Undefined) {return transitions [t] ;}}/ * listens for transition! */Var transitionEvent = whichTransitionEvent (); transitionEvent & e. addEventListener (transitionEvent, function () {console. log ('transition completed! Native JavaScript callback execution! ');});/* In whichTransitionEvent, you can replace the transition text with the animation, and the animation is processed. The code is omitted here ...)*/
After the animation/transition effect is completed, the callback function will be triggered, because it can be implemented without the support of those heavyweight class libraries, which should be very useful in many cases.
Imagine how much we can save without using a class library. for example, duration, fill-mode, and delay can all be set through CSS. As a result, JavaScript will become more lightweight and feel a better life!

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.