Fancybox关闭现有弹出窗口

时间:2018-09-10 12:52:28

标签: javascript fancybox fancybox-3

我正在使用Fancybox 3显示3个项目。它们都使用gallery属性链接在一起,就像这样:

<!-- Item 1 -->
<a data-fancybox="gallery" data-src="#gallery-1" href="javascript:;">
  Open Me
</a>

<div style="display: none;" id="gallery-1">
  <h2>Gallery 1</h2>
  <p>Content</p>
  <a data-fancybox data-src="signup" href="javascript:;">
</div>

<!-- Item 2 -->
<a data-fancybox="gallery" data-src="#gallery-2" href="javascript:;">
  Open Me
</a>

<div style="display: none;" id="gallery-2">
  <h2>Gallery 2</h2>
  <p>Content</p>
  <a data-fancybox data-src="signup" href="javascript:;">
</div>

<!-- Item 3 -->
<a data-fancybox="gallery" data-src="#gallery-3" href="javascript:;">
  Open Me
</a>

<div style="display: none;" id="gallery-3">
  <h2>Gallery 3</h2>
  <p>Content</p>
  <a data-fancybox data-src="#signup" href="javascript:;">
</div>

<!-- Signup -->
<div style="display: none;" id="signup">
  <h2>Signup</h2>
  <p>Sign up form goes here</h2>
</div>

每个项目中都有一个注册链接,该链接链接到一个单独的弹出窗口。单击时,这将在已打开的图库项目内打开另一个fancybox窗口。如何在触发另一个实例之前关闭前一个实例?我希望他们不筑巢。

我已经在JS中尝试过此方法,但是它不起作用:

( function( $ ) {
    $('[data-fancybox]').fancybox({
        closeExisting: true,
        loop: true
    });
} )( jQuery )

2 个答案:

答案 0 :(得分:1)

您的代码段效果很好-https://codepen.io/anon/pen/oPEXQj?editors=1010

请确保您使用的是最新版本,因为closeExisting选项从v3.4.0开始可用

顺便说一句,您应将data-src="signup"替换为data-src="#signup"以加载内联内容

答案 1 :(得分:0)

基本解决方案

您的问题的解决方案是:

<div style="display: none;" id="gallery-1">
  <h2>Title 1 - Make this screen much bigger ****************************</h2>
  <p>  <a data-fancybox data-src="#signup" onclick='$.fancybox.close()' href='#'">Sign me up</a></p>
</div>

参考:close fancy box from function from within open 'fancybox'

工作示例:

https://jsfiddle.net/euo4byj2/15/

工作代码:

<script src="//code.jquery.com/jquery-3.3.1.min.js"></script>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.4.1/jquery.fancybox.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.4.1/jquery.fancybox.min.js"></script>


<div style="display: none;" id="signup">
  <h2>SIGN UP 1</h2>
  <p>Content</p> 
</div>



<!-- Item 1 -->
<a data-fancybox="gallery" data-src="#gallery-1" href="javascript:;">
  Open Me
</a>

<div style="display: none;" id="gallery-1">
  <h2>Title 1 - Make this screen much bigger ****************************</h2>
  <p>  <a data-fancybox data-src="#signup" onclick='$.fancybox.close()' href='#'">Sign me up</a></p>
</div>

<!-- Item 2 -->
<a data-fancybox="gallery" data-src="#gallery-2" href="javascript:;">
  Open Me
</a>

<div style="display: none;" id="gallery-2">
  <h2>Title 2</h2>
  <p>Content</p>
  <a data-fancybox data-src="signup" href="javascript:;">
</div>

<!-- Item 3 -->
<a data-fancybox="gallery" data-src="#gallery-3" href="javascript:;">
  Open Me
</a>

<div style="display: none;" id="gallery-3">
  <h2>Title 3</h2>
  <p>Content</p>
  <a data-fancybox data-src="signup" href="javascript:;">
</div>

<!-- Signup -->
<div style="display: none;" id="signup">
  <h2>Signup</h2>
  <p>Sign up form goes here</h2>
</div>


<script>

/* ( function( $ ) {
    $('[data-fancybox]').fancybox({
        closeExisting: true,
        loop: true
    });
} )( jQuery );
 */

</script>