View Full Version : Changing background like MTV.com

Apr 27th, 2008, 10:12 PM

Please take a moment to visit http://mtv.com, go ahead and refresh the page a few times. You'll see what I am talking about, the background changes. I like to know how they did this.

I will be blunt, I have never coded a single line of DOM/Javascript in my life.

I only know html and css, and just starting to learn js--which is a little challenging for me. :)

I basically want the JavaScript to change the background div, and replace it with a random background image from a folder in the server per every page refresh.

I don't have any code to show you as I don't know where to begin to be honest. :o

Apr 28th, 2008, 02:44 AM

There should be plenty of scripts to choice from out on the net.


Apr 28th, 2008, 03:32 AM

Thanks for the reply, I have look through those.

They are close to what I want, a lot of the examples have the bg repeating over and over. What string do I need to add so it doesn't repeat? I want the background shown once, centered on the browser window.

For example this code have the background repeating, what do I need to add/change to have the bg shown only once and centered to the window?

<script type="text/javascript">

var randombgs=["../clip/backgr4.jpg", "../clip/backgr17.jpg", "../clip/backgr13.jpg"]

document.body.style.background='white url('+Math.floor(Math.random()*randombgs.length)]+')')

Apr 28th, 2008, 03:45 AM
nvm, I found the answer! thanks for reading....:)

Apr 28th, 2008, 01:23 PM
guessing you set the css to not repeat and you set the position to be centered.


Apr 30th, 2008, 02:56 AM
Thank you A1ien51! It works, but I still have to tweak it a bit more. *sign

I posted a new thread about it, http://www.codingforums.com/showthread.php?t=138696

I probably should had posted that inquiry on this thread rather starting a new one. :o