jQuery - Effects

jQuery - Effects

jQuery provides a trivially simple interface for creating amazing effects. jQuery methods allow us to quickly apply commonly used effects with minimal configuration. This tutorial covers all the important jQuery methods for creating visual effects.

Showing and hiding elements

The commands for showing and hiding elements are pretty much what we expected - show() will show the elements in the wrapped set and hide() to hide them.

Syntax

Here is the simple syntax for show() method −

[selector]. show(speed, [callback]);

Here is a description of all options −

  • speed - A string representing one of three predefined speeds (slow, normal, or fast) or the number of milliseconds to run the animation (for example, 1000).

  • callback - this optional parameter represents a function to be executed when the animation ends; runs once for each element animated against.

speed - A string representing one of three predefined speeds (slow, normal, or fast) or the number of milliseconds to run the animation (for example, 1000).

callback - this optional parameter represents a function to be executed when the animation ends; runs once for each element animated against.

Following is the simple syntax of the hide() method:

[selector]. hide(speed, [callback]);

Here is a description of all options −

  • speed - A string representing one of three predefined speeds (slow, normal, or fast) or the number of milliseconds to run the animation (for example, 1000).

  • callback - this optional parameter represents a function to be executed when the animation ends; runs once for each element animated against.

speed - A string representing one of three predefined speeds (slow, normal, or fast) or the number of milliseconds to run the animation (for example, 1000).

callback - this optional parameter represents a function to be executed when the animation ends; runs once for each element animated against.

Switching elements

jQuery provides methods for toggling the display state of elements between expanded or hidden. If the element is initially displayed, it will be hidden; if hidden, it will be shown.

Syntax

Here is a simple syntax for one of the toggle() methods −

[selector]. .toggle([speed][, callback]);

Here is a description of all options −

  • speed - A string representing one of three predefined speeds (slow, normal, or fast) or the number of milliseconds to run the animation (for example, 1000).

  • callback - this optional parameter represents a function to be executed when the animation ends; runs once for each element animated against.

speed - A string representing one of three predefined speeds (slow, normal, or fast) or the number of milliseconds to run the animation (for example, 1000).

callback - this optional parameter represents a function to be executed when the animation ends; runs once for each element animated against.

jQuery effect methods

You've seen the basic concept of jQuery effects. The following table lists all the important methods for creating different kinds of effects −

Sr.No. Methods and Description
1 animate(params, [duration, easing, callback])

Function for creating custom animations.

2 fadeIn(speed, [callback])

Decorate all eligible elements by adjusting their opacity and making an optional callback upon completion.

3 fadeOut(speed, [callback])

Disappear all eligible elements by setting their opacity to 0, then setting the display to "none" and firing an additional callback upon completion.

4 fadeTo(speed, opacity, callback)

Decrease the opacity of all matched elements to the specified opacity and fire an optional callback upon completion.

5 hide( )

Hides each set of eligible elements if they are shown.

6 hide(rate, [callback])

Hide all matching elements using a nifty animation and calling an extra callback upon completion.

7 show( )

Displays each set of eligible elements if they are hidden.

8 show(rate, [callback])

Show all matching elements using a graceful animation and firing an optional callback upon completion.

9 slideDown(speed, [callback])

Reveal all eligible elements by adjusting their height and making an additional callback upon completion.

10 slideToggle(speed, [callback])

Toggle the visibility of all relevant elements by adjusting their height and firing an additional callback upon completion.

11 slideUp(speed, [callback])

Hide all matching elements by adjusting their height and calling an extra callback when done.

12 stop([clearQueue, gotoEnd])

Stops all running animations on all specified elements.

13 switch()

Toggle the display of each set of matched items.

14 switch(speed, [callback])

Toggle the display of each of the set of matching elements using a graceful animation and fire an optional callback upon completion.

15 toggle switch )

Toggle the display of each set of matched elements based on the radio button (true shows all elements, false hides all elements).

16 jQuery.fx.off

Disable all animations globally.

Function for creating custom animations.

Decorate all eligible elements by adjusting their opacity and making an optional callback upon completion.

Disappear all eligible elements by setting their opacity to 0, then setting the display to "none" and firing an additional callback upon completion.

Decrease the opacity of all matched elements to the specified opacity and fire an optional callback upon completion.

Hides each set of eligible elements if they are shown.

Hide all matching elements using a nifty animation and calling an extra callback upon completion.

Displays each set of eligible elements if they are hidden.

Show all matching elements using a graceful animation and firing an optional callback upon completion.

Reveal all eligible elements by adjusting their height and making an additional callback upon completion.

Toggle the visibility of all relevant elements by adjusting their height and firing an additional callback upon completion.

Hide all matching elements by adjusting their height and calling an extra callback when done.

Stops all running animations on all specified elements.

Toggle the display of each set of matched items.

Toggle the display of each of the set of matching elements using a graceful animation and fire an optional callback upon completion.

Toggle the display of each set of matched elements based on the radio button (true shows all elements, false hides all elements).

Disable all animations globally.

Effect-Based User Interface

To use these effects, you can either download the latest jQuery UI Library jquery-ui-1.11.4.custom.zip from the jQuery UI Library, or use the Google CDN to use it the same way we did for jQuery.

We have used Google CDN for jQuery UI using the following code snippet in HTML page, so we can use jQuery UI −

Blinds the element or shows it by blinding.

Bounces the element vertically or horizontally n times.

Clamps or disables the element, vertically or horizontally.

Discards the element or shows it by inserting it.

Explodes an element into multiple pieces.

Folds the element like a piece of paper.

Highlights the background with the specified color.

The zoom and fade animation creates a puff effect.

Pulsates the element's opacity multiple times.

Reduce or enlarge an element by a percentage.

Shakes the element vertically or horizontally n times.

Resize the element to the specified width and height.

Pushes the element out of the viewport.

Moves the outline of an element to another.