Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I automatically swap background image? 2

Status
Not open for further replies.

southbeach

Programmer
Jan 22, 2008
879
US
Here is the site I am working on


I have a number of images I'll like to alternate on the background.

The one thing that comes to mind is the use of onload() and setting up a routine that runs as a loop and randomly change the background image ...

The problem is that while I have the concept of how it could be done, I am lost on getting started with it.

Question:
If I simply use onload="autoswap();"; what should autoswap() look like to run as a loop and swap image?

Thank you all in advance!



--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
Hi

Supposing your background candidates are numbered from 0 to imagecount-1, ( as 'back0.png' to 'back24.png', ) where variable imagecount is set to the number of images, this could be an approach :
JavaScript:
[b]function[/b] [COLOR=darkgoldenrod]autoswap[/color][teal]()[/teal]
[teal]{[/teal]
  [b]var[/b] randomurl[teal]=[/teal][green][i]'/imgdir/back'[/i][/green][teal]+[/teal]Math[teal].[/teal][COLOR=darkgoldenrod]trunc[/color][teal]([/teal]Math[teal].[/teal][COLOR=darkgoldenrod]random[/color][teal]()*[/teal]imagecount[teal])+[/teal][green][i]'.png'[/i][/green]
  document[teal].[/teal][COLOR=darkgoldenrod]getElementById[/color][teal]([/teal][green][i]'fixedimg'[/i][/green][teal]).[/teal]style[teal].[/teal]backgroundImage[teal]=[/teal][green][i]'url('[/i][/green][teal]+[/teal]randomurl[teal]+[/teal][green][i]')'[/i][/green]
  window[teal].[/teal][COLOR=darkgoldenrod]setTimeout[/color][teal]([/teal]autoswap[teal],[/teal][purple]30[/purple][teal]*[/teal][purple]1000[/purple][teal]);[/teal]
[teal]}[/teal]

Feherke.
 
feherke,

I am getting the error
Code:
Match.trunc is not a function

Here is the snippet of code I am using
Code:
<script type="text/javascript">
	var imagecount = 2;
	function autoswap()
	{
	  var randomurl='images/fixedimage'+Math.trunc(Math.random()*imagecount)+'.jpg'
	  document.getElementById('fixedimg').style.backgroundImage='url('+randomurl+')'
	  window.setTimeout(autoswap,30*1000);
	}

</script>

Using FF 3.5.3

Thanks!



--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
That's because there is no 'trunc' method natively built in to the JS math object.

Perhaps feherke has added one to his, or perhaps he meant to use 'Math.floor' or 'Math.ceil' instead?

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Hi

Dan said:
or perhaps he meant to use 'Math.floor' or 'Math.ceil' instead?
You are right, I intended to use [tt]Math.floor()[/tt]. I typed that by reflex, but I realized my mistake before FireBug displayed the error message. Then I corrected it. Then I copy & pasted the wrong code here. :-( Sorry.

Feherke.
 
I must admit that when I first started learning JS, I was surprised at the lack of "Math.trunc", as I was so used to the trunc command in Turbo Pascal returning the int portion of a float.

That was before I learned about parseInt, though :)



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Hi

[tt][small][blue][ignore][off-topic][/ignore][/blue][/small][/tt]
Lol. My attachment to [tt]Trunc[/tt] also comes from Turbo-Pascal.
( But its absence in JavaScript was not a surprise for me. The surprise happened years before when learned FoxPro. )
[tt][small][blue][ignore][/off-topic][/ignore][/blue][/small][/tt]

Feherke.
 
Nice!

My background is now swapping every X seconds and I am happy :)

Thank you so very much to you all!


--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top