Jump to content



Welcome to KnowledgeSutra - Dear Guest , Please Register here to get Your own website. - Ask a Question / Express Opinion / Reply w/o Sign-Up!
- - - - -

Javascript Slideshow Tutorial


31 replies to this topic

#1 andrewsmithy

    Newbie [Level 1]

  • Kontributors
  • Pip
  • 19 posts
  • Location:United States

Posted 19 March 2005 - 06:08 AM

JavaScript Slideshow Tutorial

I'm going to show you how to make a impressive JavaScript slideshow. First, you're probably asking: why would I want to make a slideshow in JavaScript? There are a number of reasons. First, you don't have to build a new HTML page for each picture. Secondly, the page will load much faster because the of the compactness of the page.
Ok let's get started with this example.

First we'll add a <script> tag in the <head> of our HTML document. In that script tag we will build the following:

      first = 1;
      last = 4;
      current = 1;
            
      function nextPicture() {
          // Hide current picture
          object = document.getElementById('slide' + current);
          object.style.display = 'none';
                
          // Show next picture, if last, loop back to front
          if (current == last) { current = 1; }
          else { current++ }
          object = document.getElementById('slide' + current);
          object.style.display = 'block';
       }

       function previousPicture() {
          // Hide current picture
          object = document.getElementById('slide' + current);
          object.style.display = 'none';
                
          if (current == first) { current = last; }
          else { current--; }
          object = document.getElementById('slide' + current);
          object.style.display = 'block';
      }

First, I want you to look at the variables. first describes the first picture id, which is 1; last defines the last picture, and current holds the index of the current picture. The function nextPicture() hides the currently displayed picture, and displays the next picture using CSS controls. The function previousPicture() is almost exactly the same as nextPicture() except that it travels back one picture. Notice that current variable holds the current picture index. We are going to make this page styled through CSS. Here is my CSS code. you can change this to whatever you want. Put this in the <style> tag of your <head> tag.

            .slideShow {
                background-color: #ebebeb;
                text-align: center;
                margin-bottom: 10px;
                padding: 5px;
            }
            .slides {
                position: relative;
                z-index: 1;
                display: none;
            }
            .setTitle, .slideTitle {
                font-family: "Franklin Gothic Book", Arial, Helvitica, sans-serif;
            }
            .setTitle {
                color: #995a01;
                font-size: 14px;
                font-weight: bold;
                }
            .slideTitle {
                color: #666666;
                font-size: 12px;
            }
            .controls {
                position: relative;
                z-index: 10;
            }
            #slide1 {
                display: block;
            }
            
            img {
                border: outset 1px #999999;
            }


Ok, now we are going to put our pictures in our page. We do this through standard HTML. I'll explain this part after we go over the code.

        <div class="slideShow">
            <div class="setTitle">Jaguars Track and Field Photos</div>
            
            <div id="slide1" class="slides">
                <div class="slideTitle">Picture 01</div>
                <img src="pic01.jpg" height="300" width="400" border="0" />
            </div>
            <div id="slide2" class="slides">
                <div class="slideTitle">Picture 02</div>
                <img src="pic02.jpg" height="300" width="400" border="0" />
            </div>
            <div class="controls">
                <a href="javascript:previousPicture()" style="margin: 10px;">« Previous</a>
                <a href="javascript:nextPicture()" style="margin: 10px;">Next »</a>
            </div>
        </div>

This code goes in the <body> tag. I just put two slides on here, but you can easily add more. Here is the format for adding more slides. You place another <div> inside of the <div class="slideShow"> like this:

<div id="slideShow">
...
<div id="slide10">
     <div class="slideTitle">Your Slide Title</div>
     <img src="pic10.jpg" height="600" width="430" border="0" />
</div>
....
</div>

