Search code examples
flexslidermagnific-popupslick.js

Magnific pop-up synchronized with slider


Just wondering if it is possible to synchronize Magnific pop-up with a slider(flexslider or slick, for example) so that when you change slide on Magnific pop-up the slider in the background also changes.

EDIT

$('.slick-slider').magnificPopup({
    delegate: '.slides:not(.slick-cloned) a',
    type: 'image',
    mainClass: 'mfp-fade',
    fixedContentPos: false,
    removalDelay: 350,
    pauseOnHover: false,
    gallery:{
        enabled: true,
        tCounter: '<span class="mfp-counter">%curr% of %total%</span>'
    },
    callbacks: {
        beforeClose: function() {
            $('.slick-slider').slick('slickGoTo', parseInt(this.index - 1));

           // works better with the code bellow but sometimes gets stuck between 2 slides 
           $('.slick-slider').slick('slickGoTo', parseInt(this.index));

        }
    }
});

Solution

  • It's definitely possible. Here's an example using Slick.js.

    Working Example

    HTML

    <div class="carousel">
        <a href="//satyr.io/1280x720/1"><img src="//satyr.io/600x400/1" width="600" height="400" alt="" /></a>
        <a href="//satyr.io/1280x720/2"><img src="//satyr.io/600x400/2" width="600" height="400" alt="" /></a>
        <a href="//satyr.io/1280x720/3"><img src="//satyr.io/600x400/3" width="600" height="400" alt="" /></a>
        <a href="//satyr.io/1280x720/4"><img src="//satyr.io/600x400/4" width="600" height="400" alt="" /></a>
        <a href="//satyr.io/1280x720/5"><img src="//satyr.io/600x400/5" width="600" height="400" alt="" /></a>
    </div>
    

    JS

    $(document).ready(function() {
    
        var $carousel = $('.carousel');
    
        $carousel
            .slick()
            .magnificPopup({
                type: 'image',
                delegate: 'a:not(.slick-cloned)',
                gallery: {
                    enabled: true
                },
                callbacks: {
                    open: function() {
                        var current = $carousel.slick('slickCurrentSlide');
                        $carousel.magnificPopup('goTo', current);
                    },
                    beforeClose: function() {
                        $carousel.slick('slickGoTo', parseInt(this.index));
                    }
                }
            });
    });
    

    Dependencies

    Also, make sure you pull in the dependencies for jQuery, Slick, and Magnific Popup.

    Update

    • Accounts for cloned item created by Slick
    • Updates Magnific Popup with current slide when opened via keyboard navigation