Jesin's Blog

Welcome to the Portal of Technology

  • Facebook
  • GitHub
  • RSS
  • Twitter
  • Home
  • Categories
    • Domains
    • Linux
    • Networking
    • PHP
    • Virtualization
    • Web Design
    • Web Servers
    • Windows
  • WordPress Plugins
    • Custom Error Pages
    • HTTP Digest Authentication
    • Mailgun Email Validator
  • Toolbox
    • DNS Lookup Tool
    • htdigest Generator Tool Online
    • htpasswd Generator Tool Online
    • HTTP Headers Lookup Tool
    • MD5 Encryption Tool
    • Open Port Check Tool
    • SHA-1 Encryption Tool
    • URL Encoding/Decoding Tool
  • About Me
  • Contact Me
  • Sitemap
Home ›
Web Design ›
Javascript Simple Slideshow

Javascript Simple Slideshow

December 20, 2010 Web Design Jesin A 3 Comments

web design category thumbnail

Create a Simple slideshow using Javascript and HTML which is very easy to understand and implement. This is a no frills slideshow without any special effects and aims at beginners. As you learn more you can add more special effects. The Javascript Simple Slideshow works by changing the src attribute of the img tag to the list of image locations specified in an array variable. And finally I’ll show you how to preload the images so that the transition takes place smoothly.
Lets go step-by-step building this Simple Slideshow using Javascript. I assume you are familiar with the basics of HTML and Javascript

Step 1: Create the HTML Code

Type the HTML code with an img tag showing an image. The img tag should have an id attribute

<html>
<head>
<title>Simple Javascript Slideshow</title>
</head>
<body>
<img id="slideshow" src="photos/photo1.jpg" />
</body>
</html>

The id attribute you specify here will be used by Javascript to modify other attributes of this img element.

Step 2: Add a Javascript Array with images list

Inside the head tag create a script tag with an array variable containing list of image locations.

<html>
<head>
<title>Simple Javascript Slideshow</title>
<script type="text/javascript">
var imgsrc = new Array();
imgsrc[0]="photos/photo1.jpg";
imgsrc[1]="photos/photo2.jpg";
imgsrc[2]="photos/photo3.jpg";
imgsrc[3]="photos/photo4.jpg";
imgsrc[4]="photos/photo5.jpg";
imgsrc[5]="photos/photo6.jpg";
</script>
</head>
<body>
<img id="slideshow" src="photos/photo1.jpg" />
</body>
</html>

The locations specified in the array should be relative to the location where this file is placed. You can add as many images you want by expanding the array. But make sure all the images are of the same size else specify the width and height attribute for the img tag <img id=”slideshow” src=”photos/photo1.jpg” width=”400″ height=”300″ /> If this isn’t specified and you used images of various sizes the page will expand and contract during the slideshow making it look ugly.

Step 3: Create a function to start the Slideshow

We’ll now create a function inside which the src attribute of the img element with id slideshow will be modified. Declare a counter variable which will change the array index of the variable imgsrc at each interval. So we should first initialize the value of of that variable to 0 then it should be incremented each time this function is called. But we’ll face a problem after the counter variable is incremented to value greater than the size of the array. This can be rectified by using an if loop to check whether the value of the counter variable is equal to the size of the array. If so it will be assigned 0 so that it can start again. An inbuilt function named setTimeout will keep calling the slideshow function at regular time intervals.

<html>
<head>
<title>Simple Javascript Slideshow</title>
<script type="text/javascript">
var i = 0, imgsrc = new Array();
imgsrc[0]="photos/photo1.jpg";
imgsrc[1]="photos/photo2.jpg";
imgsrc[2]="photos/photo3.jpg";
imgsrc[3]="photos/photo4.jpg";
imgsrc[4]="photos/photo5.jpg";
imgsrc[5]="photos/photo6.jpg";
function startSlideshow()
{
document.getElementById("slideshow").src=imgsrc[i];
i++;
if(i==imgsrc.length)
{
i=0;
}
setTimeout("startSlideshow()",1000);
}
</script>
</head>
<body>
<img id="slideshow" src="photos/photo1.jpg" />
</body>
</html>

The value 1000 given as a parameter of the function setTimeout indicates the interval time in milliseconds so every one second the function startSlideshow() will be called. The value of the interval can be increased as per requirement.

Step 4: Preload the images

If you start the slideshow at this point the first time it executes it’ll take sometime to load each image as it is called so the transition will not be seamless. To rectify this we’ll preload all the images before the slideshow starts.

<html>
<head>
<title>Simple Javascript Slideshow</title>
<script type="text/javascript">
var i = 0, imgsrc = new Array(), preload = new Array();
imgsrc[0]="photos/photo1.jpg";
imgsrc[1]="photos/photo2.jpg";
imgsrc[2]="photos/photo3.jpg";
imgsrc[3]="photos/photo4.jpg";
imgsrc[4]="photos/photo5.jpg";
imgsrc[5]="photos/photo6.jpg";
for (var j=0;j<imgsrc.length;j++)
{
preload[j] = new Image;
preload[j].src = imgsrc[j];
}
function startSlideshow()
{
document.getElementById("slideshow").src=imgsrc[i];
i++;
if(i==imgsrc.length)
{
i=0;
}
setTimeout("startSlideshow()",1000);
}
</script>
</head>
<body>
<img id="slideshow" src="photos/photo1.jpg" />
</body>
</html>

