Last active
June 6, 2016 13:58
-
-
Save ThiefMaster/7070754 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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