Ok, when all of this is put together, you have a quite nice Javascript-enhanced slideshow! Here is the code for the whole page. Remember to edit the variable last to be the same as your last slide number.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <title>Slideshow</title>
        <script language="JavaScript" type="text/javascript">
            //<!--
            //<![CDATA[
            
            first = 1;
            last = 4;
            current = 1;
            
            function nextPicture() {
                // Hide current picture
                object = document.getElementById('slide' + current);
                object.style.display = 'none';
                
                // Show next picture, if last, loop back to front
                if (current == last) { current = 1; }
                else { current++ }
                object = document.getElementById('slide' + current);
                object.style.display = 'block';
            }

            function previousPicture() {
                // Hide current picture
                object = document.getElementById('slide' + current);
                object.style.display = 'none';
                
                if (current == first) { current = last; }
                else { current--; }
                object = document.getElementById('slide' + current);
                object.style.display = 'block';
            }
            //]]>
            // -->
        </script>
        <style type="text/css">
        <!--
            .slideShow {
                background-color: #ebebeb;
                text-align: center;
                margin-bottom: 10px;
                padding: 5px;
            }
            .slides {
                position: relative;
                z-index: 1;
                display: none;
            }
            .setTitle, .slideTitle {
                font-family: "Franklin Gothic Book", Arial, Helvitica, sans-serif;
            }
            .setTitle {
                color: #995a01;
                font-size: 14px;
                font-weight: bold;
                }
            .slideTitle {
                color: #666666;
                font-size: 12px;
            }
            .controls {
                position: relative;
                z-index: 10;
            }
            #slide1 {
                display: block;
            }
            
            img {
                border: outset 1px #999999;
            }
        -->
        </style>
    </head>
    <body>
        <div class="slideShow">
            <div class="setTitle">Your Title</div>
            
            <div id="slide1" class="slides">
                <div class="slideTitle">Picture 01</div>
                <img src="pic01.jpg" height="300" width="400" border="0" />
            </div>
            <div id="slide2" class="slides">
                <div class="slideTitle">Picture 02</div>
                <img src="pic02.jpg" height="300" width="400" border="0" />
            </div>
            <div id="slide3" class="slides">
                <div class="slideTitle">Picture 03</div>
                <img src="pic03.jpg" height="300" width="400" border="0" />
            </div>
            <div id="slide4" class="slides">
                <div class="slideTitle">Picture 04</div>
                <img src="pic04.jpg" height="300" width="400" border="0" />
            </div>
            <div class="controls">
                <a href="javascript:previousPicture()" style="margin: 10px;">« Previous</a>
                <a href="javascript:nextPicture()" style="margin: 10px;">Next »</a>
            </div>
        </div>
    </body>
</html>

There you go! If you have any problems, suggestions, or questions please reply to this post. If you like this code, please rate me! Thanks

#2 iGuest

    Hail Caesar!

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 5,876 posts
  • Interests:Trap17 Free Web Hosting, No Ads

Posted 29 December 2007 - 01:49 PM

adding backgroundimage slide show.
Javascript Slideshow Tutorial

What would be the coding of adding backgroundimage slide show in javascript?

-shiv

#3 karlosvalencia

    Newbie [Level 1]

  • Kontributors
  • Pip
  • 17 posts
  • Gender:Male
  • Location:Ottawa - Canada
  • myCENT:53.02

Posted 05 January 2008 - 05:06 AM

This is actually pretty cool. Tested it and works like a charm. Is there any way in java to protect the pictures so they cant be downloaded using right-click once they're displayed?

#4 games4u

    Newbie

  • Kontributors
  • Pip
  • 7 posts

Posted 30 April 2008 - 10:32 AM

View PostFeedBacker, on Dec 29 2007, 07:19 PM, said:

adding backgroundimage slide show.

Javascript Slideshow Tutorial
What would be the coding of adding backgroundimage slide show in javascript?

-shiv

The easiest way of background image slideshow is by assigning different backgrounds to different <div> tags.
So the following part of above code:
<div id="slideShow">
...
<div id="slide10">
Some Text Here...
</div>
....
</div>
can be changed to:
<div id="slideShow">
...
<div id="slide10" style="background-image:url(bg_image_10.bmp)">
Some Text Here...
</div>
....
</div>

