1

lets say i have an horizontal webiste ( http://toniweb.us/gm2/ ) and in a certain point i would like to know how much horizontal width is avaliable,

i made this picture so it's easier to understand

enter image description here

So i would need to get the EXTRA SPACE's width

and i am trying like this:

$(window).outerWidth(true)-($('header').outerWidth(true)+$('#nvl1').outerWidth(true)+$('#nvl2').outerWidth(true))

also tried

$(window).width()-($('header').outerWidth(true)+$('#nvl1').outerWidth(true)+$('#nvl2').outerWidth(true))

but in IE8 it's not returning the same value...

and then i need to apply it to the div, i made this function

function rellenarEspacio(){
    if(lte8){
        var w = $(window).width()-($('header').outerWidth(true)+$('#nvl1').outerWidth(true)+$('#nvl2').outerWidth(true))+$('#nvl2').outerWidth(true);
    }else{
        var w = $(window).width()-($('header').outerWidth(true)+$('#nvl1').outerWidth(true)+$('#nvl2').outerWidth(true));
    }

    //alert(w);
    $('.extra').css({'display':'block'});
    $('.extra').css({'width':w});
    return false
}

but is not using the whole space

any idea what I am missing?

1
  • 1
    you probably want to use the inner width of the body object $('body').width()
    – Dutchie432
    Commented Dec 30, 2011 at 9:15

1 Answer 1

6

Explanation

You're getting a negative value because... well because that's correct.

If you've got 3 divs that always take up X amount of space while your viewport is smaller than all the div widths combined, there is no space left. To best describe it, I've print screened my JSFiddle example: Showing some space
Here you can see there is plenty of space, 235px to be exact.
While the window's width is larger than the sum of all the div widths, there is still space left.
Both window and document show the same width at this point, so both are valid.


Showing no space
Here, there is no space left.
In your case, this would become 530 -(200 + 200 + 200) = -70) empty space.

Look what happens:
The window width is actually smaller than the document width. Why?
Well, because I resized the window to be that small, that's why.

The document width on the other hand is still the sum of all those div widths.

The reason for this, is because you have the size of the viewport, meaning, the size of your window, not the size of the document that contain all your elements.

So, to conclude: using this $(document).width() instead, should fix the problem.


Solution

Here is the example on how it works.
And here's my solution.

function adjustSpace(){
    var iHeaderW = $('header').outerWidth(),
        iNvl1W = $('#nvl1').outerWidth(),
        iNvl2W = $('#nvl2').outerWidth()
        iDocW = $(document).width();
    var iEmptyWidth = iDocW - (iHeaderW + iNvl1W + iNvl2W);

    console.log("Empty space: "+iEmptyWidth);        

    if(iEmptyWidth <= 0){
        $('.extra').width(0).hide();
        return false;
    }

    $('.extra').width(iEmptyWidth).show();
    return true
}

Additional information

Before you use the outerWidth() function, you should check out what the documentation has to say:

This method is not applicable to window and document objects; for these, use .width() instead.

The reason why IE8 return a different value than the other browsers, is because IE sucks. :)
No, but seriously, IE8 probably has a different box-sizing property and gives you a different result because of it, and you're using outerWidth(); which, as previously mentioned, is not the correct application for that function.

Check out this answer.

6
  • any idea why is it working once and not next? (please enter to the webiste and choose -> 'About Us', choose any section and toggle it) Commented Dec 30, 2011 at 12:31
  • ha! just fixed it removing the 'style' attribute at the beggining of the function ! thanks man! Commented Dec 30, 2011 at 12:34
  • Hi again, i didn't realise (in my screen) but it seems that in IE8 it won't apply the whole empty space with a bigger screen :S, any idea why would this happen? Commented Jan 9, 2012 at 10:00
  • @ToniMichelCaubet IE is much different from normal browsers. To put it bluntly, IE sucks. It handles and processes Javascript and CSS differently, and by differently, I mean moronically. It could be because javascript is executed in a poor manner, or there's something with the CSS. I don't have a good answer for it though. Commented Jan 9, 2012 at 17:09
  • well thanks for answering, was just asking in case you had a quick/intuitive thought. Salut! thx! Commented Jan 10, 2012 at 1:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.