Magnific-Popup, Limit items to direct siblings

2019-04-08 23:58发布

问题:

How can I limit magnific-popup gallery to direct siblings? When I open an image from the first .child, I don’t want the images from the second .child to be in the gallery.

The html structure:

<div class="parent">
    <div class="child">
        <a class="image-link" href="img/red1.jpg"><img src="img/red1.jpg"></a>
        <a class="image-link" href="img/red2.jpg"><img src="img/red2.jpg"></a>
        <a class="image-link" href="img/red3.jpg"><img src="img/red3.jpg"></a>
        <a class="image-link" href="img/red4.jpg"><img src="img/red4.jpg"></a>
        <a class="image-link" href="img/red5.jpg"><img src="img/red5.jpg"></a>
    </div>

    <div class="child">
        <a class="image-link" href="img/city1.jpg"><img src="img/city1.jpg"></a>
        <a class="image-link" href="img/city2.jpg"><img src="img/city2.jpg"></a>
        <a class="image-link" href="img/city3.jpg"><img src="img/city3.jpg"></a>
        <a class="image-link" href="img/city4.jpg"><img src="img/city4.jpg"></a>
        <a class="image-link" href="img/city5.jpg"><img src="img/city5.jpg"></a>
    </div>
</div>

how I initialize Magnific-Popup:

$('.child').magnificPopup({ 
    delegate: '.image-link',
    type:'image', 
    gallery: {
        enabled: true
    }
});

回答1:

If delegate option is used, only like so:

$('.child').each(function() {
     $(this).magnificPopup({ 
        delegate: '.image-link',
        type:'image', 
        gallery: {
            enabled: true
        }
    });
});