Lightgallery哈希插件中的多个画廊?

时间:2015-09-03 13:31:01

标签: javascript jquery html gallery lightgallery

我无法运行插件Lightgallery哈希。我希望在同一页面上有两个画廊,点击一个按钮应该显示另一个。如果我没弄错,插件应该这样做。

我添加了这个HTML结构:

<div id="hash">
<a href="img/img_port/reportage/img-1.jpg">
    <img src="img/img_port/reportage/thumb-1.jpg" />
</a>
<a href="img/img_port/reportage/img-2.jpg">
    <img src="img/img_port/reportage/thumb-2.jpg" />
</a>
<a href="img/img_port/reportage/img-3.jpg">
    <img src="img/img_port/reportage/thumb-3.jpg" />
</a>
<a href="img/img_port/reportage/img-4.jpg">
    <img src="img/img_port/reportage/thumb-4.jpg" />
</a>

<div id="hash2">   
<a href="img/img_port/reportage/img-1.jpg">
    <img src="img/img_port/reportage/thumb-1.jpg" />
</a>
<a href="img/img_port/reportage/img-2.jpg">
    <img src="img/img_port/reportage/thumb-2.jpg" />
</a>
<a href="img/img_port/reportage/img-3.jpg">
    <img src="img/img_port/reportage/thumb-3.jpg" />
</a>
<a href="img/img_port/reportage/img-4.jpg">
    <img src="img/img_port/reportage/thumb-4.jpg" />
</a>

这个Javascript:

<script type="text/javascript">
        $(document).ready(function() {
            $("#hash").lightGallery({
                download: false,
                counter: false,
                hash: true;
                galleryId: 1
                });
            $("#hash2").lightGallery({
                download: false,
                counter: false,
                hash: true;
                galleryId: 2
                    });
            });
    </script>

这是正确的还是有问题?

2 个答案:

答案 0 :(得分:1)

<script type="text/javascript">
    $(document).ready(function() {
        $("#hash").lightGallery({
            download: false,
            counter: false,
            hash: true,  /*Here is the error.*/
            galleryId: 1
            });
        $("#hash2").lightGallery({
            download: false,
            counter: false,
            hash: true,/*Here is the error.*/
            galleryId: 2
                });
        });
</script>

答案 1 :(得分:0)

首先,您应该通过类而不是ID来标识画廊。

<div class="lightgallery">
     <a href="img/img_port/reportage/img-1.jpg">
         <img src="img/img_port/reportage/thumb-1.jpg" />
     </a>
     <a href="img/img_port/reportage/img-2.jpg">
         <img src="img/img_port/reportage/thumb-2.jpg" />
     </a>
</div>

<div class="lightgallery">   
     <a href="img/img_port/reportage/img-1.jpg">
         <img src="img/img_port/reportage/thumb-1.jpg" />
     </a>
     <a href="img/img_port/reportage/img-2.jpg">
         <img src="img/img_port/reportage/thumb-2.jpg" />
     </a>
</div>

然后在javascript中,您将获得该类的所有元素的引用

var galleries = document.getElementsByClassName('lightgallery');

然后循环它们中的每一个并启动

for(let i = 0 ; i < galleries.length; i++){
      lightGallery(galleries[i],{
          thumbnail:true,
          share: false,
          download: false,
      })
  }