The style attribute can be given for all <div> tags. And if needed the text in all <div> tags can be same - this creates a background changing effect.

I hope that solved your problem :lol:

Edited by games4u, 30 April 2008 - 10:36 AM.


#5 ultranet

    Newbie

  • Kontributors
  • Pip
  • 5 posts

Posted 26 September 2008 - 05:03 AM

Where I can get the source code of this example?

#6 agdurrette

    Newbie

  • Kontributors
  • Pip
  • 2 posts

Posted 29 October 2008 - 01:50 PM

Dooood Thats sooo cool love it thanks so much

how can i make it auto-play/play

thanks

#7 minimcmonkey

    Super Member

  • Kontributors
  • PipPipPipPipPipPipPipPipPip
  • 414 posts
  • Gender:Male
  • Location:United Kingdom
  • myCENT:62.89

Posted 04 November 2008 - 04:32 PM

Quote

This is actually pretty cool. Tested it and works like a charm. Is there any way in java to protect the pictures so they cant be downloaded using right-click once they're displayed?
No, there is absolutely no way to stop a persistent person from getting your images.
you can disable the context menu, and call alerts on right click etc, but there are plenty of other ways of getting the image.
I have seen people position transparencies over their images, so you download a blank image when you try and right click it to save it.
Unfortunately, the browser needs the URL of the image to show it, for which reason, there is no way of protecting it.

#8 asdftheking

    Member [Level 3]

  • Kontributors
  • PipPipPipPipPipPip
  • 90 posts
  • Gender:Male
  • Location:Peru
  • myCENT:42.21

Posted 16 November 2008 - 06:41 AM

View Postagdurrette, on Oct 29 2008, 08:50 AM, said:

Dooood Thats sooo cool love it thanks so much

how can i make it auto-play/play

thanks

As soon as I set this up the first thing I did was make it autoplay, it just makes sense. If you haven't already figured it out or given up, here's how it goes:

add two elements:

           function nextPicture() {
               // Hide current picture
               object = document.getElementById('slide' + current);
               object.style.display = 'none';
               
               // Show next picture, if last, loop back to front
               if (current == last) { current = 1; }
               else { current++ }
               object = document.getElementById('slide' + current);
               object.style.display = 'block';
               setTimeout(nextPicture, 2500);  //NEW LINE in the end of nextPicture function
           }                                              //a self repeating call at a delay of 2.5 secs
                                                           //adjust the seconds to your liking


(the line which needs to be added should be 24.)

