SUBSCRIBE - [ Tech News ] [ Make Money Blogging Tips ] [ Online Marketing Tips ] [ Web Dev News ]
Powered by MaxBlogPress  
CoolWebDeveloper.com CoolWebDeveloper.com
Menu
Search
Building a Javascript Ad Rotator / Slideshow (with 123 buttons, play, pause and stop options)

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
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

Following functions are used to control the behavior of the slide show -

forward()
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.

arrow73 Responses

  1. 56 mos, 3 wks ago

    This is an awesome script, i am downloading it right now and will be uploading on my site….this will rock the blogging world

  2. Carlo
    55 mos, 2 wks ago

    Great work! Is there a way to remove the play/pause and the numbers under the slideshow? Thanks!

  3. Carlo
    55 mos, 2 wks ago

    Oops! I found it already. Silly me. Thanks again!

  4. Daniel
    54 mos, 2 wks ago

    Great script! How do you disable the automatic rotation of divs? I want to be able to initiation the rotation by clicking the play button. Thanks!

  5. 54 mos, 2 wks ago

    Hello Daniel, Thanks for your comment.
    To disable automatic rotation you can comment or remove the call to start function this.start(); from the following class initialization function in slideshow.js file –

    Object.extend(MDSK.Slideshow.prototype, {

    and that will do the trick.

  6. Christopher
    54 mos, 1 wk ago

    Thanks for the great script.

    First off, please bear with me. I’m super new to this stuff. I’m amazed I’ve gotten this far. While the design elements are close, I’m not quite finished with them either.

    Here’s the thing: I’m having just a little problem with the slideshow images doing a little jumpy thing when they switch from one image to the next. Probably something to do with reloading the divs or something.

    Can you have a look at this and help me with my code?

    Thanks
    Christopher
    [email protected]

  7. Christopher
    54 mos, 1 wk ago

    Again… thanks for the script… I’m diggin’ it big time.

    I fixed the problem by adding an iframe in there rather than adding a directly to the main page and it seems to have done the trick. After a little tweaking and fumbling around with table sizes and targets, it’s working great.

    Thanks a ton. Works great.

    Please have a look anyway and tell me if you think I could optimize it some.

    CSK in CA

  8. Christopher
    54 mos, 1 wk ago

    That last post didn’t post properly because I used “<”.
    should read as:
    ” fixed the problem by adding an “iframe” that breers to another html page in there rather than adding a “div” directly to the main page and it seems to have done the trick.”
    Thanks

  9. 54 mos, 1 wk ago

    Hello Christopher, Thanks for your feedback and comments. I dont think you need to use an iframe. This should work without using an iframe. I will look into your page and will email you back, Thanks again.

  10. andy
    54 mos ago

    looks great, its exactly what i am after. thanks for sharing

  11. 54 mos ago

    Hello Christopher,

    I have checked your code. The slideshow is jumpy because your div’s height or width is not defined properly, that means an inner DIV has more height or width then parent DIV. And when code tries to show/hide then it hides some part of the DIV with improper height or width.
    This make content jump in IE7 and 6, should be okay in firefox though. Let me know if you still see issues.

  12. 53 mos, 3 wks ago

    Very cool info. thanks

  13. 53 mos, 3 wks ago

    I’m using two Javascript libraries that conflict with each other, one is this slideshow and the other is MooTools Accordion. Both use the variable Element, so is there a workable way in which to rename this scripts variable from Element to something else?

    I have tried using Dreamweaver’s ‘Replace All’ function to rename Element to Element1, and elements to elements1, and again to ElementOne/elementsOne, but to no avail.

    Have any advice?

  14. 53 mos, 2 wks ago

    this is a great plugin ..I am downloading it now , will mail you incase of any issue..thanks for your help

  15. Jeff
    52 mos, 3 wks ago

    Hello this is a great. I’m attempting, not very well i might add, to make it so that it can be used multiple times on a page. Do you know of a way to use it like that?

  16. Rich
    52 mos, 2 wks ago

    I’d like to set the order of the images and have them display in the same order every time the page is loaded/refreshed. How can you turn off the randomness?

  17. Rich
    52 mos, 2 wks ago

    One more question, how can I add additional images (say 5 total) to the rotation?

  18. 52 mos, 1 wk ago

    Hello Rich,

    To remove the randomness, comment or remove rndm() function in slideshow.js
    And to add more images, just add a nother slideshow div with the image inside and you will be good to go.

    Thanks,
    MK

  19. 52 mos, 1 wk ago

    Yeah, cool script indeed.
    I’ll put it in my blog header.
    Need to customized it first.
    Thanks for the script.

  20. Edgar
    51 mos, 4 wks ago

    Hi man!

    As everyone said.. thanks for posting this… I have made some upgrades to the css that will remove the need for spaces at the number’s sides and the gray line at the bottom.

    Let me know if you want it! Either way, thank you so much! :D It rocks!

  21. Edgar
    51 mos, 4 wks ago

    By the way, while going to Christopher’s page, http://virtualhde.com/iphone2 page, i got my computer inffected with Bloodhound.Exploit.196…

    thought you might want to know that…

  22. ben
    51 mos, 3 wks ago

    Very cool script. How can I change it to two images only? And also before and after instead of 1 & 2?

  23. ben
    51 mos, 3 wks ago

    how can i remove 3 since I have only two images?

  24. ben
    51 mos, 3 wks ago

    Hey, I got smarter all of a sudden and solved all my questions. Many thanks!

  25. 51 mos, 3 wks ago

    Hello Ben,

    Its good to know that you were able to solve the issue. Do let me know if you have any other issues, I will try to reply back ASAP.

    MK

  26. Carl
    51 mos, 3 wks ago

    It is a very cool script. I like it a lot. Thank you so much.

    I have a question about how to automaticly stop the slideshow after a given number of runs. How difficult to do it? Can you give me some ideas?

  27. 51 mos, 3 wks ago

    Thanks Edgar, I have since removed that link from his comments.

    MK

  28. Carl
    51 mos, 2 wks ago

    Never mind. I found my solution.

  29. 50 mos, 3 wks ago

    I confirmed my registration via the email link sent to me but have not recieved the download link for the script. Can you please resend it? Thanks

  30. 50 mos, 3 wks ago

    Hello Jeremy,

    Code was resent to both of your email addresses in our system. euclidlibrary.org domain was blocking the zipped attachment. So please check your other email.

    Subject of the email – Javascript Ad Rotator Code – Coolwebdeveloper.com
    Please make sure you check your SPAM folder too just incase.

    Thanks for visting coolwebdeveloper.com and do let us know if you still havnt received it and we will upload it to our server for you to download and we will send the ldownload link to you.

  31. Jen
    50 mos, 1 wk ago

    Awesome script, thanks!

    Question – the “pause on mouseover” doesn’t work for me – I’ve set the “pause_on_mouseover” flag to “true”… is there anything else I need to do to enable this?

    Thanks!

  32. Jen
    50 mos, 1 wk ago

    Actually – I got the pause on mouseover working!

    Is there any way to toggle the button to be “play” when “pause_on_mouseover” and then switch back to showing “pause” on mouseout? I tried adding the “toggle” JS function, but this threw me an error.

    Thanks!

  33. 50 mos, 1 wk ago

    Hello Jen, Thanks for using this script and your comments. Are you trying to implement mouseover on the rotating DIV’s or on the 123 buttons?
    If you are using mouseover on 123 buttons then you can call toggle() on mouseover on the anchor tag. And then call toggle() again on mouseout while changing the value of togle accordingly. Do let em know if you still need help, Thanks.

  34. vaibhav
    50 mos ago

    Hey its really cool and very easy to use ..
    thank you very much

  35. vaibhav
    49 mos, 3 wks ago

    hey.

    I have a problem with FF. I have 6 images & I wanted the images flow in sequence 1-6 so i made following changes in Slideshow.js

    this.slides.sort(function(a,b){
    return (0);

    and commented the sndm(); function.

    it works fine in IE & safari but images not flows in sequence in FF. it picks randomly. any solution?

  36. 49 mos, 2 wks ago

    One solution is to stop calling the rndm() function totally. You can comment this function where its been called.

  37. 48 mos, 2 wks ago

    What a great script! Thanks for the code.

    One problem I am having is that when I increase the size of the div holding the item numbers on the bottom, only the area immediately to the left & right of the text and thin border to the right is clickable. The area under the text is not clickable. I would like for the whole div to be clickable if possible.

    I added an onclick to the divs vs. the href which works for about 2 seconds, and then it goes away. At that point, the area under the text is not clickable again. When it changes to the next number, it is once again clickable for about 2 seconds and the problem keeps repeating. Any clues how I can fix this?

  38. 48 mos, 2 wks ago

    Hello Ron, Try using lists (<li>) instead of DIV’s and that should fix the issue.

  39. Deb
    48 mos, 1 wk ago

    Looks Great but it appears that download is not working. I confirmed my registration via the email link sent to me but have not recieved the download link for the script.

    Thanks

  40. 48 mos, 1 wk ago

    You should get an email with attachment. Please check your spam folder. If you still did not receive it, do let us know or email us – info [at] coolwebdeveloper.com [dot] com and we will attach it for you, Thanks.

  41. Amber
    48 mos ago

    is there a way to have one or more of the images be able to have a link?

  42. Amber
    48 mos ago

    is there a way to adjust the size of the box the images rotate in..

  43. 48 mos ago

    yes, you need to change the size of the div containing the images. And increase the size of parent div. That is it.

  44. 48 mos ago

    yes, just put an anchor tag around the images and you are good to go.

  45. Vanessa
    47 mos, 2 wks ago

    I want to eliminate the randomness of the images. I’ve already commented out

    this.slides.sort(function(a,b){
    return me.rndm(-1,1);
    });

    AND

    rndm : function(min, max){
    return Math.floor(Math.random() * (max – min + 1) + min);
    },

    But in Firefox, it is still rotating randomly. What else do I have to do to get it to eliminate the randomness in Firefox? Thanks!

  46. 47 mos, 1 wk ago

    Hi -

    I added 2 images to the rotation but now the play/pause and image buttons don’t work. In other words, the images rotate, but I can’t pause or jump with the buttons.

    I don’t know javascript… at all. :)

    Could you please tell me what I need to edit to make the buttons work again.

    Thanks.

  47. 45 mos, 2 wks ago

    What other changes do I need to make in order to get a .swf file to run? 1.swf is a short Flash movie.

    I have resized the space the .swf will run in but…. No luck.

    What am I missing?

    Thanks.

    ofb

  48. bjsg
    43 mos, 4 wks ago

    greetings of peace…

    i just want to know how to add text in between of pause/play and the pictures?

    thanks in advance…

  49. 43 mos, 2 wks ago

    Hi!
    I am having one problem on my site with this feature. For some reason when the slides transition, the new slide comes in under the previous one and shifts the whole page down until the previous image drops off. Any ideas what might be causing this?

    Daiv
    PS. I really appreciate you offering this code. It is 100% exactly what I was looking for and works well.

  50. 43 mos, 2 wks ago

    Just posted a help question, but figured it out by reading other comments above.

    All is well and your script is fantastic!

    Daiv

  51. This is just what i needed. Lovely. Thanks a lot for share.

    Don

  52. Tfudge
    42 mos ago

    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?

  53. 41 mos, 2 wks ago

    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

  54. 41 mos, 2 wks ago

    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/

  55. Craig
    41 mos ago

    Nice script, but I am finding that the ‘backward()’ function is not working. Any ideas?

  56. Vince
    39 mos, 3 wks ago

    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?

  57. Vince
    39 mos, 3 wks ago

    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.

  58. Mircea
    38 mos ago

    It’s working. It’s great. Thanks. You help me finishing my page.

  59. Andy
    33 mos ago

    Hi MK

    I have added a forward button which works but I am getting errors with the back button using the code and error code below.

    Andy

    back

    Webpage error details

    Message: ‘null’ is null or not an object
    Line: 51
    Char: 2
    Code: 0
    URI: file:///G:/Applications/javascript_rotator/javascript_rotator/javascripts/slideshow.js

  60. 29 mos, 2 wks ago

    how can the images rotated in 123 order without random

  61. sapna
    27 mos, 3 wks ago

    I wanted to display two images in place of one image and wanted to redirect to two different url. Will that be possible?

  62. conan
    26 mos, 3 wks ago

    It works, by the way, if i want to add more slides, how can i update it?

  63. makiko
    24 mos, 2 wks ago

    This is awesome! This is exactly what I was looking for. At the end, I decided to go without the random function by just taking out “Math.random()”. It works great! Thank you!!

  64. CHR
    24 mos, 1 wk ago

    Was wondering if the IE issue with displaying 2 issues then freezing has been addressed? I can get the script to display multiple images in ff, opera, safari and chrome but it freezes after two images in IE. Any help would be greatly apreciated. Thanks

  65. bhussein
    22 mos, 3 wks ago

    Doesn’t work for ie 9, any ideas?

  66. Bahaa
    22 mos, 3 wks ago

    Do you have an update or fix for internet explorer 9? Thanks.

  67. Bahaa
    22 mos, 3 wks ago

    Any update to make it work with internet explorer 9? Thanks.

  68. paulw
    18 mos, 4 wks ago

    Hi,

    Nice script, but I can’t seem to add more than 3 images? I added more slideDiv elements with new images and added corresponding divs for buttons but it will only show the first 3?

  69. paulw
    18 mos, 4 wks ago

    Forget the last message, I had an error in the HTML! Can’t seem to get the pause_on_mouseover working? Set it to true, but nothing happens?

  70. 18 mos ago

    Is there a way to use an image for the “current slide” instead of just highlighting the bg color?

    Thanks!

  71. MahaLingam.S
    17 mos, 2 wks ago

    I have added a Time intervel Text box which not works I am getting errors with the chck button using for paly random the code and error code below.
    so kindly send me some other…….

  72. 13 mos, 3 wks ago

    Hello, just wondering is there an update for your script for IE 9. I have a banner slideshow on my home which was working fine on windows xp/IE 8, and just rolled in the new monster windows 7 machine in my office and boom no banner. Please help.

  73. 7 mos, 2 wks ago

    Peculiаr articlе, exactly what I needed.

Leave A Comment

CommentLuv badge