The CSS code:
#bgimgwrapper {
position: fixed;
top: 0;
left: 0;
bottom: 0;
width: 100%;
height: 100%;
z-index: -1;
overflow: hidden;
}
#bgimg {
-ms-interpolation-mode: bicubic;
display: block;
}
The HTML code:
<div id="bgimgwrapper">
<img alt="" src="photo.jpg" id="bgimg" style="width: 100%;" />
</div>
And the jQuery that glues it all together:
(function($){
var pr = 0, bgWidth = true, bgResize;
bgResize = function(){
var wr = $(window).width() / $(window).height();
if(wr > pr && !bgWidth) {
$('#bgimg').css('width', '100%').css('height', '');
bgWidth = true;
} else if(wr < pr && bgWidth) {
$('#bgimg').css('height', '100%').css('width', '');
bgWidth = false;
}
};
$(document).ready(function() {
var img = $('#bgimg');
if(img.height()) {
pr = $('#bgimg').width() / $('#bgimg').height();
bgResize();
} else {
img.load(function(){
pr = $('#bgimg').width() / $('#bgimg').height();
bgResize();
});
}
$(window).resize(bgResize);
});
})(jQuery);