Skip to content Skip to sidebar Skip to footer

Use Magnificpopup With Dynamic Elements

I have two photos, both have the class 'foto'. Under each photo i added a button which allows me to delete the photo. However, i can still open the photo in the galery after removi

Solution 1:

Try this ;)

functioninitMagnificPopup(){
    $('.foto').magnificPopup({
        type: 'image',
        closeOnContentClick: false,
        closeBtnInside: false,
        mainClass: 'mfp-with-zoom mfp-img-mobile',
        image: {
            verticalFit: true,
            titleSrc: function(item) {
                return item.el.attr('title') + ' &middot; <a class="image-source-link" href="'+item.el.attr('data-source')+'" target="_blank">image source</a>';
           }
        },
        gallery: { 
           enabled: true 
        },
        zoom: {
            enabled: true,
            duration: 300, // don't foget to change the duration also in CSSopener: function(element) {
                return element.find('img');
            }
        }
    });
}

$(function(){
    initMagnificPopup();
    /* add call this function whenever you delete an image. */
});

Solution 2:

I found a solution. I added the event listeners into a function, then i just call this function anytime when i need to reinitialise it.

functioninit_magnificPopup()
{
        $('.foto').magnificPopup
        (
            {
                type: 'image',
                closeOnContentClick: false,
                closeBtnInside: false,
                mainClass: 'mfp-with-zoom mfp-img-mobile',
                image: 
                {
                    verticalFit: true,
                    titleSrc: function(item) 
                    {
                        return item.el.attr('title') + ' &middot; <a class="image-source-link" href="'+item.el.attr('data-source')+'" target="_blank">image source</a>';
                    }
                },
                gallery: 
                { 
                    enabled: true 
                },
                zoom: 
                {
                    enabled: true,
                    duration: 300, // don't foget to change the duration also in CSSopener: function(element) 
                    {
                        return element.find('img');
                    }
                }
            }
        );
}

$(document).ready
(
    function()
    {
        init_magnificPopup();
    }
);

So i just call init_magnificPopup() at the end of my delete function. That works :)

Solution 3:

Try this with dynamic elements:

$(document).on('click', '.foto', function () { $(this).magnificPopup({....}); });

Post a Comment for "Use Magnificpopup With Dynamic Elements"