Quote

Why Aren't You Using jQuery: PART 3

Posted ByJeffrey Jordan Way The date this entry was posted.05/21/2008 Comments34 Comments CategoryjQuery

In this third lesson, we are going to create a "flash-like" image gallery with the help of jQuery. We'll have a list of large images. When the user clicks on one of these images, we will display it in a div. While the image is loading, a "loading" animated gif will display. Depending on how fast your connection is, you may or may not see the gif. View the simple example HERE.

The Semantic Markup

Before we begin, we must create the html. We might as well do it the most semantic way possible. First, import your jQuery script.

<script src="js/jquery.js" type="text/javascript"></script>

Then, add the images.

  <div id="container">
<div id="loader">
<h3>Click on an image to view it full size.</h3>
</div>
<ul id="imageOptions">
<li><a href="#"><img src="img/1.jpg" alt="image" /></a></li>
<li><a href="#"><img src="img/2.jpg" alt="image" /></a></li>
<li><a href="#"><img src="img/1.jpg" alt="image" /></a></li>
<li><a href="#"><img src="img/2.jpg" alt="image" /></a></li>
<li><a href="#"><img src="img/1.jpg" alt="image" /></a></li>
</ul>
</div>

The div with an id of loader is just a white box containing the text from the h3 tag. This will be the div that shows the large version of the image that the user chooses. The imageOptions div will be floated to the right and will contain thumbnails of all the available choices. For the sake of the example, I just have two large images that repeat.

The Outline

I always find that coding is easiest when I start by saying out loud what I am trying to do.

"When the user clicks on an image, we need to get rid of the text. Then we need to display the animated gif which tells the user that the image is currently loading. When the image has loaded, we want to remove the animated gif. Finally, we want to slowly fade in the new image."

Implementing the jQuery

I will recommend that you create a new .js file and then import it into your main html page. For example...

<script src="js/imageGallery.js" type="text/javascript"></script>

Paste the following into your javascript file.

$(function()
{
$("#imageOptions a").click(function()
{
var imageSource = $(this).children("img").attr("src");

$("#loader").addClass("loading");
$("h3").remove();
showImage(imageSource);
return false;
});
});

function showImage(src)
{
$("#loader img").fadeOut("slow")
.remove();
var largeImage = new Image();
$(largeImage).attr("src", src)
.load(function()
{
$(largeImage).hide();
$("#loader").removeClass("loading")
.append(largeImage);
$(largeImage).fadeIn("slow");
});
}

