sachinchoolur / lightgallery.js

Full featured JavaScript image & video gallery. No dependencies
https://sachinchoolur.github.io/lightgallery.js/
Other
5.3k stars 596 forks source link

How can I programmatically open the gallery? #133

Closed dyegonery closed 3 years ago

dyegonery commented 5 years ago

I tried running the slide() method to open the gallery programmatically, but it doesn't seem to work. Here's my code: let el = document.getElementById('lightgallery'); window.lgData[el.getAttribute('lg-uid')].slide(1);

Is there any other way to accomplish this?

michaelmolino commented 4 years ago

For others that might come across this, I have it mostly working by placing the .slide() call inside an event handler so it only fires after the gallery has openend. But there is a bug that the slide direction is wrong for the first arrow press :(. Will look into that later.

lg.addEventListener(
        'onAfterOpen',
        function() {
          window.lgData[lg.getAttribute('lg-uid')].slide(2);
        },
        false
      );
michaelmolino commented 4 years ago

This is the final version and it fixes the issue I had with the slide direction being wrong.

      lg.addEventListener(
        'onAfterOpen',
        function() {
          window.lgData[lg.getAttribute('lg-uid')].slide(Number(slide));
          window.lgData[lg.getAttribute('lg-uid')].index = Number(slide);
        },
        {
          once: true
        }
      );
189 commented 3 years ago

You can try

let el = document.getElementById('lightgallery');
lightGallery(el);
el.firstElementChild.click()
sachinchoolur commented 3 years ago

You can use dynamic mode to programmatically open the gallery and use lightGallery slide public method to go to a specific slide Dynamic mode demo - https://sachinchoolur.github.io/lightgallery.js/demos/dynamic.html Public method docs - https://sachinchoolur.github.io/lightgallery.js/docs/api.html#methods Public method demo - https://sachinchoolur.github.io/lightgallery.js/demos/methods.html

Please let me know if you have any questions