PDA

View Full Version : adding an array of images



Vis Mech
06-08-2009, 09:41 PM
I am trying to fill out an array of random background images that float and are centered on a page. So far everything is in place and three images load in a random order. I haven't been able to resolve getting more than three images to wrok in this code. Looking at it too long has made everything a blur. Please help!!

here is the code:

<html>
<head>
<title>&bull; 49/51 &bull;</title>
<script type="text/javascript">
var backgr1="images/bg1.jpg"
var backgr2="images/bg2.jpg"
var backgr3="images/bg3.jpg"
var backgr4="images/bg4.jpg"
var backgr5="images/bg5.jpg"
var backgr6="images/bg6.jpg"
var backgr7="images/bg7.jpg"
var backgr8="images/bg8.jpg"
var backgr9="images/bg9.jpg"
var backgr10="images/bg10.jpg"
var backgr11="images/bg11.jpg"
var backgr12="images/bg12.jpg"
var backgr13="images/bg13.jpg"
var backgr14="images/bg14.jpg"
var backgr15="images/bg15.jpg"
var backgr16="images/bg16.jpg"

var cur=Math.round(6*Math.random())
if (cur<=1)
backgr=backgr1
else if (cur>=4)
backgr=backgr2
else
backgr=backgr3
document.write('<body background="'+backgr+'">')

var backgr = ['images/bg1.jpg','images/bg2.jpg','images/bg3.jpg'];
var cur = Math.round((backgr.length)*Math.random());
document.write('<body background="'+backgr[cur]+'">');
var backgrounds = ['images/bg1.jpg','images/bg2.jpg','images/bg3.jpg'],
bg,
cur = Math.round((backgrounds.length)*Math.random());
document.body.style.backgroundImage=backgrounds[cur];
document.body.style.backgroundPosition="top center";
document.body.style.backgroundRepeat="no-repeat";
</script>

</head>
<body>
</body>
</html>


If you need to view the page:

http://www.hardtimesmfg.com

forum_amnesiac
06-09-2009, 08:44 AM
This is a modification to your script but it does display a random background image, centred and not repeating, on the page.


<script>
function getbody(){
var backgr=["1.jpg", "2.jpg", "3.jpg", "4.jpg", "5.jpg", "6.jpg"];
arraylength=backgr.length;

cur=Math.round (arraylength * Math.random())-1;
if (cur<0){
cur=0;
}

document.write('<body background="'+backgr[cur]+'" style="background-repeat:no-repeat;background-position:center">');
}

</script>
</head>
<script>getbody()</script>
</body>