11

The Background:

I tried to solve the StackOverflow question yet another HTML/CSS layout challenge - full height sidebar with sticky footer on my own using jQuery. Because the sidebar in my case may be longer than the main content it matches the case of comment 8128008. That makes it impossible to have a sidebar longer than the main content and having a sticky footer without getting problems when shrinking the browser window.

The status quo:

I have a html page with a div, which is automatically stretched to fill the screen. So if there is empty space below the element, I stretch it downwards:

The div is stretched to fill the screen

But if the browser viewport is smaller than the div itself, no stretching is done but the scrollbar shows up:

Small viewport: no resizing

I've attached jQuery to the window's resize event to resize the div, if the browser window is not to small and remove any resizing in the other case. This is done by checking if the viewport is higher or smaller than the document. If the viewport is smaller than the document, it seems like the content is larger than the browser window, why no resizing is done; in the other case we resize the div to fill the page.

if ($(document).height() > $(window).height()) {
    // Scrolling needed, page content extends browser window
    // --> No need to resize the div
    // --> Custom height is removed
    // [...]
} else {
    // Window is larger than the page content
    // --> Div is resized using jQuery:
    $('#div').height($(window).height());
}

The Problem:

Up to now, everything runs well. But if I shrink the browser window, there are cases, where the div should be resized but the document is larger than the window's height, why my script assumes, that no resizing is needed and the div's resizing is removed.

The point is actually, that if I check the document's height using Firebug after the bug appeared, the height has just the value is was meant to have. So I thought, the document's height is set with a little delay. I tried to run the resize code delayed a bit but it did not help.

I have set up a demonstration on jsFiddle. Just shrink the browser window slowly and you'll see the div "flickering". Also you can watch the console.log() output and you will notice, that in the case of "flickering" the document's height and the window's height are different instead of being equal.

I've noticed this behavior in Firefox 7, IE 9, Chrome 10 and Safari 5.1. Can you confirm it?

Do you know if there is a fix? Or is the approach totally wrong? Please help me.

Community
  • 1
  • 1
msiemens
  • 2,223
  • 1
  • 28
  • 38
  • 2
    Very well formatted question. +1 – Akos Oct 16 '11 at 16:39
  • A little off-topic, but you're passing `jQuery` as an argument to your self-invoking function, whilst that function does not accept any arguments. – pimvdb Oct 16 '11 at 16:41
  • 3
    I played around a little, is this anything close to what you want? http://jsfiddle.net/2yKgQ/25/ – Esailija Oct 16 '11 at 16:48
  • @Esailija -- i might be wrong, but wouldn't the use of timeouts here be a detriment to page performance? On every resize there's a timeout? Although it works, it's a kind of like using a hand grenade to kill a mosquito. Check out my answer below, there's a much simpler solution (perhaps) – tmsimont Oct 16 '11 at 17:06
  • It should increase performance as the browser doesn't need to calculate overflow after every single resize but only after every resize session (the resize session is 150 milliseconds long). And there is only one timeout active at a time, I am not 100% sure but it's the amount of concurrent timers active that degrade performance, not resetting one timer? jQuery launches a new timeout internally every 13 milliseconds after all :P – Esailija Oct 16 '11 at 17:27
  • i always thought timeouts were bad news. i see wat u are doing... cool. 1 downside, however, once that screen is too small and overflow is needed, it's a little twitchy, because it won't show that scrollbar until the page has been settled a second or so. but, your method might actually be better for performance. hard to say tho, would take some measuring to decide. – tmsimont Oct 16 '11 at 17:36
  • pure css would definately be better than js hacks but 100% height most likely doesn't work if the `body` element's height isn't exactly equal to window height. – Esailija Oct 16 '11 at 17:47
  • I can confirm the bug described by @tmsimont. I think I'll do an performance test - but your solution works and that is the main thing. – msiemens Oct 16 '11 at 19:58
  • @Esailija: Of course a JS solution is only the last choice to consider but all my attempts on solving the problem using pure CSS failed - and in my case an JS solution is bearable. – msiemens Oct 16 '11 at 19:58

3 Answers3

6

Ok -- wiping my old answer and replacing...

Here's your problem:

You are taking and comparing window and document height, without first taking into consideration the order of events here..

  1. Window loads
  2. Div grows to window height
  3. Window shrinks
  4. Document height remains at div height
  5. Window height is less than div height

At this point, the previously set height of the div is keeping document height greater than the window height, and this logic is misinterpreted: "Scrolling needed, no need to extend the sidebar" fires, erroneously

Hence the twitch.

To prevent it, just resize your div along with the window before making the comparison:

(function () {
    var resizeContentWrapper = function () {
            console.group('resizing');


            var target = {
                content: $('#resizeme')
            };

            //resize target content to window size, assuming that last time around it was set to document height, and might be pushing document height beyond window after resize
        //TODO: for performance, insert flags to only do this if the window is shrinking, and the div has already been resized
            target.content.css('height', $(window).height());

            var height = {
                document: $(document).height(),
                window: $(window).height()
            };

            console.log('height: ', height);


            if (height.document > height.window) {
                // Scrolling needed, no need to externd the sidebar
                target.content.css('height', '');

                console.info('custom height removed');
            } else {
                // Set the new content height
                height['content'] = height.window;
                target.content.css('height', height['content']);

                console.log('new height: ', height);
            }
            console.groupEnd();
        }
    resizeContentWrapper();
    $(window).bind('resize orientationchange', resizeContentWrapper);
})(jQuery);

Per pmvdb's comment, i renamed your $$ to "target"

tmsimont
  • 2,651
  • 2
  • 25
  • 36
  • Works just the way it should. Thank you! :) Somehow I didn't consider the order of events you described. – msiemens Oct 16 '11 at 19:48
1
$(window).bind('resize',function(){
    $("#resizeme").css("height","");

    if($("#resizeme").outerHeight() < $(window).height()){
       $("#resizeme").height($(window).height());
       $("body").css("overflow-y","hidden");  
    }else{
       $("body").css("overflow-y","scroll");  
    }           
});
MartyIX
  • 27,828
  • 29
  • 136
  • 207
Marknl
  • 182
  • 1
  • 9
0

Maybe I am misunderstanding the problem, but why are you using Javascript? This seems like a layout (CSS) issue. My solution without JS: http://jsfiddle.net/2yKgQ/27/

newbie
  • 1
  • That's why I linked to [yet another HTML/CSS layout challenge - full height sidebar with sticky footer](http://stackoverflow.com/questions/6837603/yet-another-html-css-layout-challenge-full-height-sidebar-with-sticky-footer). As mentioned in [comment 8128008](http://stackoverflow.com/questions/6837603/yet-another-html-css-layout-challenge-full-height-sidebar-with-sticky-footer#comment-8128008) the bug caused by a css solution (see [jsfiddle.net/2ZhpH/62/](http://jsfiddle.net/2ZhpH/62/) and shrink the browser window) cannot be solved - without using Javascript. – msiemens Oct 16 '11 at 19:29
  • IE6+ doesn't let you do it in pure css which is probably why the javsscript – Sinaesthetic Apr 18 '12 at 22:23