Photoswipe: start index option not working

Created on 7 Mar 2016  路  17Comments  路  Source: dimsemenov/PhotoSwipe

Hi I'm having an issue right now, plugin is working as expected but when I try to initialize the gallery with options.index different to zero the gallery always start at zero anyway. I dont know if I missing something

Most helpful comment

It seems for me that PhotoSwipe crashes because it hasn't loaded the image requested ( by goTo() ) yet. Setting the startIndex before loading the PhotoSwipe Object solved the problem.

options.index = index; gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, options); gallery.init();

All 17 comments

Also if I use goTo(index) the image is not rendered

goTo method works but image is not displayed

Im also getting this error while using goTo method
Cannot read property 'center' of undefined I just read another post about ensuring the index value is an integer and I already did that

Hey. Did you ever figure this out? I have the same issue too. If I hardcode the index of 2 as an example, then it is fine and goTo works. If I pass in a variable, it fails.

Hopefully you have found a fix. I'm going to go through the code and see if I can spot anything.

Cheers

Hi, make sure you're setting index property to an integer and not a string - if you use string, strange things happen ;)

thanks @chesio, Yeah, I am already pasrseInt(myVar, 10) on it, but its not playing. Very odd.

Cheers

Do you get any errors in browser console?

Sadly not. I've dropped it in a Cordova app. All is working perfectly except this little issue. I think it is the way it is initiated that is causing the issue. I'll keep playing and report back if I find the issue. Thanks again.

Same for me - calling goTo on the gallery directly after initializing it breaks rendering of images sometimes.
Calling it something like 500ms later works.
Would be happy if this was fixed.

Please provide a reduced test case (isolated example) that reproduces the problem.

You may create it based on CodePens in PhotoSwipe documentation: raw gallery, gallery with thumbnails, custom HTML content in slides. If you're not comfortable with CodePen, you may export code from it via Export -> Export .zip, and upload test case to your server or use any other online service like JSFiddle or JSBin.

@robertogonzalezdelarosa can this be closed?

Still an issue as far as I know.
What works however is directly specifying the index in the options when initializing PhotoSwipe

I'm not sure if it will help for you, but for me using parseInt(itemIndex,10) did the trick.
gallery.init(); gallery.goto(parseInt(index,10));
Also, set the index for items array too.

Experiencing this issue as well.

@brmendez And now 500+ people know about it. Please use the emojis when indicating such as thing so all watchers aren't notified by email. Thanks.

Well that's annoying and good to know. Thanks for bringing attention to it. Probably could have done without the first sentence though. I hope the frustrating bug you are currently working on brings you great reward once you squash it! Either way, understood!

It seems for me that PhotoSwipe crashes because it hasn't loaded the image requested ( by goTo() ) yet. Setting the startIndex before loading the PhotoSwipe Object solved the problem.

options.index = index; gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, options); gallery.init();

It seems for me that PhotoSwipe crashes because it hasn't loaded the image requested ( by goTo() ) yet. Setting the startIndex before loading the PhotoSwipe Object solved the problem.

options.index = index; gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, options); gallery.init();

Ok guys, for the very first time I tried to fix this issue and yes Samsonsam, you are very helpfuly right, we must set the startIndex but that's a part of the solution... I did what you said but I did it after my gallery.init() and this is the other part that made the workaround for me to go nice ... I passed the same starter index to the options index directly and boom made the trick for me ... so it would be something like

var options = {
      // optionName: 'option value'
      // for example:
      index: my_index,
          ... etc other options ...
       }

gallery = new PhotoSwipe( pswpElement, PhotoSwipeUI_Default, items, options);
gallery.init();
options.index = my_index;

So, that's it... I did that and it's working good.

Thanks, hope it helps others in the future. =)

Was this page helpful?
0 / 5 - 0 ratings