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)
Parameter | Description |
speed | Optional values in milliseconds, "slow", "normal", and "fast". |
callback | An 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>
Did you find the page informational and useful? Share it using one of your favorite social sites.
Recommended Books & Training Resources