With the script presented in this page you can create a Simple Slideshow effect with any type of content in your web page: images, menu links, texts, etc. (without jQuery or other JavaScript framework).
This JavaScript Slideshow is simple and easy to customize, you can add Start, Stop, Previous, and Next buttons for slideshow, or to load the effect after the page loads.
• To Download the examples presented below, click: Simple JavaScript Slideshow Content.
- To see examples, click: Demo.
Instructions Slideshow Content script
To use this script in your web page, follow this steps:
1. In the place you want to show the slideshow, create a DIV with id="slshow", in this DIV add the images, menus, paragraphs, or other HTML elements for slideshow, with class="slshowc" to each element.
- The first element must have id="firstsls" because, initially all items with class="slshowc" are hiidden but the item with id="firstsls" is displayed.
Example of HTML code with three items for slideshow:
3. At the end of the HTML document, before the closing tag </body>, add this JavaScript script:
<script type="text/javascript"><!--
// Here sets the frequency for chenging elements (in milliseconds, 1000 = 1 sec.)
var frequency = 1300;
// object for slideshow effect
var slshow = new Object();
// Free JavaScript Course: https://coursesweb.net/javascript/
slshow.fun = ''; // store a string that indicate how the Slideshow to work
slshow.slshowc = []; // will contain all Tags in #slshow, with class="slshowc"
slshow.nrslshowc = 0; // the number of elements in slshowc Array
slshow.nslshowc = 0; // used to store the index of current "slshowc" item to show
// adds in "slshowc" property all Tags in #slshow, with class="slshowc"
slshow.setSlshowc = function() {
var slshowdivs = document.getElementById('slshow').children;
var nr_slshowdivs = slshowdivs.length;
var ic = 0;
for(var i=0; i<nr_slshowdivs; i++) {
if(slshowdivs[i].className == 'slshowc') {
this.slshowc[ic] = slshowdivs[i];
ic++;
}
}
this.nrslshowc = this.slshowc.length;
}
// define the index of the item to display, then shows it
slshow.sBackNext = function() {
if(this.fun == 'back' || this.fun == 'goback' || this.fun == 'next' || this.fun == 'gonext') {
if(this.fun == 'back' || this.fun == 'goback') {
this.nslshowc--;
if(this.nslshowc <0) this.nslshowc = this.nrslshowc - 1;
}
else if(this.fun == 'next' || this.fun == 'gonext') {
this.nslshowc++;
if(this.nslshowc >= this.nrslshowc) this.nslshowc = 0;
}
// hides all elements in "slshowc" property
// display the element in "slshowc" property with index from 'nimg'
// auto call this function if "fun" is 'goback' or 'gonext'
for(var i=0; i<this.nrslshowc; i++) {
this.slshowc[i].style.display = 'none';
}
this.slshowc[this.nslshowc].style.display = 'block';
if(this.fun == 'goback' || this.fun == 'gonext') setTimeout('slshow.sBackNext()', frequency);
}
}
// sets value of "fun" property, calls the function to set elements in "nrslshowc" (if not set)
// calls the sBackNext() to show the element according to "fun"
slshow.sShow = function(f) {
this.fun = f;
if(this.nrslshowc == 0) this.setSlshowc();
if(this.fun == 'back' || this.fun == 'goback' || this.fun == 'next' || this.fun == 'gonext') this.sBackNext(this.fun);
}
// Removes the three slashes "///" if you want the slideshow to auto starts after page loads
/// slshow.sShow('gonext');
// -->
</script>
- To change the interval of appearing the elements in slideshow, modify the value of the frequency variable, in milliseconds. Initially is set 1300 (1.3 seconds).
var frequency = 1300;
Setting the Slideshow controls
1. If you want the slideshow effect to auto start after the page loads, removes the three slashes "///" from this line of code, at the end in the JS script.
/// slshow.sShow('gonext');
2. To start the Slideshow, just calls the method: slshow.sShow('gonext').
- For exampe, this button will start the Slideshow when the user clicks on it:
4. To show the next element, just calls the method: slshow.sShow('next').
- For exampe, this button will show the next element when the user clicks on it:
6. To display continuously the next element (one by one), just calls the method: slshow.sShow('gonext').
- For exampe, this button will display the items one by one when the user has the mouse over button, and the Slideshow stops when the mouse cursor leaves the button:
<button onmouseover="slshow.sShow('gonext')" onmouseout="slshow.sShow('stop')">Go Next >></button>
7. To display continuously the previous element (one by one), just calls the method: slshow.sShow('goback').
- For exampe, this button will display the items one by one in reverse order when the user has the mouse over button, and the Slideshow stops when the mouse cursor leaves the button:
<button onmouseover="slshow.sShow('goback')" onmouseout="slshow.sShow('stop')"><< Go Back</button>
Demo
1. Slideshow with images, Title and Description to each image; Start, Stop, Back, and Next buttons.
- Code: