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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Template Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Is there a way to have a random background image loaded?


Go to End


12 Posts   4985 Views

Avatar
bummzack

Community Member, 904 Posts

3 February 2010 at 8:19pm

I think your path isn't correct.
Try something like this instead:

body { background: url(mysite/images/background-{$RandomBackgroundNumber}.jpg) 20% 20% no-repeat; } 

Just put the path to the images in there, relative to your website root.

Avatar
evsoul

Community Member, 36 Posts

4 February 2010 at 6:11am

As a test I did the full images directory root with this:

(this is what the URL looks like until I activate DNS)

		
<style type="text/css">
			body { background: url(http://sonic.net/~carrera/carrera-marble.com/themes/carreramarble/images/background-{$RandomBackgroundNumber}.jpg) 20% 20% no-repeat; }
</style> 

still does not display a background.. thoughts? or did I do that completely wrong?

Avatar
bummzack

Community Member, 904 Posts

4 February 2010 at 6:19am

Uhh. looks fine to me.
Although relative paths like I described should work just as well.

Please have a look at the (generated) source code of the Page and tell us how your css style looks there...

Avatar
evsoul

Community Member, 36 Posts

4 February 2010 at 6:23am

yeah it looks fine because it was HAHA.
I looked at the source code and it was calling to background-5.jpg
and I only have background-1 through 3.

How can I tell it I only have 3 backgrounds?

	function RandomBackgroundNumber() {
	return rand(1, 3);
	}

I did that because the original had a 6 there. But it still wants to look higher than 3.

Thank you SO much for the help here!

Go to Top