0

i have a page with 4 fancybox (v.2) gallerys, and when i click trough the first gallery he circles through ALL galleries on the page, that's bad. so i want to give each gallery a different "rel" attribute (rel="gallery1", rel="gallery2", rel="gallery3" ...). so i need to count these gallerys and give them the number. my code:

$(document).ready(function() {
$(".fancybox")
    .attr('rel', 'gallery')
    .fancybox({
        openEffect  : 'none',
        closeEffect : 'none',
        padding : 0,
        loop : false
});

});

1 Answers1

0

Assuming each gallery is in a <div class="post"> like:

<!-- first gallery -->
<div class="post">
    <a class="fancybox">...</a>
    <a class="fancybox">...</a>
</div>

<!-- second gallery -->
<div class="post">
    <a class="fancybox">...</a>
    <a class="fancybox">...</a>
</div>

You simply need to use .each( function(index, element) ) to loop over the .post elements. Use each .post index to build the gallery identifier, and use each .post element as a jQuery selector context when selecting your .fancybox elements:

$(document).ready(function() {
    $(".post").each(function(idx, elm) {
        $(".fancybox", elm)
            .attr('rel', 'gallery' + idx)
            .fancybox({
                // ...
            });
    });
});
...

$(".fancybox", elm) tells jQuery to look for elements with the fancybox class, but only match elements that are descendants of elm (here, the current .post element for the .each loop).

Depending on how fancybox works, you might be able to move the .fancybox() call outside of the each:

$(".post").each(function(idx, elm) {
    // assign `rel` values as above...
});

$(".fancybox").fancybox({
    // ...
});
apsillers
  • 112,806
  • 17
  • 235
  • 239
  • this gives every .fancybox link the count, but i need to give all .fancybox links in EACH gallery the same count (gallery1: rel="gallery0, gallery2: rel="gallery1 ...). –  Dec 27 '13 at 14:28
  • @herrfischer I see. How do you determine which gallery a given link belongs to? If I look at any `.fancybox` element on your page, how could I tell which gallery it belongs to? I don't know fancybox very well. You can edit your question to include relevant HTML if necessary. – apsillers Dec 27 '13 at 14:35
  • each gallery is in a DIV called "post". so i have to count the "post" DIVs (in this case 4 but it's dynamic) and give all .fancybox links inside the same rel attribute ... –  Dec 27 '13 at 14:37
  • @herrfischer Edited my answer to use `.each` over the `.post` elements. – apsillers Dec 27 '13 at 15:18