Javascript Tip: Easy Fading Using jQuery
Thursday, April 30th, 2009
Whilst working on a project recently I came across a need for us to show a selection of images depending on their type and when a link to them is clicked. I decided to use jQuery as it saves a lot of time as most of the work is done for you. I will step through the code so you can see how I did it.
View Demo
As with any jQuery, we are required to use the following line to tell our jQuery code to run as soon as DOM is ready rather than when the page has finished loading.
$(document).ready(function(){
The next line detects any click from a link inside the div with an id of “links”
$('#links a').click(function(){
Once a link has been clicked any attribute from that link is accessible through $(this), so the next line sets up a new variable called “color” and takes the “rel” attribute from the tag and puts it into our new variable.
var color = $(this).attr('rel');
Once we know which colour link has been clicked we can then fade out all images that have a different colour and fade in the colour that has been clicked.
$('#tiles div.tile:not(.' + color + ') img').fadeTo("slow", 0.3);
$('#tiles div.' + color + ' img').fadeTo("slow", 1);
To finish off we need to close our function brackets. You will notice there is a “return false;”. This is to stop the link actually activating the href attribute and basically overriding it so we can do our magic. This becomes useful when users don’t have Javascript activated in there browser, you can make the link work as normal. Unfortunately, you would need to create separate pages with different colours highlighted instead. (sigh!)
So, to sum up I have put all the code below and you will need to remember to include the latest jQuery Javascript file from www.jquery.com
All the HTML you can grab from the source of the demo page.
Final Code
$(document).ready(function(){
$('#links a').click(function(){
var color = $(this).attr('rel');
$('#tiles div.tile:not(.' + color + ') img').fadeTo("slow", 0.3);
$('#tiles div.' + color + ' img').fadeTo("slow", 1);
});
return false;
});
Hopefully, this will be useful to someone. Have fun!
View the Demo
If you enjoyed this article, you might consider subscribing to our rss feed to stay updated with all the latest tips and articles!
Article Sponsored by:
Expert guidance on designing a great theme for one of the most popular, open-source blog systems available for the Web today! This book can be used by WordPress users or visual designers (with no server-side scripting or programming experience) who are used to working with the common industry-standard tools like PhotoShop and Dreamweaver or other popular graphic, HTML, and text editors. Regardless of your web development skill-set or level, you'll be walked through the clear, step-by-step instructions, but familiarity with a broad range of web development skills and WordPress know-how will allow you to gain maximum benefit from this book.








Great stuff John, the demo has a really pleasing effect. Hope to see more of this kind of stuff from you in the future
Thanks Drew, I am going to have a go at a jQuery plugin tutorial next I think.
nice effect ….
Thanks so much for writing/creating this! Any chance anyone can show us how to make it play through the steps automatically while keeping the current functionality? Just looked like it should to me, but I’m still a newb at jquery/javascript so I can’t do it myself.