jQuery - Effects

jQuery provides a trivially simple interface for doing various kind of amazing effects. jQuery methods allow us to quickly apply commonly used effects with a minimum configuration. This tutorial covers all the important jQuery methods to create visual effects.
The commands for showing and hiding elements are pretty much what we would expect − show() to show the elements in a wrapped set and hide() to hide them.
Here is the simple syntax for show() method −
[selector].show( speed, [callback] );

Here is the description of all the parameters −
speed − A string representing one of the three predefined speeds ("slow", "normal", or "fast") or the number of milliseconds to run the animation (e.g. 1000).
speed − A string representing one of the three predefined speeds ("slow", "normal", or "fast") or the number of milliseconds to run the animation (e.g. 1000).
callback − This optional parameter represents a function to be executed whenever the animation completes; executes once for each element animated against.
callback − This optional parameter represents a function to be executed whenever the animation completes; executes once for each element animated against.
Following is the simple syntax for hide() method −
[selector].hide( speed, [callback] );

Here is the description of all the parameters −
speed − A string representing one of the three predefined speeds ("slow", "normal", or "fast") or the number of milliseconds to run the animation (e.g. 1000).
speed − A string representing one of the three predefined speeds ("slow", "normal", or "fast") or the number of milliseconds to run the animation (e.g. 1000).
callback − This optional parameter represents a function to be executed whenever the animation completes; executes once for each element animated against.
callback − This optional parameter represents a function to be executed whenever the animation completes; executes once for each element animated against.
Consider the following HTML file with a small JQuery coding −
<html>
   <head>
      <title>The jQuery Example</title>
      <script type = "text/javascript" 
         src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
      </script>
		
      <script type = "text/javascript" language = "javascript">
         $(document).ready(function() {

            $("#show").click(function () {
               $(".mydiv").show( 1000 );
            });

            $("#hide").click(function () {
               $(".mydiv").hide( 1000 );
            });
				
         });
      </script>
		
      <style>
         .mydiv{ 
            margin:10px;
            padding:12px; 
            border:2px solid #666; 
            width:100px; 
            height:100px;
         }
      </style>
   </head>
	
   <body>
      <div class = "mydiv">
         This is a SQUARE
      </div>

      <input id = "hide" type = "button" value = "Hide" />   
      <input id = "show" type = "button" value = "Show" />
   </body>
</html>
This will produce following result −
jQuery provides methods to toggle the display state of elements between revealed or hidden. If the element is initially displayed, it will be hidden; if hidden, it will be shown.
Here is the simple syntax for one of the toggle() methods −
[selector]..toggle([speed][, callback]);

Here is the description of all the parameters −
speed − A string representing one of the three predefined speeds ("slow", "normal", or "fast") or the number of milliseconds to run the animation (e.g. 1000).
speed − A string representing one of the three predefined speeds ("slow", "normal", or "fast") or the number of milliseconds to run the animation (e.g. 1000).
callback − This optional parameter represents a function to be executed whenever the animation completes; executes once for each element animated against.
callback − This optional parameter represents a function to be executed whenever the animation completes; executes once for each element animated against.
We can animate any element, such as a simple <div> containing an image −
<html>
   <head>
      <title>The jQuery Example</title>
      <script type = "text/javascript" 
         src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
      </script>
		
      <script type = "text/javascript" language = "javascript">
         $(document).ready(function() {
            $(".clickme").click(function(event){
               $(".target").toggle('slow', function(){
                  $(".log").text('Transition Complete');
               });
            });
         });
      </script>
		
      <style>
         .clickme{ 
            margin:10px;
            padding:12px; 
            border:2px solid #666; 
            width:100px; 
            height:50px;
         }
      </style>
   </head>
	
   <body>
      <div class = "content">
         <div class = "clickme">Click Me</div>
         <div class = "target">
            <img src = "./images/jquery.jpg" alt = "jQuery" />
         </div>
         <div class = "log"></div>
      </div>
   </body>
</html>
This will produce following result −
You have seen basic concept of jQuery Effects. Following table lists down all the important methods to create different kind of effects −
A function for making custom animations.
Fade in all matched elements by adjusting their opacity and firing an optional callback after completion.
Fade out all matched elements by adjusting their opacity to 0, then setting display to "none" and firing an optional callback after completion.
Fade the opacity of all matched elements to a specified opacity and firing an optional callback after completion.
Hides each of the set of matched elements if they are shown.
Hide all matched elements using a graceful animation and firing an optional callback after completion.
Displays each of the set of matched elements if they are hidden.
Show all matched elements using a graceful animation and firing an optional callback after completion.
Reveal all matched elements by adjusting their height and firing an optional callback after completion.
Toggle the visibility of all matched elements by adjusting their height and firing an optional callback after completion.
Hide all matched elements by adjusting their height and firing an optional callback after completion.
Stops all the currently running animations on all the specified elements.
Toggle displaying each of the set of matched elements.
Toggle displaying each of the set of matched elements using a graceful animation and firing an optional callback after completion.
Toggle displaying each of the set of matched elements based upon the switch (true shows all elements, false hides all elements).
Globally disable all animations.
To use these effects you can either download latest jQuery UI Library jquery-ui-1.11.4.custom.zip from jQuery UI Library or use Google CDN to use it in the similar way as we have done for jQuery.
We have used Google CDN for jQuery UI using following code snippet in the HTML page so we can use jQuery UI −
<head>
   <script src = "https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.3/jquery-ui.min.js">
   </script>
</head>
Blinds the element away or shows it by blinding it in.
Bounces the element vertically or horizontally n-times.
Clips the element on or off, vertically or horizontally.
Drops the element away or shows it by dropping it in.
Explodes the element into multiple pieces.
Folds the element like a piece of paper.
Highlights the background with a defined color.
Scale and fade out animations create the puff effect.
Pulsates the opacity of the element multiple times.
Shrink or grow an element by a percentage factor.
Shakes the element vertically or horizontally n-times.
Resize an element to a specified width and height.
Slides the element out of the viewport.
Transfers the outline of an element to another.

    27 Lectures 
     1 hours 

 Mahesh Kumar

    27 Lectures 
     1.5 hours 

 Pratik Singh

    72 Lectures 
     4.5 hours 

 Frahaan Hussain

    60 Lectures 
     9 hours 

 Eduonix Learning Solutions

    17 Lectures 
     2 hours 

 Sandip Bhattacharya

    12 Lectures 
     53 mins

 Laurence Svekis
 Print
 Add Notes
 Bookmark this page