This article contains 10 visual tutorials intended for web designers and newbies on how to apply Javascript effects with jQuery. In case you don't know about jQuery, it is a "write less, do more" Javascript library. It has many Ajax and Javascript features that allow you to enhance user experience and semantic coding. Since these tutorials are focused on jQuery, I'm not going to get into the details of the CSS.

How jQuery works?

First you need to download a copy of jQuery and insert it in your html page (preferably within the <head> 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 all <h3> element

$("div#content .photo") = get all element with class="photo" nested in the <div id="content">

$("ul li") = get all <li> element nested in all <ul>

$("ul li:first") = get only the first <li> element of the <ul>

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 <div id="panel"> element and then toggle a CSS class="active" to the <a class="btn-slide"> element. The .active class will toggle the background position of the arrow image (by CSS).

4a. Accordion #1

The first line will add a CSS class "active" to the first <H3> element within the <div class="accordion"> (the "active" class will shift the background position of the arrow icon). The second line will hide all the <p> element that is not the first within the <div class="accordion">.

When the <h3> element is clicked, it will slideToggle the next <p> and slideUp all its siblings, then toggle the class="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 to display: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.

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 <ul> list with class="pane-list" and you want to make the nested <li> clickable (entire block). You can assign the click function to ".pane-list li"; and when it is clicked, the function will find the <a> element and redirect the browser location to its href attribute value.

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 <div class="message_body"> after the first one.Second line: hide all <li> element after the 5thThird part: when the <p class="message_head"> is clicked, slideToggle the next <div class="message_body">Fourth part: when the <a class="collpase_all_message"> button is clicked, slideUp all <div class="message_body">Fifth part: when the <a class="show_all_message"> is clicked, hide this, show <a class="show_recent_only">, and slideDown all <li> after the fifth one.Sixth part: when the <a class="show_recent_only"> is clicked, hide this, show <a class="show_all_message">, and slideUp all <li> after the 5th.

8. Imitating the WordPress Comment Backend

I think most of you have probably seen the WordPress Ajax comment management backend. Well, let's imitate it with jQuery. In order to animate the background color, you need include the Color Animations plugin. (view demo)

First line: will add "alt" class to even <div class="pane"> (to assign the grey background on every other <div >)Second part: when <a class="btn-delete"> is clicked, alert a message, and then animate the backgroundColor and opacity of <div class="pane">Third part: when <a class="btn-unapprove"> is clicked, first animate the backgroundColor of <div class="pane"> to yellow, then white, and addClass "spam"Fourth part: when <a class="btn-approve"> is clicked, first animate the backgroundColor of <div class="pane"> to green, then white, and removeClass "spam"Fifth part: when <a class="btn-spam"> is clicked, animate the backgroundColor to red and opacity ="hide"

9. Image replacement gallery

Suppose you have a portfolio where you want to showcase multi images without jumping to another page, you can load the JPG into the target element. (view demo)

First append an empty <em> to H2.

When a link within the <p class=thumbs> is clicked:
- store its href attribute into a variable "largePath"
- store its title attribute into a variable "largeAlt"
- replace the <img id="largeImg"> scr attribute with the variable "largePath" and replace the alt attribute with the variable "largeAlt"
- Set the em content (within the h2) with the variable largeAlt (plus the brackets)

10. Styling different link types

With most modern browsers, styling the link selector is very easy. For example, to style the link to .pdf file, you can simply use the following CSS rule: a[href $='.pdf'] { ... }. Unfortunately, IE 6 doesn't support this (this is another reason why we hate IE!). To get around this, you can do it with jQuery. (view demo)

The first three lines should be very straight foward. They just a CSS class to the <a> element according to their href attribute.

The second part will get all <a> element that does not have "http://www.webdesignerwall.com" and/or does not start with "#" in the href attribute, then addClass "external" and set target= "_blank".

1,131 Comments

Thank you, I’ve just been searching for information about this subject for ages and yours is the best I’ve found out so far. But, what about the bottom line? Are you sure in regards to the source?|What i do not realize is in reality how you’re no longer really a lot more neatly-preferred than you may be right now. You’re so intelligent.

I’ve found out so far. But, what about the bottom line? Are you sure in regards to the source?|What i do not realize is in reality how you’re no longer really a lot more neatly-preferred than you may be right now. You’re so intelligent.

This is such an innovative idea that feeds my brain, although it doesn’t look too simple for me. I am a newbie jquery user too so this is a fresh juice for my day. I have been exploring web designing for weeks now and these tutorials are definitely of great help.