Latest tutorial: Making a Movieclip face another Movieclip or point on the stage | Ask Tutorial5!
 

Creating a JavaScript photo gallery which automatically change

(90 votes)
Written by AnaS   

This tutorial explains the manner you can create a JavaScript photo gallery which automatically change.
Prepare 5 images for introducing them in the JavaScript gallery.

Create a HTML file and introduce one of the images selected with the "photo-gallery" id (set the src, width and height attributes corresponding to your own images):

<img src="photos/pic1.jpg" id="photo-gallery" width="420" height="260">

Now let's create a JavaScript function that will automatically unroll the images at one second interval.
Put this code in the head section of your HTML document:

<script type="text/javascript">
var c=0
var s
function photoGallery()
{
if (c%5==0){
document.getElementById('photo-gallery').src = "photos/pic1.jpg";
}
if (c%5==1){
document.getElementById('photo-gallery').src = "photos/pic2.jpg";
}
if (c%5==2){
document.getElementById('photo-gallery').src = "photos/pic3.jpg";
}
if (c%5==3){
document.getElementById('photo-gallery').src = "photos/pic4.jpg";
}
if (c%5==4){
document.getElementById('photo-gallery').src = "photos/pic5.jpg";
}
c=c+1
s=setTimeout("photoGallery()",1000)
}
</script>

We use here the c (initially having 0 value) variable which is incremented every one second. We use the getElementById method (which returns the object with the "photo-gallery" id ) of the document object (that represents the entire HTML page).
Depending of the division remainder ( JavaScript modulus operator: c%5) it is chosen the picture to be displayed.

In the last line of the script it is used the setTimeout() method for calling the photoGallery() function each 1000 miliseconds (that means 1 second).

Now just use the onLoad Event Handler and call the photoGallery() function from the body.

<body onLoad="photoGallery()">

You can also launch application with JavaScrip .

Great job! The result should look like this PHOTO GALLERY.



Subscribe now via RSS feed and get all the new tutorials

written by Chris , September 14, 2007

How can you get this work with master pages/
written by Giji , October 24, 2007

Thanks for your great help
written by vijju , October 29, 2007

how can we use it in flash (i want the images attach with database). If we can add new images, the new images also display in the slide show.
written by Ruaan , November 15, 2007

Good day,

I want to know, I want to coundown the days to christmas via pictures but have them change every day, can you help me??

thank you,
written by AnaSt , November 19, 2007

Hi Ruaan! You may create a gallery that shows a picture depend on current date.

For example you have the currentDate variable that holds the current date of the month:

var datetime=new Date();
var d=currentDate.getDate();

Having the above example you can do as it follows:

if (d==1){
document.getElementById('photo-gallery').src = "photos/pic1.jpg";
}

and so on.

written by Joseph Srisuk , November 25, 2007

I like this!"Creating a JavaScript photo gallery which automatically change", but I'm working on improve my site at coolpage.biz. In my gallery I like to have small thumb at left and display on the right in flae = x: 291, y: 447
Could you advice on how to program it to: when click on thumb on left, this will call the enlarge size to display panel on the right, in place of the initial displaying animated MC.??
Hoped that you could help out,
Thanks
written by Rathi , November 30, 2007

hai

how to change the document in weekly once automatically

give me a soution
written by AnaSt , December 10, 2007

Rathi, see the line:
s=setTimeout("photoGallery()",1000)

1000 represents the interval the function is called (in my example 1 second). So calculate how may milisecond a week has and replace the value 1000 with the new value.

That is all!
written by sweden , December 12, 2007

Thankss
written by Rupali Patil.. , January 15, 2008

It's really fine...!
written by Abbas , January 22, 2008

This is exactly what I was looking for, thanks for the tutorial
written by Adept Infotech , January 27, 2008

I want to take all images from a folder to show website designing portfolio.
written by nick chau , June 18, 2008

Excellent instructions. Thanks a lot. My Web site is looking so much better.
written by Zzypty Zzyp , July 23, 2008

Hi! The code works perfectly! But, I've a couple of question -

1) If I have more than five images, say around 100 or so, is there any way to write the code? As per your present mode of writing code, the 100-image gallery code will extend to 300 lines.

2) The code changes images automatically every 1 second. Is there any way to change he image when a person clicks on button or clicks on the image?

Thanks
Zzypty
written by gigi , October 10, 2008

im sort of new to this just have a question on where do i put the last tag for the body? before each img src or ?

im confused thanks
written by Edwin , October 30, 2008

can any one please give me the javascript code for html mouse over zoom effect . ( For exampel clip.jpg image was in left side the same clip.jpg image should be in right side ,The right side should be in zoom( ) effect if i keep mouse on left side of clip.jpg )



written by dangot , November 01, 2008

Hi: How can I have the fotoalbun to change when the people click over the picture they choose to see larger?

How can I do for showing that picture with a short explainig text?

Thanks.
written by http://freetriptojapan.blogspot.com , November 06, 2008

