How jQuery works?
First you need to download a copy of jQuery and insert it in your html page (preferably within the tag). Then you need to write functions to tell jQuery what to do. The diagram below explains the detail how jQuery work:
How to get the element?
Writing jQuery function is relatively easy (thanks to the wonderful documentation). The key point you have to learn is how to get the exact element that you want to apply the effects.
$("#header")
= get the element with id="header"$("h3")
= get allelement
$("div#content .photo")
= get all element with class="photo" nested in the$("ul li")
= get all- element nested in all
$("ul li:first")
= get only the first- element of the
1. Simple slide panel
Let’s start by doing a simple slide panel. You’ve probably seen a lot of this, where you click on a link and a panel slide up/down. (view demo)
When an elment with class="btn-slide" is clicked, it will slideToggle (up/down) the
element and then toggle a CSS class="active" to the element. The .active class will toggle the background position of the arrow image (by CSS).$(document).ready(function(){
$(".btn-slide").click(function(){
$("#panel").slideToggle("slow");
$(this).toggleClass("active");
});
});2. Simple disappearing effect
This sample will show you how to make something disappear when an image button is clicked. (view demo)
When the
is clicked, it will find its parent element
and animate its opacity=hide with slow speed.$(document).ready(function(){
$(".pane .delete").click(function(){
$(this).parents(".pane").animate({ opacity: "hide" }, "slow");
});
});3 Chain-able transition effects
Now let’s see the power of jQuery’s chainability. With just several lines of code, I can make the box fly around with scaling and fading transition. (view demo)
Line 1: when the is clicked
Line 2: animate theopacity=0.1, left property until it reaches 400px, with speed 1200 (milliseconds)
Line 3: then opacity=0.4, top=160px, height=20, width=20, with speed "slow"
Line 4: then opacity=1, left=0, height=100, width=100, with speed "slow"
Line 5: then opacity=1, left=0, height=100, width=100, with speed "slow"
Line 6: then top=0, with speed "fast"
Line 7: then slideUp (default speed = "normal")
Line 8: then slideDown, with speed "slow"
Line 9: return false will prevent the browser jump to the link anchor$(document).ready(function(){
$(".run").click(function(){
$("#box").animate({opacity: "0.1", left: "+=400"}, 1200)
.animate({opacity: "0.4", top: "+=160", height: "20", width: "20"}, "slow")
.animate({opacity: "1", left: "0", height: "100", width: "100"}, "slow")
.animate({top: "0"}, "fast")
.slideUp()
.slideDown("slow")
return false;
});
});4a. Accordion #1
Here is a sample of accordion. (view demo)
The first line will add a CSS class "active" to the first
element within the
(the "active" class will shift the background position of the arrow icon). The second line will hide all theelement that is not the first within the
.When the
element is clicked, it will slideToggle the next
and slideUp all its siblings, then toggle the class="active".
$(document).ready(function(){
$(".accordion h3:first").addClass("active");
$(".accordion p:not(:first)").hide();
$(".accordion h3").click(function(){
$(this).next("p").slideToggle("slow")
.siblings("p:visible").slideUp("slow");
$(this).toggleClass("active");
$(this).siblings("h3").removeClass("active");
});
});4b. Accordion #2
This example is very similar to accordion#1, but it will let you specify which panel to open as default. (view demo)
In the CSS stylesheet, set
.accordion p
todisplay:none
. Now suppose you want to open the third panel as default. You can write as$(".accordion2 p").eq(2).show();
(eq = equal). Note that the indexing starts at zero.$(document).ready(function(){
$(".accordion2 h3").eq(2).addClass("active");
$(".accordion2 p").eq(2).show();
$(".accordion2 h3").click(function(){
$(this).next("p").slideToggle("slow")
.siblings("p:visible").slideUp("slow");
$(this).toggleClass("active");
$(this).siblings("h3").removeClass("active");
});
});5a. Animated hover effect #1
This example will create a nice animated hover effect with fade in/out. (view demo)
When the menu link is mouseovered, it will find the next and animate its opacity and top position.
$(document).ready(function(){
$(".menu a").hover(function() {
$(this).next("em").animate({opacity: "show", top: "-75"}, "slow");
}, function() {
$(this).next("em").animate({opacity: "hide", top: "-85"}, "fast");
});
});5b. Animated hover effect #2
This example will get the menu link
title
attribute, store it in a variable, and then append to the tag. (view demo)The first line will append an empty
to the menu
element.
$(document).ready(function(){
$(".menu2 a").append("");
$(".menu2 a").hover(function() {
$(this).find("em").animate({opacity: "show", top: "-75"}, "slow");
var hoverText = $(this).attr("title");
$(this).find("em").text(hoverText);
}, function() {
$(this).find("em").animate({opacity: "hide", top: "-85"}, "fast");
});
});6. Entire block clickable
This example will show you how to make the entire block element clickable as seen on my Best Web Gallery’s sidebar tabs. (view demo)
Suppose you have a
- list with class="pane-list" and you want to make the nested
- clickable (entire block). You can assign the click function to ".pane-list li"; and when it is clicked, the function will find the
element and redirect the browser location to its
href
attribute value.$(document).ready(function(){
$(".pane-list li").click(function(){
window.location=$(this).find("a").attr("href"); return false;
});
});7. Collapsible panels
Let’s combine the techniques from the previous examples and create a serie of collapsible panels (similar to the Gmail inbox panels). Notice I also used the same technique on Web Designer Wall comment list and Next2Friends message inbox? (view demo)
First line: hide all
- clickable (entire block). You can assign the click function to ".pane-list li"; and when it is clicked, the function will find the
Không có nhận xét nào:
Đăng nhận xét