Now, let's take each line step by step.

    
$(function()

This is shorthand for "$(document).ready(function{});"

  
$("#imageOptions a").click(function()

When the user clicks on an anchor tag that is within an element with an id of "imageOptions", run a function.

var imageSource = $(this).children("img").attr("src");

"Find all img child elements of the anchor that the user clicked on.  Then grab the source of that specific image.

$("#loader").addClass("loading");

Get the element with an id of "loader" and add a class called "loading" to it. Our CSS file will contain a background image of the animated gif.

.loading
{
background: url('../loadingIcon.gif') no-repeat 50% 50%;
height: 100%;
width: 100%;
}
$("h3").remove();

Here, we are removing the level three heading tag. We don't want the text to show up when an image is revealed.

showImage(imageSource);

We are now calling a function called "showImage". We'll pass in one parameter - "imageSource". "imageSource", if you remember from a few lines above, is equal to the source url of the image that was clicked on.

Now, we need to create the "showImage" function. This function will accept one parameter. That parameter needs to contain the url to the source of the image that was clicked on.

function showImage(src)

We create the new function.

$("#loader img").fadeOut("slow")
.remove();

Wrap the images that are children of an element with an id of "loader". Then, fade them out slowly. Let's say that an image is already displaying when a new image is clicked. That large image will fade out...and the new image will fade in!

var largeImage = new Image();

Here, we are creating a new variable called "largeImage".

$(largeImage).attr("src", src)

Now that we've dynamically created a new image, we need to add the source attribute to it. We are setting the "src" property equal to the a parameter that we received when the "showImage" function was called.

.load(function()

Next, we load the image.

$(largeImage).hide();

If we want the image to dramatically fade in, we'll need to initially hide it.

$("#loader").removeClass("loading")

When the image has fully loaded, we want to get rid of the animated gif. We do this by removing the class("loading") that has the gif as its background.

.append(largeImage);

Almost finished! Here we are adding the new image to the bottom of the "loader" element.

$(largeImage).fadeIn("slow");                      

Lastly, we create a smooth transition by slowly fading in the new image.

VIEW THE FINAL PRODUCT.

READ PART 1
READ PART 2
READ PART 2.5

Download the revised source code.

Detached Designs
Avatar Submitted by fsd on 11/18/2008 1:07:00 AM
fds
Avatar Submitted by jeff on 11/15/2008 1:27:00 AM
Do you ever reply to comments?
Avatar Submitted by Josette Grimes on 11/12/2008 6:41:00 PM
zd02kbbg389c0f6e
Avatar Submitted by on 11/12/2008 6:41:00 PM
Avatar Submitted by sayooj on 11/12/2008 1:55:00 AM
it is very very useful site , very very thanks
Avatar Submitted by sayooj on 11/12/2008 1:54:00 AM
it is very very useful site , very very thanks
Avatar Submitted by emptywalls on 11/7/2008 4:41:00 PM
Great stff, I'm learning a lot about JQuery from all of your interesting tutorials.
Avatar Submitted by Tedd on 11/4/2008 10:18:00 AM
I am dying to know how to add descriptions below pictures. can anybody help, please?
Avatar Submitted by OutOfTouch on 10/30/2008 8:28:00 PM
How can you add unique descriptions below each img?
Avatar Submitted by fano on 10/25/2008 12:56:00 PM
I like it! I am actully thinking about adding descriptions underneath each image once loaded... any idea?
Avatar Submitted by çiçekçi on 10/25/2008 3:45:00 AM
very thanks
Avatar Submitted by Tom on 10/24/2008 8:28:00 PM
JQuery is great, there are so much stuff to learn.. so little time!
Avatar Submitted by Stephen Ainsworth on 10/14/2008 7:53:00 AM
I've been unsure what jQuery is...but thanks to your tutorials I have a better understanding, thanks.
Avatar Submitted by Veera on 10/10/2008 2:15:00 AM
Useful tutorial.
Avatar Submitted by dimi on 10/7/2008 6:33:00 AM
useful , step by step. Thanks for that source
Avatar Submitted by Alan on 10/6/2008 1:50:00 PM
Brilliant tutorial , I'm very new to Jquery but this has helped me a lot. thanks
Avatar Submitted by Ben K on 9/18/2008 8:27:00 PM
Nice tutorial. Thanks. One thing: the image fading does not work in Safari. To fix, change this: $(largeImage).hide(); To this: $(this)[0].style.display = "none";
Avatar Submitted by bocelal on 9/15/2008 4:31:00 PM
ercvarnositt
Avatar Submitted by robi on 8/26/2008 1:04:00 AM
why the fadeIn/fadeOut doesn't work with Safari? :(
Avatar Submitted by AuntieSocial on 8/13/2008 4:59:00 AM
tweaked js code and html to load thumbnails, not resized large images: in showImage.js replace: +++++++++++++++++++++++++++++++++++++++++++++ var imageSource = $(this).children("img").attr("src"); +++++++++++++++++++++++++++++++++++++++++++++ with: +++++++++++++++++++++++++++++++++++++++++++++ var imageSource = $(this).attr("href"); +++++++++++++++++++++++++++++++++++++++++++++ in the gallery.htm replace: +++++++++++++++++++++++++++++++++++++++++++++ <ul id="imageOptions"> <li><a href="#"><img src="img/1.jpg" alt="image" /></a></li> +++++++++++++++++++++++++++++++++++++++++++++ with: +++++++++++++++++++++++++++++++++++++++++++++ <ul id="imageOptions"> <li><a href="img/1.jpg"><img src="img/tn1.jpg" alt="image" /></a></li> +++++++++++++++++++++++++++++++++++++++++++++
Avatar Submitted by Casey Wise on 8/10/2008 11:48:00 AM
Thank you for posting this. I was looking for a small snippet of code and you had it. Hadn't really put "children" to use, but wow, how powerful! Very good stuff, thanks again.
Avatar Submitted by mT on 7/23/2008 9:45:00 AM
cool!!!
Avatar Submitted by Jeremy Cook on 7/16/2008 12:37:00 PM
Great article-thanks. I've created 3 image galleries on a website I'm working on using your code (I haven't added an attribution yet but will do when it's all fully working). It all works perfectly in Firefox but in other browsers if you click on a thumbnail to view an image for a second time the image doesn't load and the loading gif image appears forever. You can see one of the galleries at www.bendaviesceramics.co.uk/stone.php. If you could offer me any suggestions on this I'd be very grateful.
Avatar Submitted by Rey on 7/14/2008 7:51:00 AM
I'm new at jQuery, but I notice something, because I have run into the same problem. When clicking on another picture, the current picture does not fade out; it immediately skips to the remove part of the code. There is a reason for this, and there is a way to get around it, but at the current moment, images presently in the loader div do not fade out before they are removed, they are simply removed. I'm incorporating some of your example into a site I'm currently working on, so once I figure out how to get around it, if no one else submits it I will.
Avatar Submitted by TG on 7/10/2008 2:43:00 PM
Good example, but I noticed the actual operational code was slightly different; you changed the src attribute of the large image AFTER the load function was defined. . . . will the order effect what happens or not? I'm asking cause the code in the toot didn't work for me but when I stole your functional code it worked and then I noticed the difference.
Avatar Submitted by Hubert on 6/23/2008 1:03:00 PM
Great blog and great script and I've only just found out about jQuery. I wonder if it is possible to substitute the "hardcoded" list of images by some innerHtml generated with ajax code from an xml-file. I've tried it so far and i get the thumbs inside the imageOptions box but then the functonality of jQuery is gone! Below is the ajax code I inserted into your script to fill the imageOptions box. The thumbs are nicely placed but clicking them doesn't have any effect... ---here comes the javascript tag -- var xmlDoc; // code for IE if (window.ActiveXObject) { xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); } // code for Mozilla, Firefox, Opera, etc. else if (document.implementation.createDocument) { xmlDoc=document.implementation.createDocument("","",null); } else { alert('Your browser cannot handle this script'); } xmlDoc.async=false; xmlDoc.load("xmls/slidelist.xml"); var x=xmlDoc.getElementsByTagName("slides"); function pg1() { page1=(x[0].getElementsByTagName("d1")[0].childNodes[0].nodeValue); document.getElementById("imageOptions").innerHTML= page1; } --- end of the js tag ---
Avatar Submitted by george marcotte on 6/17/2008 2:23:00 AM
I am looking at the two small functions you wrote from my c++ java and php mind and I've lost the plot... where dones this thing know that 1.jpg is the larger version of tn1.jpg ? I am looking for some code like loadimage( substr(imagename,2), "slow") but I dont see it. ok time to start at the begining part 1 thanks for you web site anyway george
Avatar Submitted by Erwin on 6/4/2008 5:05:00 PM
Okay... think I need some more of your lessons..:) Where do you place the reference folder? Probably in the var imageSource = $(this).children("img").attr("src"); line??
Avatar Submitted by Jeffrey Way on 6/4/2008 3:06:00 PM
Erwin - absolutely. I used the same image for the sake of simplicity. The only difference is that you would reference a folder containing the larger images.
Avatar Submitted by Erwin on 6/4/2008 2:28:00 PM
Great script was just wondering if it possible to use separate thumbs instead of the resized larger image?
Avatar Submitted by Jeffrey Way on 5/24/2008 6:21:00 PM
Ray, that gallery is meant as more of an example. But to answer your question, you would just add, "overflow: scroll;" to the image list.
Avatar Submitted by ray on 5/24/2008 5:24:00 PM
what happens where there a lots of images though, does the column of images move into scroll mode or something?
Avatar Submitted by Jeffrey Way on 5/23/2008 11:42:00 AM
Why do you think I've done a whole series on it?! :)
Avatar Submitted by weblizzer on 5/23/2008 11:33:00 AM
Wow, i love the post, i can't imagine that this is how easy to deal with jquery. love it.

Leave A Comment

Name:

*

Email:

*

Your Website:

Comments:

*


Sponsors

Theme Forest Templates

Text Sponsors

DotNetTemplates

Videos

Most Popular

Recent Postings

Categories

Recent Comments

Dtech web design said...

Wow!! Excellent tutorial. Easy to understand as well. Saved me a lot of time. Is it possible to validate and submit forms using jquery and an Ajax/php based script. A brief tutorial would be nice. Thanks again

Luke said...

Good article, but one thing is you might want to use the proper onload of jquery. That is the $(document).ready(function() {}); function, it loads it when the DOM is ready rather than the page.

Sold Articles

Profile

Jeffrey Jordan Way

Tweets

quotes


You are viewing a mobilized version of this site...
View original page here

How do you rate mobile version of this page?

Mobilized by Mowser Mowser