简体   繁体   English

jQuery outerHeight没有返回正确的值

[英]jQuery outerHeight is not returning the correct value

I am trying to dynamically set the height of the webpage content by subtracting the header and footer values from the window size and setting the content to this value on document load. 我试图通过从窗口大小中减去页眉和页脚值并在文档加载时将内容设置为此值来动态设置网页内容的高度。

Each of the functions parameters takes in an element id in order to grab the element height; 每个函数参数都采用元素id来获取元素高度; excluding the content parameter. 排除内容参数。

 function setH(header, content, footer) 
{
    winH = top.innerHeight;
    winTitle = 32; // height value of the window title (part of the header)
    headH = $(header).outerHeight(true);
    footH = $(footer).outerHeight(true);
    contentH = winH - winTitle - headH - footH;
    $(content).css({'height' : (contentH) + 'px','overflow-y' : 'scroll'});
}

The issue that I am running into is the outerHeight values are returning the wrong values. 我遇到的问题是outerHeight值返回错误的值。 The header returns 23px and footer 40px. 标题返回23px和页脚40px。

When examining the elements in FF and Chrome the values are 25px and 44px. 在检查FF和Chrome中的元素时,值为25px和44px。

I have tried using innerHeight,, outerHeight and outerHeight(true) but not getting the correct values. 我尝试过使用innerHeight,outerHeight和outerHeight(true)但没有得到正确的值。

Any suggestions on what might be going wrong? 什么可能出错? or an alternative way to setting the height of the content dynamically? 或动态设置内容高度的另一种方法? I'm running out of hair to pull so any help is appreciated. 我已经没有头发拉,所以任何帮助都表示赞赏。

Edit: I am working with content in iframes. 编辑:我正在处理iframe中的内容。 The following: winH = top.innerHeight is getting the height value of the top most iframe window. 以下内容: winH = top.innerHeight获取最顶层iframe窗口的高度值。

One thing to try which helped me is to put the code that checks outerHeight() in $(window).load() and not $(document).ready() . 尝试帮助我的一件事是将检查outerHeight()的代码放在$(window).load()而不是$(document).ready() Obviously in many cases it's fine to use $(document.ready() , but sometimes the incorrect value of outerHeight() is caused from elements not being completely loaded. 很明显在很多情况下使用$(document.ready() ,但有时候outerHeight()值不正确是由于元素没有被完全加载。

I've modified a script I use for calculating screenwidth/height. 我修改了一个用于计算屏幕宽度/高度的脚本。 See if this works: 看看这是否有效:

    if (typeof window.innerWidth != 'undefined') {
        viewportwidth = window.innerWidth;
        viewportheight = window.innerHeight;
        winTitle = 32;
        headH = $(header).outerHeight(true);
        footH = $(footer).outerHeight(true);
        contentH = viewportheight - winTitle - headH - footH;
    }

    // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)

    else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) {
        viewportwidth = document.documentElement.clientWidth;
        viewportheight = document.documentElement.clientHeight;
        winTitle = 32;
        headH = $(header).outerHeight(true);
        footH = $(footer).outerHeight(true);
        contentH = viewportheight - winTitle - headH - footH;
    }

    // older versions of IE

    else {

    viewportwidth = document.getElementsByTagName('body')[0].clientWidth;
    viewportheight = document.getElementsByTagName('body')[0].clientHeight;
    }
    document.getElementById("content id").style.height = contentH + "px";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM