I have a masonry gallery with ul / li elements. To make them responsive I am adding to each 'li' element it's column number as a class, so I can sort them later.
I am trying to get previous elements height in column before adding new one. So basically the column height before new 'li' element.
I am using prevAll(), to get all previous elements, but I can't combine height calculation and filtering columns. So tried to achieve it with each(), with filter, and with changing selector, I added that part of the code.
var that= this;
that.prevAll = $(this).prevAll();
that.top += $(this).prevAll('.column' + that.current_column).height();
// selector
that.top += that.prevAll.filter('.column' + that.current_column).height();
// filter
that.prevAll.each(function(){
if(that.prevAll.hasClass('.column' + that.current_column)) {
that.top += that.prevAll.height() + that.settings.imegesGap;
};
});
// .each
DOM
jQuery
$this.addClass('column' + that.current_column);
$this.addClass('row' + that.row);
HTML
<ul class="mosaic1">
<li ><img src="img/test.png"></li>
<li ><img src="img/test6.jpg"></li>
<li ><img src="img/test4.jpg"></li>
<li ><img src="img/test.3.jpg"></li>
<li ><img src="img/test7.jpg"></li>
<li ><img src="img/test5.png"></li>
</ul>
The main problem with 'each' function.It keeps summing up.
P.S. This is my first question here, so sorry if description not good enough.