Skip to content
For the complete documentation index, see llms.txt. This page is also available as Markdown.

40% Animation

The jQuery library makes it super easy to animate DOM elements — there are several functions built just for that. But let's take it one step at a time. We'll start with the simple hide() and show() — these two methods hide or display elements, respectively:

hides elements

displays elements

These calls only operate on the CSS display attribute, toggling it from the current state to none and back. You can pass the animation speed as the first parameter — either one of the reserved words slow or fast, or specify the speed in milliseconds (1000 ms = 1 sec):

// slowly descending the mountain and… hiding all images
//   slow == 600
//   fast == 200
$("img").hide('slow')
// now let's bring them back, a bit faster
$("img").show(400)

In this case, the disappearance of elements will be accompanied by changes to width, height, opacity, and other attributes. On top of these two methods, there's also toggle(), which works as a switch between hide → show or show → hide:

// run this a couple of times
$("img").toggle()

Moving on — the second parameter in these methods can be a callback function — it will be executed when the animation is complete:

// hide all images
$("img").hide("slow", function() {
    // afterwards, show an alert
    alert("Images were hidden")
});

The animation of attributes like height, width, and opacity is visible to the naked eye, but in reality that's far from all of it. If you peek inside jQuery, you'll see that internal and external padding — padding and margin — also change, so don't forget about that.

Let's keep going — next up is the slide family of methods: slideUp(), slideDown(), and slideToggle(). Their behavior is similar to the previous functions, but the animation will only affect block heights:

hides elements by reducing height to 0

shows all elements by gradually increasing their height from zero

works as slideDown() for hidden elements, otherwise as slideUp()


Before we get to dessert, let me mention the fade family of functions — they only manipulate the opacity attribute:

changes opacity from the current value to 0

changes opacity from 0 to its previous value

toggles between In and Out

changes opacity to the desired value


And now the sweetest part — all animation effects in jQuery revolve around the animate() method. This function takes one or more CSS properties of an element and changes them from the original value to the specified one over N iterations (the number of iterations depends on the specified time, but no less than one iteration per 13 ms — if I dug up that value correctly). Alright then, from words to action — let's try to implement fadeIn() and fadeOut() using animate():

Pretty simple, right? Now let's up the ante — change the size of blocks and their opacity:

As you can see, also not complicated. Now let's try working from the current values instead of setting specific ones:

Last updated