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.
- You correctly have put the link in the anchor tag and not the image tag
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 Overflowcss - How to include caption for gallery using magnific popup? - Stack Overflow
jquery - Magnific Pop-Up - Show different gallery depending upon image clicked - Stack Overflow
jquery - Magnific Popup – Image gallery inside of modal box - Stack Overflow
How can I get magnific popup to show both images and an iframe video in one dynamic gallery?
Videos
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.
- You correctly have put the link in the anchor tag and not the image tag
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
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.
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.
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
<script type="text/javascript">
$(document).ready(function(){
$('div.gallery').magnificPopup({delegate: 'img' , type: 'image', gallery:{enabled:true},
callbacks: {
elementParse: function() { this.item.src = this.item.el.attr('src'); }
}
});
});
</script>
elementParse http://dimsemenov.com/plugins/magnific-popup/documentation.html#api
I actually have not worked, and I found how to do . Now the script when clicking pictures increases and creates a gallery of them .
<script type="text/javascript">
$(document).ready(function() {
$('#text').magnificPopup({
delegate: 'img',
type: 'image',
gallery: {
enabled: true
},
callbacks: {
elementParse: function(qw) {
qw.src = qw.el.attr('src');
}
}
});
});
</script>