dimsemenov / Magnific-Popup

Light and responsive lightbox script with focus on performance.
http://dimsemenov.com/plugins/magnific-popup/
MIT License
11.39k stars 3.48k forks source link

Open() method doesn't work for the dynamic popups #227

Open drgullin opened 11 years ago

drgullin commented 11 years ago

// Open popup immediately. If popup is already opened - it'll just overwite the content (but old options will be kept). // - first parameter: options object // - second parameter (optional): index of item to open $.magnificPopup.open({ items: { src: 'someimage.jpg' }, type: 'image'

// You may add options here, they're exactly the same as for $.fn.magnificPopup call // Note that some settings that rely on click event (like disableOn or midClick) will not work here }, 0);

This doesn't work for a dynamically created popups.

Here's an example http://jsbin.com/esebEGa/1

Child static works well:

$('.toggle').each(function() {
  var item = $(this);
  var decor = item.data('mfp-decor');

  item.magnificPopup({
    type: 'inline',
    midClick: true,
    removalDelay: 400,
    mainClass: function() {
      var classname = 'mfp-fade';

      if (decor && decor.length) {
        classname = classname + ' ' + decor;
      }

      return classname;
    }
  });
});

Child dymanic doesn't work:

$('.dynamic').on('click', function(event) {
    event.preventDefault();

      $.magnificPopup.open({
            items: {
              src: '<div class="popup popup-status">Dynamic</div>'
            },
            type: 'inline',
            showCloseBtn: false,
            removalDelay: 400,
            mainClass: 'mfp-status'
          });
});

If child dymanic link is clicked, both popups are closed.

I've also tried this solution https://github.com/dimsemenov/Magnific-Popup/issues/53#issuecomment-17804411 - same result, both popups are closed once you try to open a dynamically created popup.

drgullin commented 11 years ago

This happens because of the wrong clicked target checking. Once some element is clicked to replace a current popup, both popups are closed, because plugin checks for a clicked target existance inside the new content, not the previous one. This means, if there's no clicked target clone inside the replacing content, popup will be closed.

Here's a hotfix:

mfp.prevContent = mfp.content;
if (mfp.prevContent) {
  if (!$.contains(mfp.prevContent[0], target)) {
    return true;
  } else {
    return false;
  }
} else {

same line, append:

}
if (!mfp.isOpen) {}
mfp.prevContent = null;

Since this solution may have some side effects, I don't make a pull request.

It works for me both for inline and image (gallery) types.

dimsemenov commented 11 years ago

Thank you, I'll look into it.

By the way, to fix this you may just add event.stopImmediatePropagation(); on that .dynamic click event handler, so the event won't come to popup at all.

drgullin commented 11 years ago

You're welcome.

Same issue for the ajax loaded popups, current hotfix doesn't help.

owlmsj commented 10 years ago

Looking for a hack too.

xfechx commented 9 years ago

Has this been resolved?

I am trying to open a magnific popup from a link within an ajax generated magnific popup, without success...

jpcharrier commented 8 years ago

@xfechx did you ever resolve your issue? I'm having the same issue as mentioned here: #852

xfechx commented 8 years ago

Hi @jpcharrier, no I still unable to resolve it. Can't swap ajax content in one magnific popup.

It works with normal non-ajax content though...

Let me know if you find a workaround!

On Tue, 19 Apr 2016, 07:54 jpcharrier notifications@github.com wrote:

@xfechx https://github.com/xfechx did you ever resolve your issue? I'm having the same issue as mentioned here: #852 https://github.com/dimsemenov/Magnific-Popup/issues/852

— You are receiving this because you were mentioned. Reply to this email directly or view it on GitHub https://github.com/dimsemenov/Magnific-Popup/issues/227#issuecomment-211597807

jpcharrier commented 8 years ago

@xfechx you will be the first to find out (other than the boss of course!). There must be a way... I might try and abstract the ajax away from Magnific and use raw JS there to achieve it.

Still think it would be great if there was a simple update method for ajax'd content!

xfechx commented 8 years ago

I agree! It would totally change many things around the web and actively updating popups with ajax loaded content!

On Tue, 19 Apr 2016, 15:16 jpcharrier notifications@github.com wrote:

@xfechx https://github.com/xfechx you will be the first to find out (other than the boss of course!). There must be a way... I might try and abstract the ajax away from Magnific and use raw JS there to achieve it.

Still think it would be great if there was a simple update method for ajax'd content!

— You are receiving this because you were mentioned. Reply to this email directly or view it on GitHub https://github.com/dimsemenov/Magnific-Popup/issues/227#issuecomment-211731678

nvontrotha commented 8 years ago

Has there been any update on this?