I need a div getting the height of its dynamic background-image after a specific window width and specific height of the div.
So when the div (.header-block-wrapper) is >= 630px and the window width >= 978px, the div needs to be as heigh as its background-image is.
The code script for detecting the background-image height i got here: How do I get background image size in jQuery?
The whole script also has to fire on resize that is why i used ( $(window).resize...).
But it is not working... any idea?
$(window).load(function() {
$(function(){
var innerHeight = $('.header-block-wrapper').height();
var innerWidth = $('.header-block-wrapper').width();
var backgroundHeight;
$(image).load(function () {
backgroundHeight = image.height;
});
image.src = $('.header-block-wrapper').css('background-image').replace(/url\(|\)$/ig, "");
$(window).resize(function(){
if( innerHeight >= 630px && innerWidth >= 978px) {
$('.header-block-wrapper').css("height",backgroundHeight);
}else {
$('.header-block-wrapper').css("height","auto");
}
});
});
});
EDIT// I fixed it:
Alright i fixed it, instead of using an background-image, is used an image within the div as background. This image has an 100% width, auto height and absolute.
Its just easier to get the height of an image then a background-image.
jQuery(window).load(function() {
jQuery(window).resize(function(){
var innerHeight = jQuery('.header-block-wrapper').height();
var innerWidth = jQuery('.header-block-wrapper').width();
var imageHeight = jQuery('.header-block-image').height();
if( innerHeight >= 630 && innerWidth >= 978) {
jQuery('.header-block-wrapper').css("height",imageHeight);
}else {
jQuery('.header-block-wrapper').css("height","auto");
}
});
});