Multiple Galleries with Magnific Popup

PnkNja15 picture PnkNja15 · May 20, 2013 · Viewed 11k times · Source

I'm trying to create a page with a few galleries using the Magnific-Popup jQuery plug-in. I different sections contained in divs with separate ids and a .gallery class containing the images.

<div id="content_1">
    <p>Some content</p>
    <div class="gallery">
        <a href="img/pic_1"><img src="img/pic_1.jpg"></a>
        <a href="img/pic_2"><img src="img/pic_2.jpg"></a>
    </div>
</div>
<div id="content_2">
    <p>More content</p>
    <div class="gallery">
        <a href="img/pic_3"><img src="img/pic_3.jpg"></a>
        <a href="img/pic_4"><img src="img/pic_4.jpg"></a>
    </div>
</div>

To get the galleries to be separate in the popup I initialized the script multiple times for each content section. When I do this, however, after the first content section, there are more images in the gallery popup (twice as much to be exact) than I linked to. I'm new to javascript, so I'm not sure if I'm just missing something obvious.

Answer

benbrandt picture benbrandt · Oct 28, 2015

From the documentation:

To have multiple galleries on a page, you need to create a new instance of Magnific Popup for each seperate gallery. For example

<div class="gallery">
    <a href="path-to-image.jpg">Open image 1 (gallery #1)</a>
    <a href="path-to-image.jpg">Open image 2 (gallery #1)</a>
</div>
<div class="gallery">
    <a href="path-to-image.jpg">Open image 1 (gallery #2)</a>
    <a href="path-to-image.jpg">Open image 2 (gallery #2)</a>
    <a href="http://vimeo.com/123123" class="mfp-iframe">Open video (gallery #2). Class mfp-iframe forces "iframe" content type on this item.</a> 
</div>

Javascript

$('.gallery').each(function() { // the containers for all your galleries
    $(this).magnificPopup({
        delegate: 'a', // the selector for gallery item
        type: 'image',
        gallery: {
          enabled:true
        }
    });
});

Hope that helps!