Building a Javascript Ad Rotator / Slideshow (with 123 buttons, play, pause and stop options)
August 22, 2008 by MK
Filed under CSS, Javascript
This script work in all major browsers - IE6, IE7, Firefox 3.0. You can even rotate images and flash objects as everything within DIV’s will be rotating.
Example of Banner Rotation using this script -
These are real banners rotating using this script. You can click on 1 2 3 buttons to see a particular banner or play / pause the rotation. Banners sequence will change randomly everytime you refresh the page
The idea is to pick up all the DIV’s within the main DIV container and rotate them randomly.
Here is an example of the Ad Rotator / Slideshow with some CSS thrown in and play/pause, 123 buttons. Please feel free to use the CSS and code. Leaving the reference back to my site (http://www.coolwebdeveloper.com) in the comments will be much appreciated though.
We will be using a set of open source scripts in addition to my own code to build this javascript adrotator/slideshow.
Please scroll to the bottom of the article to download the zipped files package that we are going to use for this project.
You will find a number of methods inside slideshow.js to control the slideshow. At the top level is a javascript class used to initiate the slideshow object from the HTML file.
Some of the options that can be set are - duration - sets the total duration of transitioning per image Following functions are used to control the behavior of the slide show - forward()
seconds_per_slide - sets the time per image/div is shown
pause_on_mouseover - whether the rotation will be paused when mouse over happens
onChange: function() {} - You can define here, any javascript code that you want to run when the image changes
backward()
jumpToSlide()
start()
pausePlay()
stop()
rndm()
transition()
Class can be initiated by using following code - You can pass any available number of property values when defining the rotation class
var slideshow = new MDSK.Slideshow(”slideMainDiv”, “.slideDiv”, {
seconds_per_slide: 5.0
});
The random() function is used to change the rotation pattern every tine the page is refreshed, as a result the images contained within the object are displayed randomly.
/**********************************/
rndm : function(min, max){
return Math.floor(Math.random() * (max - min + 1) + min);
},
/**********************************/
and then we will call this function every time the class object is instantiated
/**********************************/
this.slides.sort(function(a,b){
return me.rndm(-1,1);
});
/**********************************/
Please fill in the following information to receive the Javascript Adrotator code with fully working example via email and to subscribe to FREE Newsletter and email feed.
Your email address will also be used to provide any important updates and new versions for Adrotator script in near future.
All the scripts are pretty self explanatory but if you have any questions please dont hesitate to contact me by leaving a message in the comments section.
There is a lot of room for improvement in this code and lot of inline CSS can be moved to CSS file. Also, I am working on a wordpress adrotator plugin (enhancing this code) and will be releasing soon. So keep checking this site.
Related posts brought to you by Yet Another Related Posts Plugin.
















This is just what i needed. Lovely. Thanks a lot for share.
Don
I am having a problem in IE. I have 3 images in the rotation.
When the second image loads it has an error with the page and the rotation stops.
Any idea why?
Hello!
This script is great! I have been trying to figure out how to integrate it with wordpress but I just can’t.
I read on your post you were working on a wordpress plugin? How is that going?
Is it ready yet?
I am really looking forward to it!
So far I haven’t been able to find a wordpress plugin that displays ads on javascript, only after page refresh.
Anyway, I will be checking back on your site!
Great work!
Cheers
yup, Plugin has been ready for a while now and have been getting great reviews. You can visit this page for more info -
http://coolwebdeveloper.com/plugins/
Nice script, but I am finding that the ‘backward()’ function is not working. Any ideas?
This is a great rotator…working great on my site. However I would like it to have the ability to degrade when a user has scripting disabled in their browser by showing one of the three images in its place, prferably randomly with each refresh or loading of the page. Right now it just shows white space with the pause/play/1-2-3 controls.
Does anybody know what code I can use to do this?
How can I make this script degrade gracefully when the viewer have javascript disabled in their browser? I woul like to be able to show one of the banners in its place.
It’s working. It’s great. Thanks. You help me finishing my page.