PDA

View Full Version : Random image


pavid
03-14-2010, 06:43 PM
Hi there

I have a query about random background image. But its not the background of the body, its quite an obscure set up. I have the image stretching horizontally and vertically, so the image is fixed in the body rather than being the bodys background.

This is my site - www.davidpullen.co.uk


Here is the html and css

<img src="indeximage.jpg" alt="David Pullen" id="bg" />

img#bg {
position:fixed;
top:0;
left:0;
width:100%;
height:100%;
}


I just want the image to be random, but i dont want to change the way i display the image because I know this works.

Any ideas? Is it possible in CSS or does it look like a javascript job?

cheers

Dave

gentleone
03-14-2010, 10:19 PM
You can't do it CSS only. You can include with PHP and the switch statement to parse different includes (if they are numbered). I've done it with random RSS feeds before. You have to wrap it in a div for this to work and make for every background image it's own PHP snippet/page called bg1.php, bg2.php, bg3.php, etc.


<div>
<?php $chosen = mt_rand(1,3);
switch($chosen){
case 1:
include 'bg1.php';
break;
case 2:
include 'bg2.php';
break;
case 3:
include 'bg3.php';
break;
default:
echo 'no background image available';
}?>
</div>