像這樣
var srcs = {"images/pic1.jpg","images/pic2.jpg","images/pic3.jpg"};
var index= Math.floor (Math.random() * 3);
imagethingy.src = srcs[index];
document.body.style.backgroundImage = "url(" + imagethingy.src = srcs[index]; + ")";
或
<html>
<head>
<script type="text/javascript">
function randomize(min, max) {
if (!min)
min = 0;
if (!max)
max = 1;
return Math.floor(Math.random()*(max+1)+min);
}
function randomBg() {
var bgs = new Array();
// I took these images from a site with a similar script already only to get example images
// I wrote this script myself
bgs.push("http://javascript.internet.com/img/backgrounds/wood.gif");
bgs.push("http://javascript.internet.com/img/backgrounds/paper.gif");
bgs.push("http://javascript.internet.com/img/backgrounds/clouds.gif");
bgs.push("http://javascript.internet.com/img/backgrounds/faces.gif");
bgs.push("http://javascript.internet.com/img/backgrounds/marble.gif");
document.body.style.backgroundImage = "url(" + bgs[randomize(0, bgs.length-1)] + ")";
}
</script>
</head>
<body onLoad="randomBg()" style="text-align:center;">
<div>some text</div>
</body>
</html>
如果你還沒有,你需要一些CSS來使圖像全屏顯示。 – starbeamrainbowlabs 2012-08-12 09:16:40