iEntry 10th Anniversary Webforumz RegistrationAnnouncements Contact Webforumz StaffContact
Home Resources Blogs Meet the Team Contact Register

Go Back   WebForumz.com > Resources > Tutorials > Javascript
Register All Albums Blogs FAQ Members List Calendar Search Today's Posts Mark Forums Read

Author


Jack Franklin
Jack is 15 years old and lives happily in Cornwall, England. Web Design is his passion and he works using xHTML, CSS and PHP to code his websites, with the odd bit of Javascript now and then. In his spare time, Jack enjoys playing Football, rugby, tennis and snooker (but not all at the same time).

RSS Feed

Your Options
Creating a Javascript Image Gallery

This tutorial will show you how to create a basic image gallery using javascript.
I’m only just beginning Javascript myself, believe it or not, and I’ve been working on an Image Gallery for a while now. I found it slightly taxing, so hopefully this tutorial should help you with any difficulties. I’ll try and do it as simply as possible! For this tutorial, you will just need a very basic knowledge of Javascript, functions, arrays, Nodes and basic DOM functions. Give yourself about 30minutes-1 hour to complete this tutorial, and if you’ve any questions leave a comment and I’ll get back to you ASAP. 

Firstly, I’ve set up a basic HTML page with a list of links:

<h3>Image Selector</h3>
<ul id="imagegallery">
<li><a href="images\Bluehills.jpg"title="Blue Hills">Blue Hills</a></li>
<li><a href="images\sunset.jpg"title="Sunset">Sunset</a></li>
<li><a href="images\lilies.jpg"title="Water Lilies">Water Lilies</a></li>
<li><a href="images\winter.jpg"title="Winter">Winter</a></li>
</ul>
 <imgid="placeholder" src="images/placeholder4x3.jpg" alt="my image gallery" />
<p id="description">Choose anImage</p>

Here is what my Image Gallery Should do:

1. When I click on a link, I want to stay on the same page. I also want to see the image on the same page plus its description.

And here is how:

  1. Put a placeholder image on the page and a blank description tag.
  2. Using Javascript, more specifically, the DOM Model, I’ll intercept the default browser behaviour and replace the text and image with the correct image and description.

To do this I’m going to create a Javascript Function. This will enable my image gallery to be reused. The function is going to be called ‘showPic’ and will have one argument, whichpic, which will select the picture on which the function will act:

function showPic(whichpic) {
}

I need to find the URL of the Image I want to display, so I use ‘get.Attribute’ to get the HREF. I then store it in a variable for later use.

var source = whichpic.getAttribute(“href”);

And now I need to find the image I want to replace. You will notice that in the code it has an id of placeholder, so we can find it using that id:

var placeholder = document.getElementById(“placeholder”);

The placeholder image gets its image via the ‘src’. So, I need to change this using Javascript. Luckily, I can use ‘set.Attribute’.

placeholder.setAttribute(“src”, source);

The syntax for the set.Attribute is:

set.Attribute(“attribute”, valuetoset);

That’s our function for switching the pictures done! Obviously it wont work yet, because we have not added an event handler. Now we need to work on changing the text in the paragraph with the id ‘description’.

Just to recap, currently our function is:

function showPic(whichpic) {
var source = whichpic.getAttribute("href");
var placeholder = document.getElementByID("placeholder");
placeholder.setAttribute("src", source);
}

So, time to take a break, because there is still a long way to go! Next we need to add the script which will change the description to the image which is selected.

Firstly, I want to get the description of each image. The description is stored in the title tag. We can use get.Attribute, like earlier, to do this.

var text = whichpic.getAttribute("title");

Notice for the first time I am now referencing the function’s only argument, whichpic. Basically, this line will get the value of the title attribute and assign it to a variable, called text. I’m now going to add a variable called ‘description’, which will be the paragraph called description. I’m doing this for easy referral later in the script.

var description = document.getElementById(“description”);

Now lets do some text swapping! This is where the knowledge of nodes is so helpful. I need to find the place where my text in the title attribute will go. The text, is the first child of the <p> tag, so I use:

description.firstChild.nodeValue

And now I need to assign a value to it. The value is the variable ‘text’, which I set earlier, which gets the value of the title attribute of the image.

description.firstChild.nodeValue = text; 

Done! It’s really as easy as that! The full function is:

function showPic(whichpic) {
var source = whichpic.getAttribute("href");
var placeholder = document.getElementByID("placeholder");
placeholder.setAttribute("src", source);
var text = whichpic.getAttribute("title");
var description = document.getElementById("description");
description.firstChild.nodeValue = text;
}

 Now, to activate this (Known as an Event Handler), we need to place this code in each link:

onclick=”showPic(this); return false;” 

So your links now should each be like this: 

<li><a href="images\winter.jpg" title="Winter" onclick="showPic(this); return false;">Winter</a></li>

And remember to include your javascript! The recommended way is to place it all in an eternal file and then in the <head> section place:

<script type=”text/javascript” src=”yourjavascriptfile.js”></script>


This script is pretty much as basic as you will get. There are so many things that could be improved with this script. For instance, if you had 50 links, try adding the ‘onClick…’ over and over again! There are ways of doing it for you. Watch out, as in next month’s editor’s article I’ll be posting a follow up tutorial that will help you improve and develop this script.



Members's Comments No Comments. Be the first to comment on and rate this resource.


Search Engine Optimization by vBSEO 3.2.0 RC8