Closed scottcharlesworth closed 3 years ago
This issue has been automatically marked as stale because it has not had recent activity. If the issue is still valid for version 2.x, please re-open. Apologize for not responding on time. Thank you for your contributions.
Sometimes it is necessary to link to a specific slide from elsewhere in the page, not just from the slide itself. For example if you had a gallery with 10 pictures and a video, a link with the text of "view video" would need to open the gallery directly on the video, not at the default index.
The slide() method from the documentation only works after lightgallery has initialised, and therefore isn't suitable.
It is possible to get the gallery open to a slide by executing:
window.lgData[el.getAttribute('lg-uid')].index = 2;
window.lgData[el.getAttribute('lg-uid')].build(2);
However this doesn't select the current slide in the thumbnail plugin correctly, nor does it trigger the onBeforeOpen event.By moving the code from the item "click" event to it's own method we can then call it directly to open to a specific slide, for example:
window.lgData[el.getAttribute('lg-uid')].openSlide(2)
As well as slide (index) there are 2 other parameters on the method to define:
I have set default parameter values for what I think will be the majority use case. I have also left the triggering of event "onBeforeOpen" to be before the setting of the index property, to prevent breaking changes to anyone's listeners.