Since I don't yet have enough reputation points to comment, I'm commenting here, in addition to providing a solution.

Comment: JSFiddle isn't working with your http:// images because JSFiddle is trying to serve them from https://

Solution:

You are halfway there. There are 2 parts to making the captions work.

  1. You correctly have put the link in the anchor tag and not the image tag
  2. You must specify your title source in your initialization script like this.

    $(document).ready(function() {
        $('.chicken').magnificPopup({ 
            type: 'image',
            gallery:{enabled:true},
            type: 'image',
            image: {
                titleSrc: 'title' 
                // this tells the script which attribute has your caption
            }
        });
    });
    

The script then automatically writes your caption to its div labeled class="mfp-title"

BONUS Solution: I needed my lightbox image to open in a new window for users on their phones to zoom in, so I added this after the first titleSrc declaration:

    titleSrc: 'title',
    titleSrc: function(item) {
        return '<a href="' + item.el.attr('href') + '">' + item.el.attr('title') + '</a>';
        }

This information is in the documentation: http://dimsemenov.com/plugins/magnific-popup/documentation.html in the "Image Type" section

Answer from JessycaFrederick on Stack Overflow
🌐
Dimsemenov
dimsemenov.com › plugins › magnific-popup
Magnific Popup: Responsive jQuery Lightbox Plugin
Three simple popups with different scaling settings. 1 — fits horizontally and vertically, 2 — only horizontally, 3 — no gaps, zoom animation, close icon in top-right corner. You may put any HTML content in each gallery item and mix content types. In this example lazy-loading of images is enabled for the next image based on move direction.
🌐
CodePen
codepen.io › kisa007 › pen › xxbZOxv
Simple Image Gallery with magnific-popup.js
$(document).ready(function(){ $('.image-popup-vertical-fit').magnificPopup({ type: 'image', mainClass: 'mfp-with-zoom', gallery:{ enabled:true }, zoom: { enabled: true, duration: 300, // duration of the effect, in milliseconds easing: 'ease-in-out', // CSS transition easing function opener: function(openerElement) { return openerElement.is('img') ?
🌐
GitHub
gist.github.com › hirejordansmith › 1ac659aadc1d720f505b28a1540d6547
How to show images and video in Magnific Popup Gallery · GitHub
I've corrected the code and made HTML example: https://github.com/sunzxs/magnific-popup-image-and-video-slider ... Hi, Sunzxs Thanks for sharing example.. I have download all files & run example. But I don't have open video popup. can you please share update or any solutions for that. Thanks advanced. ... <script> $(document).ready(function() { $('.popup-gallery').magnificPopup({ delegate: 'a', type: 'image', gallery: { enabled: true, navigateByImgClick: true, preload: [0, 1] // Will preload 0 - before current, and 1 after the current image }, callbacks: { elementParse: function(item) { if(item.el[0].className == 'video') { item.type = 'iframe'; } else { item.type = 'image'; } } }, }); }); </script>
🌐
Dimsemenov
dimsemenov.com › plugins › magnific-popup › documentation.html
Magnific Popup Documentation
To have multiple galleries on a page, you need to create a new instance of Magnific Popup for each separate gallery.
Top answer
1 of 2
13

Since I don't yet have enough reputation points to comment, I'm commenting here, in addition to providing a solution.

Comment: JSFiddle isn't working with your http:// images because JSFiddle is trying to serve them from https://

Solution:

You are halfway there. There are 2 parts to making the captions work.

  1. You correctly have put the link in the anchor tag and not the image tag
  2. You must specify your title source in your initialization script like this.

    $(document).ready(function() {
        $('.chicken').magnificPopup({ 
            type: 'image',
            gallery:{enabled:true},
            type: 'image',
            image: {
                titleSrc: 'title' 
                // this tells the script which attribute has your caption
            }
        });
    });
    

The script then automatically writes your caption to its div labeled class="mfp-title"

BONUS Solution: I needed my lightbox image to open in a new window for users on their phones to zoom in, so I added this after the first titleSrc declaration:

    titleSrc: 'title',
    titleSrc: function(item) {
        return '<a href="' + item.el.attr('href') + '">' + item.el.attr('title') + '</a>';
        }

This information is in the documentation: http://dimsemenov.com/plugins/magnific-popup/documentation.html in the "Image Type" section

2 of 2
6

I tried to use the selected answer, but even using the documentation, the examples wouldn't work for me. What I ended up using was:

$('.elements').magnificPopup({
   type: 'image',
   gallery: {
       enabled: true
   },
   image: {
       titleSrc: function(item) {
          return item.el.find('img').attr('title');
       }
   }
});

This probably has something to do with the version I was using, but it wasn't clear what version the documentation was for. Hopefully this is useful to someone.

🌐
GitHub
github.com › solodev › magnific-popup-photo-gallery
GitHub - solodev/magnific-popup-photo-gallery: Whether you're showcasing your work or team - displaying images on the web is commonplace but how you display them is another matter altogether. Solodev customizes the Magnific Popup plugin to create outstanding photo galleries.
JavaScript to initialize the pop up is in magnific-popup.js. $(document).ready(function() { $('.popup-gallery').magnificPopup({ delegate: 'a', type: 'image', tLoading: 'Loading image #%curr%...', mainClass: 'mfp-img-mobile', gallery: { enabled: true, navigateByImgClick: true, preload: [0,1] // Will preload 0 - before current, and 1 after the current image }, image: { tError: '<a href="%url%">The image #%curr%</a> could not be loaded.', titleSrc: function(item) { return item.el.attr('title') + '<small>by WebCorpCo</small>'; } } }); });
Author   solodev
Top answer
1 of 1
2

You can create two separate magnificPopup for both parent..

$('.imageParent').each(function() {
    $(this).magnificPopup({
        delegate: 'a.zoom',
        type: 'image',
        gallery: {
            enabled: true,
            preload: [0,2]
        }
    });
});

$('.imageParent').each(function() { 
    $(this).magnificPopup({
        delegate: 'a.zoom',
        type: 'image',
        gallery: {
            enabled: true,
            preload: [0,2]
        }
    });
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/magnific-popup.js/1.1.0/magnific-popup.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/magnific-popup.js/1.1.0/jquery.magnific-popup.js"></script>
<span class ="imageParent parent1">
    <a href="#" target="_blank">
        <span class="portfolio-links">
            <span class="ion-ios-arrow-right portfolio-links-icons">noimage</span> 
        </span>
    </a>&nbsp;
    <a href="https://images.pexels.com/photos/34950/pexels-photo.jpg?auto=compress&cs=tinysrgb&h=350" class="zoom" >
        <span class="portfolio-links">
            <span class="ion-arrow-expand portfolio-links-icons">image 1</span> 
        </span>
    </a>&nbsp;
    <a href="https://media.istockphoto.com/photos/plant-growing-picture-id510222832?k=6&m=510222832&s=612x612&w=0&h=Pzjkj2hf9IZiLAiXcgVE1FbCNFVmKzhdcT98dcHSdSk=" class="zoom">image2</a>&nbsp;
</span>
<span class="imageParent parent2">
    <a href="#" target="_blank">
        <span class="portfolio-links">
            <span class="ion-ios-arrow-right portfolio-links-icons">noimage</span> 
        </span>
    </a>&nbsp;
    <a href="https://wallpaperbrowse.com/media/images/soap-bubble-1958650_960_720.jpg" class="zoom">
        <span class="portfolio-links">
            <span class="ion-arrow-expand portfolio-links-icons">image 3</span> 
        </span>
    </a>
</span>

You can also test it here .. https://jsfiddle.net/nimittshah/vgpsyado/

Without repeating same code.. https://jsfiddle.net/nimittshah/vgpsyado/8/

🌐
Simmons University
web.simmons.edu › ~grovesd › comm244 › notes › week13 › magnific › magnific
How to Use Magnific | Comm244 Notes
Magnific Popup is a variation of a common way of displaying images or text in a modal window. The effect is usually called a "lightbox", named after one of the original JavaScript libraries that used it. There are many different JavaScript libraries you can use to achieve this same effect.
Find elsewhere
🌐
CodePen
codepen.io › ryanjbonnell › pen › DgdGXr
Open Magnific Popup Gallery from Link
$('.gallery-link').on('click', function () { $(this).next().magnificPopup('open'); }); $('.gallery').each(function () { $(this).magnificPopup({ delegate: 'a', type: 'image', gallery: { enabled: true, navigateByImgClick: true }, fixedContentPos: false }); });
🌐
Drupal
drupal.org › project › magnific_popup
Magnific Popup | Drupal.org
February 13, 2024 - Activate the Magnific Popup module, then manage the display of any Image fields or video_embed_field fields. Choose "Magnific Popup" as the formatter and optionally configure Gallery Style options on the field.
🌐
cdnjs
cdnjs.com › home › libraries › magnific-popup.js
magnific-popup.js - Libraries - cdnjs - The #1 free and open source CDN built to make life easier for developers
magnific-popup.js · Lightbox and modal dialog plugin. Can display inline HTML, iframes (YouTube video, Vimeo, Google Maps), or an image gallery. Animation effects are added with CSS3 transitions.
🌐
Smashing Magazine
smashingmagazine.com › 2013 › 05 › truly-responsive-lightbox
Magnific Popup, A Truly Responsive Lightbox Plugin — Smashing Magazine
After the lightbox gallery is opened, it should start loading not one, but a group of images (the number of items in a group should depend on the size of the images and on how likely your visitor will navigate to the next one — test it!). Don’t even think about waiting to load the next image until after the current one has completely loaded; otherwise, it will reduce browsing speed significantly. In Magnific Popup, you can set the number of images to preload before and after the current one, and these two values will automatically switch according to the direction in which the user is navigating.
🌐
CodePen
codepen.io › peposh › pen › GLabXM
Magnific Popup - Open gallery with single image
$(document).ready(function() { $('a.btn-gallery').on('click', function(event) { event.preventDefault(); var gallery = $(this).attr('href'); $(gallery).magnificPopup({ delegate: 'a', type:'image', gallery: { enabled: true } }).magnificPopup('open'); }); });
🌐
SoloDev
solodev.com › blog › web-design › adding-magnific-popup-to-your-photo-gallery.stml
Adding Magnific Popup to your Photo Gallery
September 28, 2016 - This tutorial shows you how to add interactivity to your photo galleries with magnific popup.
🌐
npm
npmjs.com › package › magnific-popup
magnific-popup - npm
June 8, 2024 - Lightbox and modal dialog plugin. Can display inline HTML, iframes (YouTube video, Vimeo, Google Maps), or an image gallery. Animation effects are added with CSS3 transitions. For jQuery or Zepto..
      » npm install magnific-popup
    
Published   Jun 08, 2024
Version   1.2.0
Author   Dmytro Semenov
🌐
Medium
medium.com › web-design-by-solodev › adding-magnific-popup-to-your-photo-gallery-ae6a5caee858
Adding Magnific Popup to your Photo Gallery | by Solodev | web design by solodev | Medium
May 17, 2017 - $(document).ready(function() { $('.popup-gallery').magnificPopup({ delegate: 'a', type: 'image', tLoading: 'Loading image #%curr%...', mainClass: 'mfp-img-mobile', gallery: { enabled: true, navigateByImgClick: true, preload: [0,1] // Will preload 0 - before current, and 1 after the current image }, image: { tError: '<a href="%url%">The image #%curr%</a> could not be loaded.', titleSrc: function(item) { return item.el.attr('title') + '<small>by Solodev</small>'; } } }); });
Top answer
1 of 4
12

Here is a simple solution I've used for this:

$('.image-link, .video-link').magnificPopup({
    callbacks: {
      elementParse: function(item) {
         // the class name
         if(item.el.context.className == 'video-link') {
           item.type = 'iframe';
         } else {
           item.type = 'image';
         }
      }
    },
    gallery:{enabled:true},
    type: 'image',
});

It should work with type formatters.

2 of 4
6

You are able to define the type ofa gallery item using the mfp-TYPE CSS class. So in this case you would add mfp-iframe to the anchor tag pointing to your video and the plugin will handle the rest.

HTML:

<div class="gallery">
  <!-- Image -->
  <a href="https://www.example.com/image-fullsize-1.jpg">
     <img src="https://www.example.com/image-thumbnail-1.jpg" alt="">
  </a>

  <!-- Video item with mfp-iframe class -->
  <a href="https://www.example.com/video-url" class="mfp-iframe">
     <img src="https://www.example.com/video-thumbnail.jpg" alt="">
  </a>

  <!-- Image -->
  <a href="https://www.example.com/image-fullsize-2.jpg">
     <img src="https://www.example.com/image-thumbnail-2.jpg" alt="">
  </a>

  <!-- Image -->
  <a href="https://www.example.com/image-fullsize-3.jpg">
     <img src="https://www.example.com/image-thumbnail-3.jpg" alt="">
  </a>
</div>

JS:

$('.gallery').magnificPopup(
        {
            delegate: 'a',
            type: 'image',
            tLoading: 'Loading image #%curr%...',
            mainClass: 'mfp-img-mobile',
            gallery: {
                enabled: true,
                navigateByImgClick: true,
                preload: [0,1] // Will preload 0 - before current, and 1 after the current image
            },
            image: {
                tError: '<a href="%url%">The image #%curr%</a> could not be loaded.',
                titleSrc: function(item) {
                    return '';
                }
            }
        }
    );

More information can be found in the documentation:

http://dimsemenov.com/plugins/magnific-popup/documentation.html#content-types

🌐
Kriesi
kriesi.at › home › topics › enfold › show gallery in lightbox on button click with magnific popup
Show gallery in lightbox on button click with Magnific Popup - Support | Kriesi.at - Premium WordPress Themes
June 4, 2017 - function fs_magnific_popup(){ ?> <script> (function($){ $(window).load(function() { $('.fs-haustier-shooting-button').magnificPopup({ items: { src: '{fqdn replaced}wp-content/uploads/2017/05/hund-sonnenbrille-1500x996.jpg' }, gallery: { enabled: true }, mainClass: 'avia-popup mfp-zoom-in', type: 'image' }); }); })(jQuery); </script> <?php }
🌐
GitHub
github.com › dimsemenov › Magnific-Popup
GitHub - dimsemenov/Magnific-Popup: Light and responsive lightbox script with focus on performance. · GitHub
Use native <dialog> element if ... dialog/modal/popup, or my PhotoSwipe library if you need an advanced image gallery. Feel free to email me if you need assistance. Fast, light and responsive lightbox plugin, for jQuery and Zepto.js. Documentation and getting started guide. Examples and plugin home page. More examples in CodePen collection. Optionally, install via Bower bower install magnific-popup or npm: ...
Starred by 11.4K users
Forked by 3.4K users
Languages   JavaScript 67.9% | HTML 18.5% | CSS 8.1% | SCSS 5.5%