/    Sign up×
Community /Pin to ProfileBookmark

Resize bg image according to browser size

Hi there,

After much searching, I have found two ways of auto-resing bg image, according to browser size.
One way uses Javascript, the second way uses Css.

However,
In the first, javascript way, the script is old and supports only IE.
The second CSS way, supports all browsers.

[B]My problem in both ways[/B] (except that the Javascript way supports only IE), is that i can not find a way to be able to change the background image like this:

[B][/B]
<img src=”newimage1.jpg” width=”100″ height=”100″ onclick=”changebg(this.src)”/>
<img src=”newimage2.jpg” width=”100″ height=”100″ onclick=”changebg(this.src)” />
[B][/B]

I write below both ways.
Can somebody help?

Thank you

——————– [B][U]Javascript Way[/U][/B] ——————–

<HEAD>
<SCRIPT LANGUAGE=”JavaScript1.2″>
<!–
NS4 = (document.layers);
IE4 = (document.all);

scaleWidth = true;
scaleHeight = true;
imSRC = “boat1.jpg”;

if (NS4) window.onload = setResize;

function setResize(){
setTimeout(“window.onresize=reDo;”,500);
}

function reDo(){
window.location.reload()
}

if (IE4) window.onresize = reDoIE;

function reDoIE(){
imBG.width = document.body.clientWidth;
imBG.height = document.body.clientHeight;
}

function makeIm() {

winWid = (NS4) ? innerWidth : document.body.clientWidth;
winHgt = (NS4) ? innerHeight : document.body.clientHeight;

imStr = “<DIV ID=elBGim”
+ ” STYLE=’position:absolute;left:0;top:0;z-index:-1′>”
+ “<IMG NAME=’imBG’ BORDER=0 SRC=” + imSRC;
if (scaleWidth) imStr += ” WIDTH=” + winWid;
if (scaleHeight) imStr += ” HEIGHT=” + winHgt;
imStr += “></DIV>”;

document.write(imStr);

}

//–>
</SCRIPT>
</HEAD>

<BODY MARGINHEIGHT=0 MARGINWIDTH=0>
<SCRIPT LANGUAGE=”JavaScript1.2″>
<!–
makeIm();
//–>
</SCRIPT>

<img src=”newimage1.jpg” width=”100″ height=”100″ onclick=”changebg(this.src)”/>
<img src=”newimage2.jpg” width=”100″ height=”100″ onclick=”changebg(this.src)” />
</body>
</html>


—————————————————————-

————————– [B][U]Css Way[/U][/B] ————————–

<head>
<title>Background to fit screen</title>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″>
<meta http-equiv=”Imagetoolbar” content=”no”>

<style type=”text/css”>
/* pushes the page to the full capacity of the viewing area */
html {height:100%;}
body {height:100%; margin:0; padding:0;}
/* prepares the background image to full capacity of the viewing area */
#bg {position:fixed; top:0; left:0; width:100%; height:100%;}
/* places the content ontop of the background image */
#content {position:relative; z-index:1;}
</style>
<!–[if IE 6]>
<style type=”text/css”>
/* some css fixes for IE browsers */
html {overflow-y:hidden;}
body {overflow-y:auto;}
#bg {position:absolute; z-index:-1;}
#content {position:static;}
</style>
<![endif]–>
<script language=”javascript” type=”text/javascript”>
function changebg(my)
{
document.getElementById(“bg”).style.backgroundImage =’url(‘+ my.src +’)’;

}

</script>
</head>

<body>
<div id=”bg”><img src=”tes_images/boat1.jpg” width=”100%” height=”100%” alt=””></div>

<div id=”content”>

<img src=”newimage1.jpg” width=”100″ height=”100″ onclick=”changebg(this.src)”/>
<img src=”newimage2.jpg” width=”100″ height=”100″ onclick=”changebg(this.src)” />

</div>
</body>
</html>


—————————————————————

Javascript Way source : [url]http://www.webreference.com/dhtml/diner/bgresize/[/url]
Css ways source : [url]http://www.htmlite.com/faq022.php[/url]

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@FangNov 19.2007 — Use absolute paths. See other changes to JavaScript and html:&lt;script type="text/javascript"&gt;
function changebg(my)
{
document.getElementById("bg").src =my;

}

&lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;img id="bg" src="http://mySite.com/tes_images/boat1.jpg" width="100%" height="100%" alt=""&gt;

&lt;div id="content"&gt;

&lt;img src="http://mySite.com/newimage1.jpg" width="100" height="100" onclick="changebg(this.src)"&gt;
&lt;img src="http://mySite.com/newimage2.jpg" width="100" height="100" onclick="changebg(this.src)"&gt;

&lt;/div&gt;
&lt;/body&gt;
×

Success!

Help @DimisD spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...