Reply
Rotating Banner
Old 12-03-2007, 02:39 PM Rotating Banner
Average Talker

Posts: 20
Name: Levi
Hey, I'm trying to make my banner rotate between a selection of 4 total banners.

Code:
/* Banner Generator */
var    timeRefresh = setInterval("bannerChange()", 1000);    
                                                                        
  function bannerChange()
    {    
         var bannerImage = new Array();
                                                                    
             bannerImage[0] = "../images/banner1.png"
             bannerImage[1] = "../images/banner2.png"
             bannerImage[2] = "../images/banner3.png"
             bannerImage[3] = "../images/banner4.png"
                                                                    
                 var i = 0;
                                                        
    
                 for (i=0; i<=4; i++)
                      {
                         document.getElementById('banner').style.backgroundImage = "url(bannerImage[i])";
                       }
                 }
Currently it flashes quickly with the default background image for the #banner I created in the stylesheet. I call the function in the body onload tag so it is running and the firefox error console doesn't say anything. Anyone help?

Last edited by Levi_ : 12-03-2007 at 02:41 PM.
Levi_ is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
Old 12-03-2007, 03:18 PM Re: Rotating Banner
chrishirst's Avatar
Super Moderator

Posts: 13,632
Location: Blackpool. UK
Each time the function is called i is set to 0 then it loops through all the images.
You redefine the array on each call as well, very wasteful of client resources

It should increment the counter, check for it being greater that the array size then set to 0 if it is.
Then set the image from the array.

so
set the array and counter as global
increment the count on each timeout and change the image.
Code:
var    timeRefresh = setInterval("bannerChange()", 1000);    
var i = 0;
var bannerImage = new Array();
    bannerImage[0] = "../images/banner1.png"
    bannerImage[1] = "../images/banner2.png"
    bannerImage[2] = "../images/banner3.png"
    bannerImage[3] = "../images/banner4.png"
                                                                                                                                          
  function bannerChange() {    
  	i++
	if (i > bannerImage.Length) {
		i = 0
		}
	document.getElementById('banner').style.backgroundImage = "url(bannerImage[i])";
}
Not tested BTW looks OK though.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Indifference will be the downfall of mankind, but who cares?
Code Samples | People Counting System
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 12-03-2007, 06:28 PM Re: Rotating Banner
Average Talker

Posts: 20
Name: Levi
I just tried that out, and right now it flashs the banner that is declared in the css as the background and then reverts to the background color.
My paths for my images are correct, but i'm assuming that since it is going to the background color instead of image that it isn't getting the image location.
Levi_ is offline
Reply With Quote
View Public Profile
 
Old 12-06-2007, 02:37 PM Re: Rotating Banner
chrishirst's Avatar
Super Moderator

Posts: 13,632
Location: Blackpool. UK
it's probably unable to load the images fast enough, because they are called by javascript the images won't be downloaded and cached until required, you could use a preloader but I find using CSS and changing class names to be MUCH quicker.

Javascript & CSS Banner rotator


I'll be doing a write up at some point, thanks for the idea

HTML
HTML Code:
Jump To:-
<div id="banner" class="b1" onmouseover="stop();" onmouseout="start();" onclick="jump();"></div>

rest of code
</html>
<script type="text/javascript">
 start();
</script>
<!-- call to start the banner timer -->
CSS
Code:
#banner {
	height:82px;
	width:322px;
	background-repeat:no-repeat;
	background-position:center center;
	cursor:pointer;
}
.b0 {
	background-image: url(images/google_logo.gif);
}
.b1 {
	background-image: url(images/yahoo_logo.gif);
}
.b2 {
	background-image: url(images/msn_logo.gif);
}
.b3 {
	background-image: url(images/ask_logo.png);
}
Obviously size everything to suit your pages

javascript
Code:
var timeRefresh = 0;
var i = 0;
var bannerImage = 3;
var URL = new Array();
	URL[0] = "www.google.com";
	URL[1] = "www.yahoo.com";
	URL[2] = "www.msn.com";
	URL[3] = "www.ask.com";
	
	function jump() {
		window.location = "http://" + URL[i];
	}	
	function start() {
		timeRefresh = setInterval("bannerChange()", 1000);   
	} 
	function stop() {
		clearInterval(timeRefresh);   
	} 
                                                                                                                                          
  function bannerChange() {    
	  	i++
	if (i > bannerImage) {
		i = 0
		} else {
		}
	document.getElementById('banner').className = "b" + i;
}
Auto start on page load
Banner stops when the mouse is over the div & starts when mouse goes out and a mouse click will take you to the URL that matches the banner
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Indifference will be the downfall of mankind, but who cares?
Code Samples | People Counting System
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 12-06-2007, 03:21 PM
Harlequin's Avatar
Super Talker

Posts: 148
Name: Mick
Location: Tenerife
Levi

This may be another way of doing this but take a look at WDT (Link Below) and you'll see that the banner at the top and the one at the top right of the page change on each load.

[merged post]
Sorry, forgot to say if it's of any use you're welcome to the souce code.
__________________
Amazing Web Design ~ Website Design Tenerife
Death Once Had a Near Harlequin Experience...!
Are You An Ethical Coder...?

Last edited by chrishirst : 12-06-2007 at 03:33 PM. Reason: member added something in a second post
Harlequin is offline
Reply With Quote
View Public Profile Visit Harlequin's homepage!
 
Old 01-16-2008, 08:04 AM Re: Rotating Banner
Robert_Jr's Avatar
Super Talker

Posts: 116
Name: Robert Mancelita, Jr.
Can you add the target url i the javascript? I'm planning to use this in my Site.
Robert_Jr is offline
Reply With Quote
View Public Profile
 
Old 01-19-2008, 05:53 AM Re: Rotating Banner
chrishirst's Avatar
Super Moderator

Posts: 13,632
Location: Blackpool. UK
target URL?

do you mean the target attribute?
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Indifference will be the downfall of mankind, but who cares?
Code Samples | People Counting System
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Reply     « Reply to Rotating Banner
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.15521 seconds with 12 queries