使用jquery获取多个div的高度

hbo*_*man 1 jquery jquery-selectors

我需要获得三个div的高度,将它们加在一起,看看滚动位置是否大于该数字.现在我能够获得一个元素的高度,但我怎么能添加其他元素呢?

基本上,我想写"如果scroll_top大于div 1 + div 2 + 3的高度"

var scroll_top = $(window).scrollTop();

if ((scroll_top > $('.nav-bar-fixed').height()) {
    alert('sometext');
}
Run Code Online (Sandbox Code Playgroud)

Den*_*ret 11

为什么不简单呢?

var h = 0;
$('#div1, #div2, #div3').each(function(){ h+=$(this).height() });
Run Code Online (Sandbox Code Playgroud)