Did you find this useful?
Socialize it today.


jQuery Basic Effects

Friday, July 06, 2012

jQuery supports a variety of methods that can be used to create animation effects. Creating effects using jQuery is rapidly growing and there are many web designers out there who have created some amazing combinations and effects you have never thought possible using jQuery. Of course, if you are new to jQuery, you'll need to learn some basics first. In this tutorial, we are going to cover some basic effects using methods such as hide(), show(), and toggle().


Syntax

$(selector).hide(speed,callback)
$(selector).show(speed,callback)
$(selector).toggle(speed,callback)


ParameterDescription
speedOptional values in milliseconds, "slow", "normal", and "fast".
callbackAn optional function to run after the method is completed


HTML Example

We will use the following HTML for the examples listed below.


<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  // ... jQuery Code ...
});
</script>
</head>
<body>
<img id="img1" src="show.png" />  
<img id="img2" src="hide.png" /> 
<img id="img3" src="collapse.png" />  
<div id="div1"> <!-- Some Content --> </div>
</body>
</html>


Example

In the following example, we will use the click event to show, hide, and toggle the div element adjacent to the images. You will also notice that we modify the source attribute of img3 when the div element is either in a show or hidden state. Click on the various icons below to show, hide, and toggle the div element.


<script type="text/javascript">
 $("#img1").click(function(){
   $("div").show(); 
   $("#img3").attr("src", "collapse.png");
   });
 $("#img2").click(function(){
   $("div").hide(); 
   $("#img3").attr("src", "show.png");
   });
 $("#img3").click(function () {
    $("#div1").toggle("slow");
    var $img = $("#img3")
    if ($img.attr("src") == "expand.png") {
    $img.attr("src", "collapse.png");
    } else {
    $img.attr("src", "expand.png");
    }
   });
</script> 


show close toggle
Click the icons to show, hide, or toggle this element.

Please help us spread the word by socializing it today!

email contact us

Did you find something wrong with the information on this page? Please take a moment to report it to us so that we can continue to improve the quality of the information on this site. Click here to report an issue with this page.



Recommended Books & Training Resources

HTML CSS and JavaScript Editor Professional jQuery Pro jQuery