The below attributes in magnific popup will help you to close the popup window,
closeOnBgClick: if it is true, the popup will close whereever click on outside the content(black overlay). To prevent this, assign the value false to close only when click on X (close) button in top right cornor of popup. By defualt, true is assigned for this attribute.
enableEscapeKey: if it is true, the popup will close when click the escape key. To prevent this, assign the value false to close only when click on X (close) button in top right cornor of popup. By defualt, true is assigned for this.
Hope that these points may help you.!
Answer from Srinivasan.S on Stack OverflowThe below attributes in magnific popup will help you to close the popup window,
closeOnBgClick: if it is true, the popup will close whereever click on outside the content(black overlay). To prevent this, assign the value false to close only when click on X (close) button in top right cornor of popup. By defualt, true is assigned for this attribute.
enableEscapeKey: if it is true, the popup will close when click the escape key. To prevent this, assign the value false to close only when click on X (close) button in top right cornor of popup. By defualt, true is assigned for this.
Hope that these points may help you.!
I had the same issue, but looking through the code I found what the problem was.
In the source code, there is a _checkIfClose function that checks where a click takes place. However, when it checks to see if a click is inside the content, it checks the root element's descendants instead of the container's descendants. The content I was loading through ajax did not have one root element, I had something like this:
<ul><li></li><li></li></ul>
<div></div>
<div></div>
By adding a containing <div> tag around my content, it fixed the problem.
It will still close if you click outside your ajaxed content - for example, I have css padding on the magnific container around my content, so if I click on the padding, it will still close.
Hope this helps!
If your snippet is located in your main js, the ajax popup button may not be binded to the event. I imagine two solutions :
Use "on" instead of "click" :(not tested)
$('#close').on( "click", function() {
$.magnificPopup.close();
});
Or do it that way : (tested)
add this function in your main js
function closePopup() {
$.magnificPopup.close();
}
And use this kind of button in your popup html
<button type="button" onClick="closePopup();">Close</button>
Iframe :
If your button is located in an iframe and the magnificpopup script in the main window, on the same domain, you can access the above function like this :
<button type="button" onClick="parent.closePopup();">Close</button>
try this
<pre class="default prettyprint prettyprinted">
<code>
$('#close').click(function(){
$('.mfp-close').trigger('click');
});
</code>
</pre>
This is the old thread but for all the future readers: to fix this, there has to be only one root element in the html that comes out from the ajax call:
As per documentation:
http://dimsemenov.com/plugins/magnific-popup/documentation.html#ajax_type
So in your ajax call you should return something like this:
<div>
...your html content
</div>
and your popup will no longer close itself on content click.
I had the same problem, solved it by using:
modal:true,
instead of:
closeOnContentClick: false,
Slight adjustment ,check fiddle :)
$('.popup-modal').magnificPopup({
type: 'inline', modal: false,});
> $(document).on('click', '.closePopup', function (e)
> {
> e.preventDefault();
> $.magnificPopup.close();
> });
http://jsfiddle.net/qweWa/27/
You need to set modal: false
Demo Fiddle
After some digging found history.js and then the following
var magnificPopup = null;
jQuery(document).ready(function ($) {
var $img = $(".img-link");
if ($img.length) {
$img.magnificPopup({
type: 'image',
preloader: true,
closeOnContentClick: true,
enableEscapeKey: false,
showCloseBtn: true,
removalDelay: 100,
mainClass: 'mfp-fade',
tClose: '',
callbacks: {
open: function () {
History.Adapter.bind(window, 'statechange', closePopup);
History.pushState({ url: document.location.href }, document.title, "?large");
$(window).on('resize', closePopup);
magnificPopup = this;
},
close: function () {
$(window).unbind('statechange', closePopup)
.off('resize', closePopup);
var State = History.getState();
History.replaceState(null, document.title, State.data["url"]);
magnificPopup = null;
}
}
});
}
});
function closePopup () {
if (magnificPopup != null)
magnificPopup.close();
}
I'm using this solution:
callbacks: {
open: function() {
location.href = location.href.split('#')[0] + "#gal";
}
,close: function() {
if (location.hash) history.go(-1);
}
}
And this code:
$(window).on('hashchange',function() {
if(location.href.indexOf("#gal")<0) {
$.magnificPopup.close();
}
});
So, on gallery open I add #gal hash. When user closes I virtually click back button to remove it. If user clicks back button - everything works fine olso.
This solution does not break back button behavior and does no require any additional plugins.
Comments are welcome.