简体   繁体   English

通过单击外部内容区域来关闭GLightbox画廊

[英]Close GLightbox gallery by clicking outside content area

I'm using the GLightbox JS library for a portfolio gallery and want to be able to close the gallery when clicking outside the inner element. 我正在将GLightbox JS库用于项目组合库,并希望在单击内部元素外部时能够关闭库。

I found other questions of a similar nature, but I'm avoiding jQuery and haven't been able to find a solution that works for this specific use case. 我发现了其他类似性质的问题,但我避免使用jQuery,但未能找到适用于此特定用例的解决方案。 Plus it looks like the functionality I want is built in. I'm just trying to access it. 另外,它看起来像我想要的功能是内置的。我只是在尝试访问它。

The documentation lists a number of options, and includes what appears to be a function for closing the gallery with something other than the default button. 该文档列出了许多选项,并包括使用默认按钮以外的功能关闭图库的功能。

Their documentation is here: http://glightbox.mcstudios.com.mx/ 他们的文档在这里: http : //glightbox.mcstudios.com.mx/

The markup for the gallery is generated on the fly, and follows this structure: 画廊的标记是即时生成的,并遵循以下结构:

<div class="goverlay"></div><!-- background overlay -->
<div class="gcontainer"><!-- main container -->
  <div id="lightbox-slider">
    <div class="gslide">
      <div class="gslide-inner-content"><!-- image/text content -->
        <img src="img/image.jpg" alt="">
      </div>
    </div>
  </div>
</div>

Based on their documentation there is a method to trigger closing the gallery listed toward the bottom of the page: 根据他们的文档,有一种方法可以触发关闭页面底部列出的画廊:

// Close the lightbox
myLightbox.close();

I want to be able to click outside of .gslide-inner-content to close the gallery, and this is the basic idea I've come up with so far: 我希望能够在.gslide-inner-content之外单击以关闭画廊,这是到目前为止我想到的基本思想:

var closeTheGallery = document.getElementsByClassName('.gslide');

closeTheGallery.onclick = function() {
  myLightbox.close();
  e.stopPropagation();
};

I've tried several iterations of the above code targeting various parent and child elements to see if there's anything I can hook into. 我已经尝试了针对各种父元素和子元素的上述代码的多次迭代,以查看是否有任何可以挂钩的内容。 So far no luck - any insights would be appreciated - thanks in advance. 到目前为止,没有任何运气-任何见解将不胜感激-在此先感谢。

The myLightbox.close(); myLightbox.close(); feature didn't seem to do anything regardless of what event or <div> I used, but Doug provided some helpful guidance. 无论我使用了什么事件或<div> ,该功能似乎都没有执行任何操作,但是Doug提供了一些有用的指导。

As a workaround, I added an event listener for a click on the .current slide container and set it to trigger another click on the default .gclose button. 作为一种解决方法,我为单击.current幻灯片容器添加了一个事件侦听器,并将其设置为触发对默认.gclose按钮的另一单击。

// find .current slide item and listen for click
// once click happens, trigger click for the close button
document.addEventListener('click', function (e) {
    if (hasClass(e.target, 'current')) {
      document.querySelector('.gclose').click();
    }
}, false);

It may not be the most elegant solution, but it gets the job done and doesn't interfere with the existing functionality of the gallery. 它可能不是最优雅的解决方案,但是它可以完成工作并且不会干扰画廊的现有功能。

Posting the code here in case other folks using the GLightbox gallery run into the same thing. 如果其他人使用GLightbox画廊碰到同样的事情,请在此处发布代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM