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 do i initialize more galleries on one page with one selector and one options? #191

Closed picarica closed 3 years ago

picarica commented 3 years ago

so i am making a website and user can use how many galleries on his website, the problem is when they all have same ID then script in the footer inicializes only the first instance of gallery, how can i change this behavior ?

                  ```
                    echo "<div id='image_gallery$aa' class='image_gallery alter'>";
        foreach ($rep->r_gallery as $image) {
            $options = array('quality' => 80, 'upscaling' => true, 'cropping' => 'north', 'sharpening'=>'medium');
            $large = $image->size(1200, 0, $options);
            $thumb = $image->size(400, 350, $options);
            echo "
            <a href='$large->url' class='hvr-underline-from-center'>
                <img src='$thumb->url'>
            </a>
            ";
        }

because right i gotta  copy paste the same code a inicialize it one by one i wanted to overlook this issue byt just echoing the javascript in the middle of the code 
this didnt worked as expected
                     echo "<script>
        lightGallery(document.getElementById('image_gallery$aa'), {
            /* mobile options */
            controls: true,
            videoMaxWidth: '70%',
            /* end mobile options */
            mode: 'lg-slide',
            loop: false,
            hideControlOnEnd:  true,
            preload: 4,
            height: '100%',
            fullScreen: true,
            autoplayFirstVideo: false,
            youtubePlayerParams: { autoplay: 0 }
        }); </script>";
for example fresco gallery just iniciliazes all galeries in one page but keeps them seperated by using  identifier see example

<a href="image1.jpg" class="fresco" data-fresco-group="unique_name">Image 1 <a href="image2.jpg" class="fresco" data-fresco-group="unique_name">Image 2


soo yeh how is this achievable with lightgallery
sachinchoolur commented 3 years ago

You can use class instead of id

var elements = document.getElementsByClassName('image_gallery');
for (let item of elements) {
    lightGallery(item, {
      share:false
    })
}

Demo - https://codepen.io/lightGallery/pen/gOLLWXL