开发者

Add heights of each matched element with jQuery

开发者 https://www.devze.com 2023-03-23 13:18 出处:网络
I have this function $(\'.gallery\').each(function(){ var thumbCount = $(this).find(\'.ngg-gallery-thumbnail-box\').size();

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

精彩评论

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