多个画廊与Magnific Popup

Pnk*_*a15 3 magnific-popup

我正在尝试使用Magnific-Popup jQuery插件创建一个包含几个画廊的页面.我在divs中包含不同的部分,包含单独的ID和.gallery包含图像的类.

<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>
Run Code Online (Sandbox Code Playgroud)

为了使图库在弹出窗口中分开,我为每个内容部分多次初始化脚本.但是,当我这样做时,在第一个内容部分之后,画廊弹出窗口中有更多图像(确切地说是两倍).我是javascript的新手,所以我不确定我是否只是错过了一些明显的东西.

小智 10

从文档:

要在页面上拥有多个图库,您需要为每个单独的图库创建一个新的Magnific Popup实例.例如

<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>
Run Code Online (Sandbox Code Playgroud)

使用Javascript

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

希望有所帮助!