jQuery是否获取元素的渲染高度?

jQuery是否获取元素的渲染高度?,jquery,css,height,Jquery,Css,Height,如何获得元素的渲染高度 假设您有一个元素,其中包含一些内容。里面的内容将延伸的高度。如果没有明确设置高度,如何获得“渲染”高度。显然,我试过: var h = document.getElementById('someDiv').style.height; 这样做有什么诀窍吗?如果有帮助的话,我正在使用jQuery。尝试以下方法之一: var h = document.getElementById('someDiv').clientHeight; var h = document.getEle

如何获得元素的渲染高度

假设您有一个
元素,其中包含一些内容。里面的内容将延伸
的高度。如果没有明确设置高度,如何获得“渲染”高度。显然,我试过:

var h = document.getElementById('someDiv').style.height;
这样做有什么诀窍吗?如果有帮助的话,我正在使用jQuery。

尝试以下方法之一:

var h = document.getElementById('someDiv').clientHeight;
var h = document.getElementById('someDiv').offsetHeight;
var h = document.getElementById('someDiv').scrollHeight;
clientHeight
包括高度和垂直填充

offsetHeight
包括高度、垂直填充和垂直边框

scrollHeight
包括所包含文档的高度(在滚动的情况下会大于高度)、垂直填充和垂直边框。

请尝试以下选项之一:

var h = document.getElementById('someDiv').clientHeight;
var h = document.getElementById('someDiv').offsetHeight;
var h = document.getElementById('someDiv').scrollHeight;
clientHeight
包括高度和垂直填充

offsetHeight
包括高度、垂直填充和垂直边框

scrollHeight
包括所包含文档的高度(在滚动的情况下会大于高度)、垂直填充和垂直边框。

应该是

$('#someDiv').height();
使用jQuery。这将以数字形式检索包装集中第一个项目的高度

试图使用

.style.height
仅当您首先设置了属性时才有效。不是很有用

应该是

$('#someDiv').height();
使用jQuery。这将以数字形式检索包装集中第一个项目的高度

试图使用

.style.height

仅当您首先设置了属性时才有效。不是很有用

您是否在css中专门设置了高度?如果没有,则需要使用
离视而不是
高度

var h = document.getElementById('someDiv').style.offsetHeight;

您是否在css中专门设置了高度?如果没有,则需要使用
离视而不是
高度

var h = document.getElementById('someDiv').style.offsetHeight;

通常在视线外


如果需要计算某些内容但不显示它,请将元素设置为
可见性:隐藏
位置:绝对
,将其添加到DOM树中,获取
离视
,然后将其删除。(我上次检查时,原型库在幕后就是这么做的)。

通常在视线之外


如果需要计算某些内容但不显示它,请将元素设置为
可见性:隐藏
位置:绝对
,将其添加到DOM树中,获取
离视
,然后将其删除。(这就是我上次检查时原型库在幕后所做的工作)。

使用MooTools


$('someDiv').getSize().y

使用MooTools

$('someDiv').getSize().y

一定要使用

$('#someDiv').height()   // to read it

我把这个作为一个额外的答案,因为我刚刚学到了一些重要的东西

我刚才差点掉进了利用越位的陷阱。事情是这样的:

  • 我使用了使用调试器“观察”元素的属性的老把戏
  • 我看到哪一个值与我期望的值相近
  • 那是在视线之外,所以我用了它
  • 然后我意识到它对一个隐藏的DIV不起作用
  • 在计算了maxHeight之后,我试着躲藏起来,但那看起来很笨拙——弄得一团糟
  • 我做了一次搜索-发现jQuery.height()-并使用了它
  • found out height()甚至对隐藏元素也有效
  • 只是为了好玩,我检查了高度/宽度的jQuery实现
这只是其中的一部分:

Math.max(
Math.max(document.body["scroll" + name], document.documentElement["scroll" + name]),
Math.max(document.body["offset" + name], document.documentElement["offset" + name])
) 
是的,它同时查看滚动和偏移。如果失败了,考虑到浏览器和css的兼容性问题,它会更进一步。换言之,我不关心或不想关心的东西

但是我不必。谢谢jQuery

这个故事的寓意是:如果jQuery有一个方法,那么它可能有一个很好的理由,很可能与兼容性有关

如果你还没有读过最近的文章,我建议你看看。

一定要用

$('#someDiv').height()   // to read it

我把这个作为一个额外的答案,因为我刚刚学到了一些重要的东西

我刚才差点掉进了利用越位的陷阱。事情是这样的:

  • 我使用了使用调试器“观察”元素的属性的老把戏
  • 我看到哪一个值与我期望的值相近
  • 那是在视线之外,所以我用了它
  • 然后我意识到它对一个隐藏的DIV不起作用
  • 在计算了maxHeight之后,我试着躲藏起来,但那看起来很笨拙——弄得一团糟
  • 我做了一次搜索-发现jQuery.height()-并使用了它
  • found out height()甚至对隐藏元素也有效
  • 只是为了好玩,我检查了高度/宽度的jQuery实现
