Finding the full height of the content of a page/document that can have absolutely positioned elements

前端 未结 2 865
臣服心动
臣服心动 2021-01-24 10:20

I\'m trying to get the height of a page (which might be loaded in an iframe) that has absolutely positioned elements that extend below the normal bottom of the page. Here are th

2条回答
  •  [愿得一人]
    2021-01-24 11:08

    Here's the function I'm using. The major improvement is that it works for IE and chrome (whereas Alessandro's original works for firefox, but gives heights far too large for IE and chrome).

    function getPageHeight() {
        function getUpdatedHeight(element, originalMaxHeight) {
            var top = element.offset().top;
            if(typeof(top)!='undefined'){
                var height = element.outerHeight();
                return Math.max(originalMaxHeight, top+height);
            } else {
                return originalMaxHeight;
            }
        }
    
        var maxhrel = 0;
        if( ! $.browser.msie) {
            maxhrel = $("html").outerHeight(); //get the page height
        } else {
            // in IE and chrome, the outerHeight of the html and body tags seem to be more like the window height
            $('body').children(":not(script)").each(function(){ //get all body children
                maxhrel=getUpdatedHeight($(this), maxhrel);
            });
        }
    
        var atotoffset=0;  // absolute element offset position from the top
        $.each($('body *:not(script)'),function(){   //get all elements
            if ($(this).css('position') == 'absolute'){ // absolute?
                atotoffset=getUpdatedHeight($(this), atotoffset);
            }
        });
    
        return Math.max(maxhrel, atotoffset);
    }
    

提交回复
热议问题