Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



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

Posts: 20
Name: Levi
Trades: 0
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 03:41 PM..
Levi_ is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 12-03-2007, 04:18 PM Re: Rotating Banner
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
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!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 12-03-2007, 07:28 PM Re: Rotating Banner
Average Talker

Posts: 20
Name: Levi
Trades: 0
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, 03:37 PM Re: Rotating Banner
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
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!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 12-06-2007, 04:21 PM
Harlequin's Avatar
Extreme Talker

Posts: 164
Name: Mick
Location: Tenerife
Trades: 0
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.
__________________

Please login or register to view this content. Registration is FREE

Death Once Had a Near Harlequin Experience...!

Last edited by chrishirst; 12-06-2007 at 04: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, 09:04 AM Re: Rotating Banner
Robert_Jr's Avatar
Super Talker

Posts: 115
Name: Robert Mancelita, Jr.
Trades: 0
Can you add the target url i the javascript? I'm planning to use this in my Site.
__________________
[
Please login or register to view this content. Registration is FREE
]
Robert_Jr is offline
Reply With Quote
View Public Profile
 
Old 01-19-2008, 06:53 AM Re: Rotating Banner
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
target URL?

do you mean the target attribute?
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Reply     « Reply to Rotating Banner
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB 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.29658 seconds with 12 queries