A new array variable named preload is declared and each image is called inside the for loop thus loading it in the visitor’s system. You can stop at this point and call the startSlideshow() function inside the body onload event <body onload=”startSlideshow()”> To add more functionality read the last step.

Step 5: Play, Pause and Stop the slideshow

The final touch is to add three buttons which will play, pause and stop the slideshow. This will involve creating a function which will accept the mode as a parameter and assign it to a variable. Inside the startSlideshow() function the value of this variable will be checked using else if loops and accordingly the action will happen. To make things more professional the pause and stop buttons will be disabled when the page loads, after pressing the play button the other two buttons will be enabled and the play button will be greyed out. Clicking the Pause button will enable and change the value of Play button to Resume. Clicking Stop will set the value of the Resume button to play, enable it and disable itself and the Pause button. The final code is here

<html>
<head>
<title>Simple Javascript Slideshow</title>
<script type="text/javascript">
var i = 0, imgsrc = new Array(), preload = new Array();
imgsrc[0]="photos/photo1.jpg";
imgsrc[1]="photos/photo2.jpg";
imgsrc[2]="photos/photo3.jpg";
imgsrc[3]="photos/photo4.jpg";
imgsrc[4]="photos/photo5.jpg";
imgsrc[5]="photos/photo6.jpg";
for (var j=0;j<imgsrc.length;j++)
{
preload[j] = new Image;
preload[j].src = imgsrc[j];
}
function mode(param)
{
smode=param;
}
function startSlideshow()
{
if(smode=="play")
{
document.getElementById("play").disabled="disabled";
document.getElementById("pause").disabled="";
document.getElementById("stop").disabled="";
document.getElementById("slideshow").src=imgsrc[i];
i++;
setTimeout("startSlideshow()",1000);
}
else if(smode=="pause")
{
document.getElementById("pause").disabled="disabled";
document.getElementById("play").disabled="";
document.getElementById("play").value="Resume";
}
else if(smode=="stop")
{
document.getElementById("play").disabled="";
document.getElementById("play").value="Play";
document.getElementById("pause").disabled="disabled";
document.getElementById("stop").disabled="disabled";
document.getElementById("slideshow").src=imgsrc[0];
i=0;
}
if(i==imgsrc.length)
{
i=0;
}
}
</script>
</head>
<body>
<img id="slideshow" src="photos/photo1.jpg" />
<br />
<input id="play" type="button" value="Play" onclick="mode('play');startSlideshow();" />
<input id="pause" type="button" value="Pause" disabled="disabled" onclick="mode('pause');startSlideshow();" />
<input id="stop" type="button" value="Stop" disabled="disabled" onclick="mode('stop');startSlideshow();" />
</body>
</html>

See a demo of this Simple Javascript Slideshow. Download the Javascript Slideshow

Related posts:

web design category thumbnailJavascript Reload Image web design category thumbnailJavascript Validation – Textbox Combobox Radiobutton Checkbox web design category thumbnailJavascript Validation with Regular Expressions web design category thumbnailUsername availability check with AJAX and PHP jquery add remove textboxHow to Add/Remove text boxes using jQuery and PHP

Tags: html, javascript

Comments

  1. Sandi says

    May 6, 2013 at 6:58 am

    Great slideshow for us novices.
    Can you place a 3 second fade in somehow without to much trouble?
    Thank you!

    Reply
  2. Mwanri says

    June 16, 2014 at 10:18 am

    Hey, your codes are great, i appriciate….
    thanx alot

    Reply
  3. David Henry says

    August 11, 2015 at 5:49 pm

    Your code is wonderfully simple and clear, unlike so many other slideshow scripts. Is there any chance you could add “Next” and “Previous” arrows, buttons or links?
    Thanks,
    David

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Get a wealth of information delivered to your inbox. Subscribe and never miss a single article.

  • Tutorials and howtos
  • Code, scripts and commands
  • Online Tools

* No spam, unsubscribe anytime

Hire Me

  • SSL installation and hardening (A+ on Qualys SSL test)
  • Apache & Nginx configuration
  • Email deliverability improvement (10/10 on Mail Tester & MailGenius)
  • WordPress customization, optimization and migration
  • and much more…

    Tools

    • DNS Lookup Tool
    • htdigest Generator Tool Online
    • htpasswd Generator Tool Online
    • HTTP Headers Lookup Tool
    • MD5 Encryption Tool
    • Open Port Check Tool
    • SHA-1 Encryption Tool
    • URL Encoding/Decoding Tool

    Nav

    • Home
    • About Me
    • Contact Me
    • Privacy Policy
    • Sitemap
    Vultr SSD VPS

    Creative Commons License
    Jesin's Blog by Jesin A is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
    Based on a work at websistent.com.