lightGallery icon indicating copy to clipboard operation
lightGallery copied to clipboard

[v2, hash] navigate to different slides via browser back/forward buttons not working

Open miedzikd opened this issue 3 years ago • 11 comments

Description

Navigate to different slides via browser back/forward buttons not working When the lgHash plugin is enabled, changing slides creates new hashes, but pressing the back/forward buttons does not change the slides or closes the gallery, but completely reloads the page

Steps to reproduce

  1. Enable hash plugin
  2. Open gallery
  3. Click forward 3 times
  4. Click browser back button

Demo: https://www.lightgalleryjs.com/demos/hash/

Expected result

You should see the previous slide, not close the gallery. On the other hand, when you pressed back on the first slide, the gallery should close but the page should not reload

Actual result

Page is reload

Additional context

Love your work and version 2 ;)

miedzikd avatar May 21 '21 13:05 miedzikd

The primary role of the hash plugin is to provide an option to share slides via URL. We already have multiple options for navigating between slides and closing the gallery. If you really want to navigate slides via the browser back button, for now, you can fork the repo and replace the replaceState with pushState.

Let me know what you think

sachinchoolur avatar May 21 '21 14:05 sachinchoolur

Thanks @sachinchoolur for the explanations and tips :)

I don't think it's important for desktops ... but for mobile devices ... opening the gallery looks like going to a new page. Sometimes it is easier than looking for the X (which disappears after a few seconds) it is easier to make "undo gesture" or click the "undo arrow". Then, however, the user lands in a completely different place. Especially if he just came from Google to the SPA website ... In this case you will return to the search results.

Please consider this one more time... Mabe you can add a new option for hash plugin?

Best Regards :) Daniel

miedzikd avatar May 25 '21 06:05 miedzikd

I think this is very important especially for mobile and SPAs like @miedzikd said, but I'm sure there are many use cases where this would be useful.

pbotas avatar Jun 12 '21 08:06 pbotas

I'll try to add a new option in the hash plugin as @miedzikd suggested.

sachinchoolur avatar Jun 13 '21 06:06 sachinchoolur

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Aug 12 '21 06:08 stale[bot]

any news @sachinchoolur ?

miedzikd avatar Aug 16 '21 11:08 miedzikd

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Oct 15 '21 13:10 stale[bot]

any news @sachinchoolur ?

miedzikd avatar Oct 15 '21 15:10 miedzikd

Any updates on this one?

petko avatar Dec 12 '22 14:12 petko

I don't really know what I'm doing here but I wanted the back button (especially on mobiles) to close the gallery and not actually go back to the previous page and here's what I added to my code.

Before initializing LightGallery:

myGalleryElement.addEventListener('lgBeforeOpen', () => { window.history.pushState('#lgopen', 'Gallery opened'); });

The 2 arguments of pushState() can be whatever you want because they're not really used, except to trick the browser in thinking there's another entry in history. As I said, this may not be a good option or the right way to do it but I don't know JS and it seems to do what I want.

Hope it helps.

peamak avatar Jan 21 '24 12:01 peamak

I don't really know what I'm doing here but I wanted the back button (especially on mobiles) to close the gallery and not actually go back to the previous page and here's what I added to my code.

Before initializing LightGallery:

myGalleryElement.addEventListener('lgBeforeOpen', () => { window.history.pushState('#lgopen', 'Gallery opened'); });

The 2 arguments of pushState() can be whatever you want because they're not really used, except to trick the browser in thinking there's another entry in history. As I said, this may not be a good option or the right way to do it but I don't know JS and it seems to do what I want.

Hope it helps.

This worked wonders! Thanks! I hope there's a solution to this in the future because the docs specifically say

It allows you to navigate to different slides via browser back/forward buttons too. (source)

And it doesn't work either.

irian-codes avatar Mar 16 '24 17:03 irian-codes