View Full Version : Dynamically Resizing + Random Background Image

11-12-2010, 12:12 AM
Hi CodingForum!

For my website, I have a working script that changes the background image of my website every time it is reloaded. However, I would also like the image to dynamically resize as the user resizes the browser window.

here is a code snippet:

<script type="text/JavaScript">
/*<![CDATA[*/var el="bgImg";var bgimg=new Array("../00home/06home.jpg","../00home/07home.jpg");var random=Math.round((bgimg.length-1)*Math.random());var cssStr="#"+el+" { background: url("+bgimg[random]+") no-repeat center } ";var style=document.createElement("style");style.setAttribute("type","text/css");if(style.styleSheet){style.styleSheet.cssText=cssStr;}else{var cssText=document.createTextNode(cssStr);style.appendChild(cssText);}document.getElementsByTagName("head")[0].appendChild(style);/*]]>*/

<style type="text/css">
.rotatebg {
top:0; left:0;
width:1378px; height:1178px;
background-repeat: no-repeat;

<div class="rotatebg" id="bgImg" style="width:100%; height:100%" ></div>

Does anyone know how I can augment/alter this coded setup to enable the randomly loaded background image to proportionately fill the browser window? :o

11-12-2010, 01:05 AM

I hope this link is helpful.

11-12-2010, 01:24 AM
Thanks marilynn.fowler for the link.

I am searching for a more complex solution, however, that will allow me to maintain the proportion of the background image as the browser window is resized as well as adapting that code to work with a "random image selector" code.

Here is an example of a website that developed their own code to handle background image resizing: http://www.serialcut.com/ I took a look at their source code but the web developer(s) have a very complex coding structure that is beyond my level of application.

Please let me know if you/anyone can help.