繁体   English   中英

jQuery outerHeight没有返回正确的值

[英]jQuery outerHeight is not returning the correct value

我试图通过从窗口大小中减去页眉和页脚值并在文档加载时将内容设置为此值来动态设置网页内容的高度。

每个函数参数都采用元素id来获取元素高度; 排除内容参数。

 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'});
}

我遇到的问题是outerHeight值返回错误的值。 标题返回23px和页脚40px。

在检查FF和Chrome中的元素时,值为25px和44px。

我尝试过使用innerHeight,outerHeight和outerHeight(true)但没有得到正确的值。

什么可能出错? 或动态设置内容高度的另一种方法? 我已经没有头发拉,所以任何帮助都表示赞赏。

编辑:我正在处理iframe中的内容。 以下内容: winH = top.innerHeight获取最顶层iframe窗口的高度值。

尝试帮助我的一件事是将检查outerHeight()的代码放在$(window).load()而不是$(document).ready() 很明显在很多情况下使用$(document.ready() ,但有时候outerHeight()值不正确是由于元素没有被完全加载。

我修改了一个用于计算屏幕宽度/高度的脚本。 看看这是否有效:

    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