Animations can be created with either pure CSS or with JavaScript, but each has its pros and cons:
Use CSS transitions and keyframe animations for small, self-contained states of UI elements
Examples: a button hover animation, showing a tooltip, sliding in a navigation menu, ...
Use JavaScript animations for complex animations and rich interactivity
Examples: event based animations (stop-pause-reverse animations on click events), complex sequences of timeline animations, ...
Vanilla JavaScript has a built-in Web Animations API(opens new window)
that is well-supported in all modern browsers
(Note: we haven't discussed this API in this course)
In this section we only discuss the basic built-in animations (so-called effects) with jQuery
The first (optional) parameter of all these effects corresponds to the duration of the effect in ms or by using the words fast (= 200ms) or slow (= 600ms)
The default duration for every effect (except for show/hide/toggle) is 400ms
The last (optional) parameter of all these effects is a callback function that runs once the effect is completed
Open jQuery/effects/effects.html and look at the behavior of some basic effects
const idMin =151;const idMax =180;let currentImg =151;functionloop(){let nextImg = currentImg;// the next image is a random image between idMin and idMax but may not be the same as the current imagedo{
nextImg = Math.floor(Math.random()*(idMax - idMin +1)+ idMin);}while(nextImg === currentImg);
currentImg = nextImg;// figure slideUp() -> replace the image -> wait 500ms -> slideDown()$('figure').slideUp(1000,function(){$(this).empty().append(`<img src="https://picsum.photos/id/${currentImg}/600/400" alt="image ${currentImg}">`).delay(500).slideDown(1000);});}loop();setInterval(loop,4000);