dimsemenov / PhotoSwipe

JavaScript image gallery for mobile and desktop, modular, framework independent
http://photoswipe.com
MIT License
24.18k stars 3.31k forks source link

Open gallery on hashchange / Open during closing transition #769

Open MGotink opened 9 years ago

MGotink commented 9 years ago

I want to show the gallery when the user uses the forward button in his browser and that page has the gallery hash in the URL.

I do this by binding to the window.onhashchange event. This works fine as long as the user doesn't click forward quickly after clicking back. In that case the destroy seems to be called when the user might already be on the new page where the gallery is starting to show, resulting in a hidden gallery/half gallery.

I've updated the photoswipe demo site to show the issue: https://github.com/MGotink/PhotoSwipe/tree/onhashchange You probably need to use the keyboard for back/forward or set the animation durations slower to see the issue.

Simply calling destroy/close if the gallery is opened introduces new errors. Am I missing something or is this not supported?

dimsemenov commented 9 years ago

Well, listen for PhotoSwipe close event. After it's triggered, add some flag that PhotoSwipe is closing. After destroy event is triggered, remove the flag.

If flag is on and URL changed "back"- delay the openining of PhotoSwipe for 500ms (or whatever duration for closing animation you use) using setTimeout.

Everything related to changing URL is in history module.

MGotink commented 9 years ago

It would be nice if we could abort all timeouts/animations so the gallery can show directly. The user perception will also be better than just delaying displaying of the gallery.

dimsemenov commented 9 years ago

I agree, I'll see what I can do...