Photoswipe: Get item on beforeChange and afterChange events

Created on 21 Jul 2015  路  9Comments  路  Source: dimsemenov/PhotoSwipe

How can I get the item from the slide if either the beforeChange or afterChange event is fired?

So rather than:

pswp.listen('afterChange', function() { });

It be:

pswp.listen('afterChange', function($item) { });

Most helpful comment

pswp.currItem

All 9 comments

pswp.currItem

Is there a way to get the previous and next items as well?

pswp.items pswp.getCurrentIndex()

http://photoswipe.com/documentation/api.html

Hi, any plans to pass in item as a parameter, the current implementation makes it very hard to write unit tests. Great Library btw!!

I second that. I can't seem to get this functionality to properly work with my own code.

Note : beforeChange gives you the indexDifference as first parameter.

So if you just opened the gallery, it will be null. If you went from 0 -> 1, it will be 1.
You can get the previous index with :

         pswp.listen('beforeChange', function(indexDiff) {
                  var previousItem = (pswp.getCurrentIndex() - indexDiff);
         });

@mastef Lets assume you have 4 items. Problem with your approach is that you get invalid index (-1 instead of 3) when the current item is the first one and you are trying to access the previous one. Good news is that you don't need to do all this calculation, simply use pswp.currItem as suggested by @dimsemenov earlier. The name "beforeChange" might be confusing here as pswp.currItem already contains the "next" item.

@phoenixdev-kl You can use the logic from _getLoopedId which will transform an index like -1 to a valid one. My comment was based on fetching the previously shown slide, not the current/next one.

I think I wrote that initial comment because the parameter is undocumented and not mentioned in the docs.

@mastef Cool ... didn't know about _getLoopedId! 馃憤 And I absolutely agree that indexDiff parameter should get mentioned in the docs.

@dimsemenov I could add it to the docs, any requirements for pull requests?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

silentbugs picture silentbugs  路  5Comments

caztcha picture caztcha  路  5Comments

sumitpaul picture sumitpaul  路  4Comments

marten-seemann picture marten-seemann  路  4Comments

LockonS picture LockonS  路  5Comments