2

The problem: I have a jQuery var setup like so:

var count = $("#gallery li:visible").length;
alert(count);

This finds all the gallery li's that are currently visible on the page. The number changes when the pagination is triggered, as people can select categories.

I want to be able to use this count to then look at the 3rd item of every count and addClass of end_item (as i have 3 in a row)

My HTML:

<ul id="gallery">
    <li class="gallery_image" data-id="" data-type="">
        <img src=""/><div class="thumb_bg"></div>
        <img src="" data-original="" />
    </li>
    <li class="gallery_image" data-id="" data-type="">
        <img src=""/><div class="thumb_bg"></div>
        <img src="" data-original="" />
    </li>
    <li class="gallery_image" data-id="" data-type="">
        <img src=""/><div class="thumb_bg"></div>
        <img src="" data-original="" />
    </li>
    <li class="gallery_image" data-id="" data-type="">
        <img src=""/><div class="thumb_bg"></div>
        <img src="" data-original="" />
    </li>
</ul>

Currently trying:

$('#gallery li').each( function(count) {
    if( count % 3 != 2 )
    return
    $(this).addClass('end')
})

I am not going to have exactly 3 on the page, it could be anything from 2 - 12, so i need to find EVERY 3rd item currently visible

EDIT: My code seems to work... however only when the page first loads. If i use a filter item on my gallery, the code is counting the hidden items, even though i am re-calling the jQuery once a filter is clicked.

4

3 回答 3

2

我错过了,但可能有用(将来)

$('#gallery li:visible').filter(function(i){ return i % 3 == 2; }).addClass('end');

演示。

于 2013-07-17T21:55:06.917 回答
2

试一试:

小提琴

$('#gallery li:visible').each(function(i) { 
    if (i % 3 == 2) $(this).addClass('end_item');
});

当您加载更多时,请务必执行以下操作:

$('.end_item').removeClass('end_item');
//then execute code from above
于 2013-07-17T21:23:21.407 回答
0
$('#gallery li:visible').each( function(count) {
      if( (count + 1) % 3 == 0 ) {
        $(this).addClass('end');
      }
});

http://jsfiddle.net/mGZmy/1/

于 2013-07-17T21:26:09.833 回答