Answer the question
In order to leave comments, you need to log in
Swiper slider creates duplicate slides, what to do when using lightbox?
Hello. I will try to explain the essence of the issue.
I really like Swiper slider, I use it a lot. But there was a problem when combining the slider in Loop mode with the Magnific Popup lightbox
https://dimsemenov.com/plugins/magnific-popup/
There is a gallery mode, the code is like this:
$(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 Marsel Van Oosten</small>';
}
}
});
});
Answer the question
In order to leave comments, you need to log in
This solution did not suit me a bit (I could not attach options for video, etc. to it), but in principle it works, at least from what I could find, maybe it will come in handy for someone:
$('.product-tab-gallery').each(function() {
var items = [];
var $gallery = $(this);
// Get links
$gallery.find('a.imageitem').each(function() {
items.push($(this).attr('href'));
});
var uniqueItems = [];
// Remove duplicates
$.each(items, function(i, el) {
if($.inArray(el, uniqueItems) === -1) {
uniqueItems.push(el);
}
});
items = [];
// Put in Magnific format
$.each(uniqueItems, function(i, el) {
items.push({
src: el,
type: 'image'
});
});
// Assign an index to each link so the clicked one is shown first
$gallery.find('a.imageitem').each(function() {
var $a = $(this);
$.each(items, function(i, el) {
if (el.src == $a.attr('href')) {
$a.data('index', i);
return;
}
});
});
$gallery.on('click', 'a.imageitem', function(e) {
e.preventDefault();
var $a = $(this);
$.magnificPopup.open({
type: 'image',
items: items,
gallery: {
enabled: true
}
});
$.magnificPopup.instance.goTo($a.data('index'));
});
});
In a similar situation, I created a hidden block with links to the gallery and connected it with data-attributes to the slider. You click on the image in the slider - a link from a hidden block opens, in which there are no duplicates.
I just used :not(.swiper-slide-duplicate) in the initialization of magnific, in the delegate.
Like this:
delegate: '.tile.swiper-slide:not(.swiper-slide-duplicate) a, *:not(.swiper-slide) > a',
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question