PDA

View Full Version : Changing body background image rotator to div


Hesadanza
07-16-2009, 10:54 PM
I'd like to change the following code so that it rotates the background image of a particular div instead of the body.

<style>
body{
/*Remove below line to make bgimage NOT fixed*/
background-attachment:fixed;
background-repeat: no-repeat;
/*Use center center in place of 300 200 to center bg image*/
background-position: 300 200;
}
</style>

<script language="JavaScript1.2">
var bgimages=new Array()
bgimages[0]="img1.jpg"
bgimages[1]="img2.jpg"
bgimages[2]="img3.jpg"

//preload images
var pathToImg=new Array()
for (i=0;i<bgimages.length;i++){
pathToImg[i]=new Image()
pathToImg[i].src=bgimages[i]
}

var inc=-1

function bgSlide(){
if (inc<bgimages.length-1)
inc++
else
inc=0
document.body.background=pathToImg[inc].src
}

if (document.all||document.getElementById)
window.onload=new Function('setInterval("bgSlide()",3000)')

</script>

I've tried changing the document.body.background=pathToImg[inc].src to document.getElementById('ID').style.background=pathToImg[inc].src but that doesn't seem to work. Any ideas?

Amphiluke
07-17-2009, 08:45 AM
Actually one should set a background as follows:
document.getElementById('ID').style.background="url(" + pathToImg[inc].src + ")";

mioot
07-21-2009, 07:37 AM
Yes, you want to set the background url as the above. I checked your code and it working fine.