元素.height返回可见高度-我想要total

element.height returns visible height - I want total?

本文关键字:我想要 total 高度 height 返回 元素      更新时间:2023-09-26

所以显然这只发生在我身上-我不知道为什么,但如果它有效,我很高兴:)

我有一个全屏幻灯片,我创建了一个函数,将任何太大的图像垂直居中。

    function fixImages(){
    maxheight = $('.index-slide-container').height();
    $('.index-slide-container img').each( function(index, ele){
        if (ele.height > maxheight){
            offset = ( maxheight - ele.height ) / 2;
            $(ele).css('top', offset + 'px');
        }
    });
}
fixImages();

然而,ele.height返回图像可见部分的高度(它的容器的高度,因为它有overflow:hidden,即使当我console.log(ele)并展开元素时,'height'显然是正确的值。

我也尝试了$(ele).height(), $(ele).css('height'), $(ele).outerHeight()ele.clientHeight;它们都返回相同的值。

谢谢

我做了一些测试,$('img').height();给了我正确的图片高度

如果你想让图片垂直居中,为什么不像这样使用css的绝对定位呢?例如:

.index-slide-container img {
    position:absolute;
    top:50%;
}

然后,你可以用jQuery编程设置负边距:

 $('.index-slide-container img').each( function(i, e){
     var height = $(e).height() / 2;
     $(e).css({'margin-top':'-'+height});
 });