then in line 79 change <body> to:
(it will be 78 if you haven't already made the first change)

   <body onload="setTimeout(nextPicture, 2500);"> 

I'm sure within a day or two I'll get bored with this and make the pictures fade in and out to black and/or white. I'll probably give a how to on that as well.
Let me know if this works for you!

Edited by asdftheking, 16 November 2008 - 06:42 AM.


#9 iGuest

    Hail Caesar!

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 5,876 posts
  • Interests:Trap17 Free Web Hosting, No Ads

Posted 22 April 2009 - 01:01 PM

Start and STOPJavascript Slideshow Tutorial

It is nice that the slide show will start playing when loaded but what if you want to stop it and use the forward and backwards buttons? How about a pause or stop button? Any ideas on code for that?

Thanks

-question by brian

 



#10 iGuest

    Hail Caesar!

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 5,876 posts
  • Interests:Trap17 Free Web Hosting, No Ads

Posted 28 April 2009 - 01:01 AM

Slide Show JavaScript with Text ControlsJavascript Slideshow Tutorial

I need a JavaScript slideshow class customizable for text controlsInstead of thumbnails. I know I have seen them before, but I can't seemTo find one now. Anybody know of a open-source script that would doSomething like this. The highlighted text and slide auto scrolls, butCan be selected onMouseOver and clicking navigates to a specified URL. Is this something you have already coded or would be willing to quote?

 

-question by Jim Hollomon

 



#11 iGuest

    Hail Caesar!

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 5,876 posts
  • Interests:Trap17 Free Web Hosting, No Ads

Posted 07 July 2009 - 02:09 PM

AwesomeJavascript Slideshow Tutorial

Thanks so much for the code it's awesome!/txtmngr/images/smileys/smiley1.Gif

It seems like theAuto-play function is only working for the first couple of pictures,And stops at pic2. Do I have my code in the wrong place?

Thanks,

Jen

http://whatatoon.Ser...g/slideshow.Php

#12 iGuest

    Hail Caesar!

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 5,876 posts
  • Interests:Trap17 Free Web Hosting, No Ads

Posted 14 July 2009 - 07:01 PM

BrilliantJavascript Slideshow Tutorial

Thank you so much-this is totally brilliant and thanks for the auto-play version.I don't really know Java so thanks for explaining it piece by piece and them putting it together like I said Brilliant!

-reply by lordofsarcasm

#13 iGuest

    Hail Caesar!

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 5,876 posts
  • Interests:Trap17 Free Web Hosting, No Ads

Posted 16 July 2009 - 11:16 PM

Javascript Slideshow TutorialJavascript Slideshow Tutorial

This works great and cool.

One thing I found that, when I check on the next button twice or thrice the speed of the next or back slide increases and increases it keeps increases the  keeps increasing.I have no control on the slide show.Is there a way to put pause or play button and how.

Thanks shan

-reply by Java learner



#14 iGuest

    Hail Caesar!

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 5,876 posts
  • Interests:Trap17 Free Web Hosting, No Ads

Posted 08 October 2009 - 03:27 PM

Multiple slideshows on one pageJavascript Slideshow Tutorial

First of all, thank you for doing this, it is extremely useful and straight forward, the best html java tutorial I've seen so far. Great Work. Is there anyway of including more Slideshows than one, though? When I try it, the second one browses the first one. And they have the same size?

Is there any solution to thisThank you again

-reply by Martin Samuelson



#15 iGuest

    Hail Caesar!

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 5,876 posts
  • Interests:Trap17 Free Web Hosting, No Ads

Posted 05 November 2009 - 02:16 PM

How to add a small icon photo spread Javascript Slideshow TutorialThis is great! I was curious, though, what it would take to add a miniature photo spreadsheet to one side of this slide show? I have several bunches of photos that I wanted to create a small spreadsheet of. When the user clicked one of the photos, an enlarged photo would appear to the right or left of this small spreadsheet. Is that possible?

#16 iGuest

    Hail Caesar!

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 5,876 posts
  • Interests:Trap17 Free Web Hosting, No Ads

Posted 28 December 2009 - 05:49 PM

how to add fading effect and auto running in this slideshowJavascript Slideshow Tutorial

Hi,

I liked this slideshow. But how to add fading images effect and auto run with next previous control in this slideshow. 



#17 akkers

    Newbie

  • Kontributors
  • Pip
  • 1 posts

Posted 30 December 2009 - 04:58 PM

View Postasdftheking, on Nov 16 2008, 06:41 AM, said:

As soon as I set this up the first thing I did was make it autoplay, it just makes sense. If you haven't already figured it out or given up, here's how it goes:

add two elements:

           function nextPicture() {
               // Hide current picture
               object = document.getElementById('slide' + current);
               object.style.display = 'none';
               
               // Show next picture, if last, loop back to front
               if (current == last) { current = 1; }
               else { current++ }
               object = document.getElementById('slide' + current);
               object.style.display = 'block';
               setTimeout(nextPicture, 2500);  //NEW LINE in the end of nextPicture function
           }                                              //a self repeating call at a delay of 2.5 secs
                                                           //adjust the seconds to your liking


(the line which needs to be added should be 24.)

then in line 79 change <body> to:
(it will be 78 if you haven't already made the first change)

   <body onload="setTimeout(nextPicture, 2500);"> 

I'm sure within a day or two I'll get bored with this and make the pictures fade in and out to black and/or white. I'll probably give a how to on that as well.
Let me know if this works for you!


Really good tutorial, But I cant quite figure out the code to make the picture fade out and in when the next button is selected. And i also wanted to add something like 1/4 so the user knows how many images their are in total,

i'm new to all this, does anyone have any ideas?

Thanks

Kam.

#18 iGuest

    Hail Caesar!

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 5,876 posts
  • Interests:Trap17 Free Web Hosting, No Ads

Posted 30 December 2009 - 07:54 PM

I keep just getting a list of pictures - no slideshowJavascript Slideshow Tutorial

Instead of seeing a slideshow, I keep coming up with a 10 pictures in a line going down the page.  What's going on?  I've checked the code and it all seems to be right..

Help?!?

-question by Kate

#19 iGuest

    Hail Caesar!

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 5,876 posts
  • Interests:Trap17 Free Web Hosting, No Ads

Posted 06 January 2010 - 09:21 PM

Fade-In/Fade OutJavascript Slideshow Tutorial

Great tutorial!

Would really like to see this have a couple of options:

Fade-In/Fade Out or Crossfade (or other transition -- of course, simple is the key!)

Thumbnailer (To provide a sort of filmstrip or contact sheet from which user can select)

Fit to screen or Fit Within Defined Area.: A way to "embed" this code into, say, a pre-sized region. The interface changes size and shape based upon the size of each picture. Would like to see, for example, the entire show (filmstrips and all) fit within a "window"  of say, 600x450. I could tuck it off to the side and still have other content flowing around it.

 ERROR:

However, I'm getting an error of it flipping radically from one image to the next if I click the Next/Previous button(s). It seems to throw the timing off and all hell breaks loose! Anyone else experience this? Is there a fix for it?

Again, excellent work!

-question by DanielNotDan

#20 iGuest

    Hail Caesar!

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 5,876 posts
  • Interests:Trap17 Free Web Hosting, No Ads

Posted 01 February 2010 - 02:13 AM

Sir, if I may call you sir, are a genius and I can't thank you enough for helping me solve my need for a slideshow... I sincerely hope that you have an awesome year!

Brett



#21 iGuest

    Hail Caesar!

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 5,876 posts
  • Interests:Trap17 Free Web Hosting, No Ads

Posted 11 February 2010 - 08:50 PM

Loop wonJavascript Slideshow Tutorial

I don't understand what I've done wrong but the loop function refuses to work in the nextpicture and lastpicture functions.

The code is identical to above but after the last slide it just draws a blank page and then hitting previous doesn't bring it back to the last slide and vice versa with going the other direction.

Been puzzling at it for hours and I can't see the problem which probably means its blindingly obvious. Any ideas?

 

 



#22 iGuest

    Hail Caesar!

  • Kontributors
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 5,876 posts
  • Interests:Trap17 Free Web Hosting, No Ads

Posted 25 February 2010 - 02:01 AM

change the imageJavascript Slideshow Tutorial

Hi,

 I need a javascript code to change the image from one image to another by mouse click

for example if a user click over the image the should change to next image.

 

can u pls help me??

 

-question by ausmaya

#23 Guest_RS_*

  • Guests

Posted 25 September 2010 - 03:23 AM

Can't thank you enough for your tutorial. I really appreciate the explanations. Thanks a million.

#24 Guest_Gaurav_*

  • Guests

Posted 06 November 2010 - 06:40 AM

Thanks for the code man.........i really appreciate it............ :blush:

#25 Guest_jacques_*

  • Guests

Posted 12 November 2010 - 09:44 AM

Hi!
Tanks for this scripts.
What about the automatic slideshow with description associate to each picture.




Reply to this topic


This post will need approval from a moderator before this post is shown.

  


2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users