2011-07-29 45 views
2

我有這個功能添加每一個匹配元素的高度與jQuery

  $('.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}); 
      }); 

它得到各.gallery div的高度。我想更進一步,並從每個函數內部獲取所有這些變量,並將它們加在一起(獲得所有.gallery div的總高度) - 但我不知道如何。

有人可以告訴我適當的語法嗎?

謝謝!

回答

4

根據您當前的代碼:

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}); 
}); 

順便說一句,這裏就是我可能會簡化現有的代碼一丁點:

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

優秀謝謝! –

相關問題