这只是其中的一部分:

Math.max(
Math.max(document.body["scroll" + name], document.documentElement["scroll" + name]),
Math.max(document.body["offset" + name], document.documentElement["offset" + name])
) 
是的,它同时查看滚动和偏移。如果失败了,考虑到浏览器和css的兼容性问题,它会更进一步。换言之,我不关心或不想关心的东西

但是我不必。谢谢jQuery

这个故事的寓意是:如果jQuery有一个方法,那么它可能有一个很好的理由,很可能与兼容性有关


如果你还没有读过最近的文章,我建议你看看。

那么这就是答案吗

“如果需要计算某个元素但不显示它,请将该元素设置为
可见性:隐藏
位置:绝对
,将其添加到DOM树中,获取offsetHeight,然后将其删除。(这是我上次检查时原型库在行后面执行的操作)。”

我在很多方面也有同样的问题。网站上没有jQuery或原型可供使用,但如果可行的话,我完全赞成借用这种技术。作为一些失败的事情的一个例子,我有以下代码:

// Layout Height Get
function fnElementHeightMaxGet(DoScroll, DoBase, elementPassed, elementHeightDefault)
{
    var DoOffset = true;
    if (!elementPassed) { return 0; }
    if (!elementPassed.style) { return 0; }
    var thisHeight = 0;
    var heightBase = parseInt(elementPassed.style.height);
    var heightOffset = parseInt(elementPassed.offsetHeight);
    var heightScroll = parseInt(elementPassed.scrollHeight);
    var heightClient = parseInt(elementPassed.clientHeight);
    var heightNode = 0;
    var heightRects = 0;
    //
    if (DoBase) {
        if (heightBase > thisHeight) { thisHeight = heightBase; }
    }
    if (DoOffset) {
        if (heightOffset > thisHeight) { thisHeight = heightOffset; }
    }
    if (DoScroll) {
        if (heightScroll > thisHeight) { thisHeight = heightScroll; }
    }
    //
    if (thisHeight == 0) { thisHeight = heightClient; }
    //
    if (thisHeight == 0) { 
        // Dom Add:
        // all else failed so use the protype approach...
        var elBodyTempContainer = document.getElementById('BodyTempContainer');
        elBodyTempContainer.appendChild(elementPassed);
        heightNode = elBodyTempContainer.childNodes[0].offsetHeight;
        elBodyTempContainer.removeChild(elementPassed);
        if (heightNode > thisHeight) { thisHeight = heightNode; }
        //
        // Bounding Rect:
        // Or this approach...
        var clientRects = elementPassed.getClientRects();
        heightRects = clientRects.height;
        if (heightRects > thisHeight) { thisHeight = heightRects; }
    }
    //
    // Default height not appropriate here
    // if (thisHeight == 0) { thisHeight = elementHeightDefault; }
    if (thisHeight > 3000) {
        // ERROR
        thisHeight = 3000;
    }
    return thisHeight;
}
这基本上是为了得到一个零的结果而尝试任何事情。没有a的ClientHeight
document.getElementById(id_attribute_value).offsetHeight; 
style = window.getComputedStyle(your_element);
var geometry;
geometry={};
var element=document.getElementById(#ibims);
var rect = element.getBoundingClientRect();
this.geometry.top=rect.top;
this.geometry.right=rect.right;
this.geometry.bottom=rect.bottom;
this.geometry.left=rect.left;
this.geometry.height=this.geometry.bottom-this.geometry.top;
this.geometry.width=this.geometry.right-this.geometry.left;
console.log(this.geometry);
document.querySelector('.project_list_div').offsetHeight;
document.getElementById('someDiv').getBoundingClientRect().height
this.$refs['some-ref'].getBoundingClientRect().height
this.$refs['some-ref'].$el.getBoundingClientRect().height
function testDistance(node1, node2) {
    /* get top position of node 1 */
    let n1Pos = node1.offsetTop;
    /* get height of node 1 */
    let n1Height = node1.clientHeight;
    /* get top position of node 2 */
    let n2Pos = node2.offsetTop;
    /* get height of node 2 */
    let n2Height = node2.clientHeight;

    /* add height of both nodes */
    let heightTogether = n1Height + n2Height;
    /* calculate distance from top of node 1 to bottom of node 2 */
    let actualDistance = (n2Pos + n2Height) - n1Pos;

    /* if the distance between top of node 1 and bottom of node 2
       is bigger than their heights combined, than there is something between them */
    if (actualDistance > heightTogether) {
        /* do something here if they are not together */
        console.log('they are not together');
    } else {
        /* do something here if they are together */
        console.log('together');
    } 
}