Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danny-englander/7071287 to your computer and use it in GitHub Desktop.
Save danny-englander/7071287 to your computer and use it in GitHub Desktop.
function urlFromHash() {
if (location.hash.substr(0, 2) != '#!') {
return null;
}
// why not location.hash? => http://stackoverflow.com/q/4835784/298479
return location.href.split('#')[1].substr(1);
}
$('#gallery').magnificPopup({
type: 'image',
delegate: 'a[rel="photo"]',
key: 'photos',
gallery: {
enabled: true,
preload: [0, 2]
},
callbacks: {
close: function () {
location.hash = '';
},
change: function (item) {
location.hash = '#!' + item.el.attr('href');
}
}
});
var hashUrl = urlFromHash();
if (hashUrl) {
$pm.find('a[href="' + hashUrl + '"]').trigger('click');
}
$(window).on('hashchange', function () {
var mp = $.magnificPopup.instance;
if (!mp) {
// this check is not needed in this example, but in some cases you might delay everything before
// this event e.g. until something else finished loading - in that case hashchange might trigger before
return;
}
var url = urlFromHash();
if (!url && mp.isOpen) {
// no url => close popup
mp.close();
} else if (url) {
if (!mp.isOpen) {
// not open => simply click the matching link
gallery.find('a[href="' + url + '"]').trigger('click');
} else if (!mp.currItem || mp.currItem.el.attr('href') != url) {
// open => only update if necessary
var index = null;
$.each(mp.items, function (i, item) {
var jqItem = item.parsed ? item.el : $(item);
if (jqItem.attr('href') == url) {
index = i;
return false;
}
});
if (index !== null) {
mp.goTo(index);
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment