banner



How To Make An Animation Stop Javascript

CSS versus JavaScript animations

You can animate with CSS or JavaScript. Which should you use, and why?

— Updated

Sam Thorogood

In that location are two primary means to create animations on the web: with CSS and with JavaScript. Which one you cull actually depends on the other dependencies of your project, and what kinds of effects you're trying to reach.

TL;DR #

  • Use CSS animations for simpler "i-shot" transitions, like toggling UI element states.
  • Use JavaScript animations when yous want to take advanced effects like billowy, stop, interruption, rewind, or slow down.
  • If you choose to breathing with JavaScript, use the Web Animations API or a modern framework that you lot're comfortable with.

Nigh basic animations can be created with either CSS or JavaScript, just the amount of effort and fourth dimension differs (see also CSS vs JavaScript Performance). Each has its pros and cons, only these are proficient guidelines:

  • Utilize CSS when y'all have smaller, self-contained states for UI elements. CSS transitions and animations are platonic for bringing a navigation card in from the side, or showing a tooltip. You may end up using JavaScript to control u.s., but the animations themselves volition be in your CSS.
  • Use JavaScript when yous need significant control over your animations. The Web Animations API is the standards-based arroyo, available today in most modernistic browsers. This provides existent objects, ideal for complex object-oriented applications. JavaScript is also useful when you need to end, pause, slow downward, or reverse your animations.
  • Apply requestAnimationFrame direct when yous want to orchestrate an entire scene by hand. This is an advanced JavaScript approach, just can be useful if y'all're building a game or drawing to an HTML canvass.

Alternatively, if yous're already using a JavaScript framework that includes animation functionality, such every bit via jQuery's .animate() method or GreenSock'southward TweenMax, then you may notice information technology more convenient overall to stick with that for your animations.

Breathing with CSS #

Animating with CSS is the simplest mode to get something moving on screen. This arroyo is described as declarative, because yous specify what you'd like to happen.

Below is some CSS that moves an element 100px in both the X and Y axes. Information technology'due south done by using a CSS transition that's set up to take 500ms. When the move course is added, the transform value is changed and the transition begins.

                          .box              {              
transform : translate (0, 0) ;
transition : transform 500ms;
}

.box.move {
transform : translate (100px, 100px) ;
}

Try it

Besides the transition's elapsing, there are options for the easing, which is substantially how the animation feels. For more information about easing, see The Nuts of Easing guide.

If, as in the in a higher place snippet, you create carve up CSS classes to manage your animations, yous tin can and so utilize JavaScript to toggle each blitheness on and off:

            box.classList.              add together              (              'move'              )              ;                      

Doing this provides a prissy remainder to your apps. You can focus on managing state with JavaScript, and but set the appropriate classes on the target elements, leaving the browser to handle the animations. If yous become downward this route, you lot can listen to transitionend events on the element, only but if y'all're able to forego support for older versions of Internet Explorer; version 10 was the first version to support these events. All other browsers have supported the event for some time.

The JavaScript required to heed for the stop of a transition looks similar this:

                          var              box              =              document.              querySelector              (              '.box'              )              ;              
box. addEventListener ( 'transitionend' , onTransitionEnd, faux ) ;

role onTransitionEnd ( ) {
// Handle the transition finishing.
}

In addition to using CSS transitions, you can besides use CSS animations, which allow you to accept much more than control over private animation keyframes, durations, and iterations.

You can, for example, breathing the box in the same way with transitions, but have information technology breathing without whatever user interactions similar clicking, and with infinite repetitions. You can as well change multiple properties at the aforementioned time.

                          .box              {              
animation-name : movingBox;

blitheness-duration : 1300ms;

animation-iteration-count : space;

animation-direction : alternate;
}

@keyframes movingBox {
0% {
transform : interpret (0, 0) ;
opacity : 0.iii;
}

25% {
opacity : 0.9;
}

fifty% {
transform : translate (100px, 100px) ;
opacity : 0.ii;
}

100% {
transform : translate (30px, 30px) ;
opacity : 0.eight;
}
}

Effort it

With CSS animations yous define the animation itself independently of the target element, and use the animation-name property to choose the required blitheness.

If you want your CSS animations to work on older browsers, you will need to add vendor prefixes. Many tools tin help you create the prefixed versions of the CSS yous need, assuasive y'all to write the unprefixed version in your source files.

Animate with JavaScript and the Web Animations API #

Creating animations with JavaScript is, past comparison, more complex than writing CSS transitions or animations, but information technology typically provides developers significantly more than power. You tin employ the Web Animations API to either breathing specific CSS properties or build composable consequence objects.

JavaScript animations are imperative, as you write them inline every bit part of your code. You tin also encapsulate them inside other objects. Below is the JavaScript that you would need to write to copy the CSS transition described earlier:

                          var              target              =              certificate.              querySelector              (              '.box'              )              ;              
var player = target. animate ( [
{ transform : 'translate(0)' } ,
{ transform : 'translate(100px, 100px)' }
] , 500 ) ;
histrion. addEventListener ( 'finish' , function ( ) {
target.way.transform = 'translate(100px, 100px)' ;
} ) ;

Past default, Web Animations but modify the presentation of an element. If you'd like to take your object remain at the location it has moved to, and so you lot should change its underlying styles when the blitheness has finished, as per our sample.

Try it

The Spider web Animations API is a relatively new standard from the W3C. It is supported natively in nearly modern browsers. For non-supporting modern browsers, a polyfill is available.

With JavaScript animations, y'all're in total command of an element's styles at every step. This ways you tin wearisome down animations, pause them, end them, contrary them, and manipulate elements as you meet fit. This is particularly useful if you're building complex, object-oriented applications, because you can properly encapsulate your behavior.

Terminal updated: — Improve article

Return to all articles

Source: https://web.dev/css-vs-javascript/

Posted by: edgertongrous1984.blogspot.com

0 Response to "How To Make An Animation Stop Javascript"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel