I want to add an image inside a div(#bg) using jquery to create an auto resizing webpage. So far I have coded like this but the auto resizing have some problems. If I manually add the image to div then everything works fine. I am a novice in jquery if you can elaborate your answers that will be great. Thanks in advance.
HTML
<div id="bg">
</div>`
CSS
#bg { position: fixed; top: 0; left: 0; }
.bgwidth { width: 100%; }
.bgheight { height: 100%; }`
jquery
$(window).load(function() {
$('#bg').prepend('<img src="Background.jpg" id="bg" alt="" />')
var theWindow = $(window),
$bg = $("#bg"),
aspectRatio = $bg.width() / $bg.height();
function resizeBg() {
if ( (theWindow.width() / theWindow.height()) < aspectRatio ) {
$bg
.removeClass()
.addClass('bgheight');
} else {
$bg
.removeClass()
.addClass('bgwidth');
}
}
theWindow.resize(resizeBg).trigger("resize");
});
I’ve written some days ago a similar function:
HTML
CSS
not really needed
JS