Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Random Background image

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

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

Page: 1
Go to End
Author Topic: 1435 Views
  • mhull
    Avatar
    Community Member
    79 Posts

    Random Background image Link to this post

    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?

  • Sean
    Avatar
    Forum Moderator
    921 Posts

    Re: Random Background image Link to this post

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

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

  • mhull
    Avatar
    Community Member
    79 Posts

    Re: Random Background image Link to this post

    Many Thanks for your help.
    All working now.

    1435 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.