dimsemenov / PhotoSwipe

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

Scroll last element into view when zoom out #2099

Open josobrate opened 2 months ago

josobrate commented 2 months ago

Is it possible to scroll the last element into view when zooming out?

This would be helpful in a large gallery.

e. g. if you swipe through 50 of 100 images and zoom out, you have to start from the beginning or scroll through the images and find the last one you've viewed.

Scrolling the last element into view when zooming out would help very much.

dimsemenov commented 2 months ago

Yes, you can do something like:

pswp.on('close', () => {
  if (pswp.currSlide.data.element) pswp.currSlide.data.element.scrollIntoView();
});
josobrate commented 2 months ago

Works like a charm, thank you so much for your quick answer <3

noisefloordev commented 1 month ago

This is what I'm using:

        this.photoSwipe.on("change", () => {
            let pswp = this.photoSwipe.pswp;
            if(pswp.currSlide && !this.photoSwipe.pswp.opener.isOpening)
                pswp.currSlide.data.element.scrollIntoView({ behavior: "instant" });
        });

Doing it in change makes sure it's ready before the gallery becomes visible, and the isOpening check is so it doesn't scroll the view when entering, only when exiting. (That's an internal interface, but simpler than tracking it myself.) "instant" makes sure it doesn't trigger a smooth scroll animation, since this is meant to happen offscreen.