nice but you should to put examples here, so we can see how it works..
written by Bogs , November 25, 2008

Hi. Is it possible for the images to contain links to other pages on the site?
Thanks
written by boja , November 26, 2008

hi,
nice work!
how to modify this to stop changing loop when mouse out?(need only to change(auto) images only on mouse over)

any ideas?

thanks,
written by Ritesh Kumar , January 21, 2009

I develop a wallpaper calendar in javascript and vbscript codes I used 42 pictures in this wallpaper which is changed after 10000 miliseconds . I ask you a question? Is this possible the pictures is changed with changing the date and no effect on it when i refresh the screen?
written by periyasamy , January 30, 2009

create a banner ads at the top of the page. The ads are changed every
10 or 15 seconds
written by a. umesh kumar , March 28, 2009

Hey yarrr! very thnks to give a simple code to image deck,

thnku, thanku
written by a. umesh kumar , March 28, 2009

Ok! i like this concept and very useful to me, at the same time, If we place the mouse over the image, it should be stop running image, culd u tell me the idea r coding

Regards

A. umesh kumar
written by Maya Nainani , April 07, 2009

hi,
i m having images in one folder say in images folder. and i want all the images of that folder in a slide show. so how to do ??
can i use datasets or array and also how shall i??
plz can anyone help me....
written by Grenso , April 23, 2009

I like the concept of creating a javascript photo gallery automatically. I am tired of seeing the same pictures all thetime on my website and instead I would the pictures to automatically change everyday thus highlighting new pictures. As you can see I have many pictures and I would want all of them to change automatically. Do you have any ideas. Any assistance will be greatly appreciated.
written by Annie T , May 02, 2009

Thanks! It worked great. Is there a way to fade from one picture to the next?
written by jojo , May 07, 2009

thank u so much :D
written by Philos Peter , May 08, 2009

Thanks so much...
written by Philos Peter , May 08, 2009

Hi friends, can i give link to each images in the photo gallery?
Thanks
written by PaulVW , July 23, 2009

Is there a random command with javascript?
How would you change the code to make the images be displayed in a random order?
Thanks,
Paul
written by kbaisley , July 24, 2009

Hi, I'd like to know how to do this but not over time. I'd like to have a picture change for every page people visit on my site and go through the library. Can anyone advise?
written by Gary Miller , August 03, 2009

It's not working for me. I used the code exactly as illustrated in Dreamweaver and it works fine when I preview the web page. But when I upload it to the website, I see the first picture flash by and then get a box with the name of the picture and a red X.

Any suggestions
written by computer turkey , August 21, 2009

hi,
i m having images in one folder say in images folder.
written by AshhaR , September 09, 2009

Hi,
The script u hv provided is very usefull,
THANQUUUUUU VERYYYY MUCH :)
written by Parmanand , October 01, 2009

Very Fantastic thanks thanks again
written by mustafa , December 04, 2009

sir i have tried ur code but images are not displayed
written by Lumi , December 28, 2009

For Mustafa,

Sir, the code is functioning very well. Verify if you have given the correct path for your photos.

Thanks a lot for this usefull script!
written by Error , January 04, 2010

'photo' is undefined?
written by markS , January 04, 2010

Hey
this code is great, but is there any way to add individual links to each image that will go to other pages?
written by markS , January 05, 2010

hey thanks for this - it works great...
But is there any way to add individual links to each image that will go to other pages?

thanks
written by Holger Debek , January 07, 2010

Hi everybody,

as far I can see , it is a wonderful script!! Thanks for sharing. I actually looking for script that changes the image on my webpage every day in the year. The 365 pics I got. Somebody can help me ??? Would be fantastic. In the moment I use a script for only one week changing the pics.

Thanks for thinking about !

Holger

P.S.my german domainname means "Picture of the day"
So you can see, I really could need that script.
written by SueAnne , January 23, 2010

Hi. I wanted the pictures to be a still picture so people can see my latest uploaded photo. Can you help me? Anyways, how do I actually upload the photo up? Thank you!
written by Shweta Soni , September 12, 2011

Thanks A lot For Give us Very Simple Coding For Website
written by max22 , October 12, 2011

Pls give the html code that suit this.pls..pls...
written by ravijs , September 26, 2012

how to make a box of images which changes automaticaly and when i click on any image it redirects to other website whichever i give in source code..
written by Manjeet , September 30, 2012

I want to slide image one by one (something like one is coming and another is going) how its possible in jquarry.
written by Anandraj A , December 28, 2012

Excellent and easy script sir, thanks and very much.....
written by Hemanth , January 11, 2013

no image is displayed it showing red cross
written by Hemanth , January 11, 2013

how to resolve this red cross
written by riz , January 29, 2013

in javascript am using hide - left arrow/ show - right arrow window.
if i click left arrow window gets hiding.
Now i want to know how to change the image to right arrow automatically if user clicks left arrow.

Do you need more help? Ask now!
 

busy
Last Updated ( Sunday, 22 April 2007 )