views:

983

answers:

4

i have a jquery script that allows for changing the body background

i want to have the background image of the body scaled to the browser window size there is a script called http://www.buildinternet.com/project/supersized/ but i am not able to make use of the scaling from that script because it effects the class img not the background-img

any ideas

<script type="text/javascript">
$(document).ready(function() { 
$("#BGSelector a").click(function() {
   var imgLink = $("img", this).attr("src");
   $.cookie("html_img", "" + imgLink + "", { path: '', expires: 7 });
   var imgCookieLink = $.cookie("html_img");       
   $("body").css("background", "url('" + imgCookieLink + "') no-repeat fixed center top #343837"); 

}); 
});
</script>

<script type="text/javascript">
$(document).ready(function() {   
       var imgCookieLink = $.cookie("html_img");  
       $("body").css("background", "url('" + imgCookieLink + "') no-repeat fixed center top #343837");

 });

</script>
A: 

Hi,

If I were you I would append the img into the DOM. Then set it to be absolutely positioned and set it's width according to client's width. Make sure everything else has a higher z-index and your img the lowest.

Ali
yeah i know that route, thats the one everyone is using, but no one has s solution for the background img, mainly because the background class is css does not have a size reallybut i really would want a solution that would resize the body background
vache
+1  A: 

You won't be able to do this cross-browser until the advanced background properties in CSS3 come more into play in the browser universe. The closest way is what Ali has already specified.

If it's only IE you care about then you could try their proprietary AlphaImageLoader CSS filter, e.g.

#myDiv { filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/workshop/graphics/earglobe.gif', sizingMethod='scale'); }

Please note though that this will play havoc when people try to click links with the filter loaded on a background - you won't be able to click the links until you implement some form of workaround (there's a few out there).

Jason Berry
thats ironic in a way, actually i do not care about IE at all, the whole site is full of transparent pngs, and other css formating that does not work on IE at all, i am going to straight out say i do not support IE, so it looks like i will have to do this with using another div, but i am sure there is a way of doing this without the div, i will not give up :) thanks for the suggestions jason
vache
A: 

There is another route you can go down which slightly goes against the separation of concerns ideology however it is feasible.

I know that there is an Images component assembly or alike assemblies exist which you can import into your project if your site is written in .net and almost certain (but don't have personal experience) that these assemblies must exist for any other platform you use for your back end.

So you can use of these to recompose the image according to the clients screen size and then either use back end code or js to set this image as a background image for an element. But as jason has mentioned doing this easy peasy would require you to wait for microsoft folks to find a commercial use in adopting the new css3 rules and then the rest of the market can also follow.

Ali
thanks for the info Ali, i really appreciate it, but the site is php, wordpress to be precise, and i really do not worry about IE or microsoft, i just do not support them when making websites, my reason is simple, i am making a website that provides a service, if the service requirers components that IE does not support than please use one of the more modern browsers such as safari firefox opera, users have no problem with using alternative browsers and web designers/programmers should support this trend
vache
A: 

Have you thought about using the canvas? Ok, I must admit barely know anything about using the canvas (I just started learning it) and my example below is probably done completely wrong, but it works (with FF & Chrome, not IE). It even resizes with the browser.

<canvas id="myCanvas" style="width:100%;height:100%;position:absolute;left:0px;top:0px;;z-index:-1;"></canvas>
<script type="text/javascript">
var elem = document.getElementById('myCanvas');
var context = elem.getContext('2d');
var w = document.body.clientWidth;
var h = document.body.clientHeight;
img = new Image();
img.src='bg.jpg';
img.onload = function () {
 context.drawImage(this,0,0,300,150);
};
</script>

Note the drawImage size must be 300x150 in this example as that is the default canvas size

fudgey
ahh i will try using canvas thanks for the idea, but yeah canvas is a html5 component so its not the most usable solution at the moment
vache
Oh, you can use explorercanvas to make this work with IE. I discovered though that the Supersized plugin and the canvas script above don't work for content that goes beyond the window... so if you scroll down, the background isn't fixed and scrolls up =(
fudgey
Opps, here is the link: http://code.google.com/p/explorercanvas/
fudgey