miromannino / Justified-Gallery

Javascript library to help creating high quality justified galleries of images. Used by thousands of websites as well as the photography community 500px.
http://miromannino.github.io/Justified-Gallery/
MIT License
1.68k stars 299 forks source link

justifiedGallery + lightGallery, several galleries, only first works. #222

Closed aitormendez closed 7 years ago

aitormendez commented 7 years ago

Just first gallery open the lightbox. Following galleries break the page.

This is the HTML markup:

<div class="gallery">
     <a href="img/img1.jpg">
         <img src="img/thumb1.jpg" />
     </a>
     <a href="img/img2.jpg">
         <img src="img/thumb2.jpg" />
     </a>
</div>
<div class="gallery">
     <a href="img/img1.jpg">
         <img src="img/thumb1.jpg" />
     </a>
     <a href="img/img2.jpg">
         <img src="img/thumb2.jpg" />
     </a>
</div>

This is the init:

  $(document).ready(function() {
      $('.gallery').justifiedGallery({
        lastRow : 'nojustify',
        rowHeight : 100,
        margins : 3
      }).on('jg.complete', function () {
        $(".gallery").lightGallery({
          thumbnail:true
        });
      });
    });

Is it possible to get woking all galleries in a page with a generic selector (".gallery")?

aitormendez commented 7 years ago

UPDATE: This works for two galleries:

     $('#gallery-1').justifiedGallery({
            rowHeight : 100,
            margins : 3
      }).on('jg.complete', function () {
          $('#gallery-1').lightGallery({
               thumbnail:true
          });
      });
      $('#gallery-2').justifiedGallery({
            rowHeight : 100,
            margins : 3
      }).on('jg.complete', function () {
           $('#gallery-2').lightGallery({
                thumbnail:true
           });
      });
biziclop commented 7 years ago

Remeber, jQuery event handlers receive the target object in this:

$(document).ready(function(){
  $('.gallery').justifiedGallery({
    lastRow : 'nojustify',
    rowHeight : 100,
    margins : 3
  }).on('jg.complete', function(){

    var $gallery = $(this);
    $gallery.lightGallery({
      thumbnail: true
    });

    // or just …

    $(this).lightGallery({
      thumbnail: true
    });

  });
});
aitormendez commented 7 years ago

Thank you for the help!

I got "this.lightGallery is not a function" error.

biziclop commented 7 years ago

Sorry, I forgot to wrap this to $(this), see updated answer above.

aitormendez commented 7 years ago

I noticed. I set the jQuery selector on my own :) but error persists.

aitormendez commented 7 years ago

Sorry! It works. I had a typo error.

Thank you so much for the help, really!