Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Archive

Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, biapar, Willr, Ingo, simon_w

Random Background image


Reply

3 Posts   1489 Views

Avatar
mhull

14 November 2008 at 10:37am Community Member, 79 Posts

I have this code making a random image:

public function RandomImage( $strLeftDirName )
{
if ( !is_string( $strLeftDirName ) )
{
return false;
}
$left_dir = DataObject::get_one("Folder", "Name = \"$strLeftDirName\"");
$randImg = ( $left_dir->ID ) ? DataObject::get_one("Image", "ParentID = $left_dir->ID", true, "RAND()" ) : false;
return $randImg->Filename;
}

But how would I make it a random background image on a div?
I want to be able to apply it to my header if possible?

Avatar
Sean

14 November 2008 at 2:56pm (Last edited: 14 November 2008 2:57pm), Forum Moderator, 921 Posts

Apply it via inline CSS on the element. e.g.:

<div id="header" style="background-image: url($randomHeaderImage)">
   <h1>My website</h1>
</div>

Avatar
mhull

14 November 2008 at 10:15pm Community Member, 79 Posts

Many Thanks for your help.
All working now.