PhotoSwipe icon indicating copy to clipboard operation
PhotoSwipe copied to clipboard

Scroll last element into view when zoom out

Open josobrate opened this issue 1 year ago • 3 comments

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.

josobrate avatar May 01 '24 08:05 josobrate

Yes, you can do something like:

pswp.on('close', () => {
  if (pswp.currSlide.data.element) pswp.currSlide.data.element.scrollIntoView();
});

dimsemenov avatar May 01 '24 16:05 dimsemenov

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

josobrate avatar May 01 '24 18:05 josobrate

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.

noisefloordev avatar May 30 '24 15:05 noisefloordev