开发者

Add heights of each matched element with jQuery

I have this function

            $('.gallery').each(function(){
                var thumbCount = $(this).find('.ngg-gallery-thumbnail-box').size();
                var rows = thumbCount/5;
                var height = rows*145;
                $(this).css({'height':height+24});
            });

Which gets the height of each .gallery div. I want to go one step further and take all of those height variables from inside the each function and add them together (getting the total height for all .gallery divs)-- but I don't know how.

Could someone show me t开发者_如何学编程he proper syntax for this?

Thanks!


With your current code:

var totalHeight = 0;
$('.gallery').each(function(){
    var thumbCount = $(this).find('.ngg-gallery-thumbnail-box').size();
    var rows = thumbCount/5;
    var height = rows*145;
    totalHeight += height;
    $(this).css({'height':height+24});
});

BTW, here's how I might simplify your existing code a wee bit:

var totalHeight = 0;

$('.gallery').each(function(){
    var thumbCount = $(this).find('.ngg-gallery-thumbnail-box').length,
        rows = thumbCount/5,
        height = rows*145;

    totalHeight += height;

    $(this).css('height', height+24);
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