检查jquery揭示模态是否有效,做一些事情

我试图只在运行揭示模式的条件下运行一些javascript。 问题是,无论模态是否有效,我尝试过的方法都是真的。 下面是我想要完成的一个例子。 我试过:是可见方法,也是:主动。 我正在使用zurb揭示模态。

html:

  • some list item
  • some list item

JS:

  if ($('#myModal:active').length){ console.log('yes'); //always yes even when not revealed } else { //do nothing }  //also tried if ($('#myModal').hasClass('someClass')) 

编辑

当活动时有“可见性”的CSS:“可见”,否则“隐藏”所以我尝试了以下,但它没有响应条件。 有什么想法可能会发生吗?

 if($("#myModal").css("visibility", "hidden").length > 0) { console.log('is hidden'); } else { console.log('is shown'); } 

这是使用Zurb的揭示弹出窗口选项的cookie代码。

 if (document.cookie.indexOf('visited=true') == -1) { var tenYears = 3600 * 1000 * 24 * 365 * 10; //1000*60*60*24*15;//15days var expires = new Date((new Date()).valueOf() + tenYears); document.cookie = "visited=true;expires=" + expires.toUTCString() + ";path=/"; //encodeURIComponent + // Zurb's popup reveal modal $('#myModal').reveal({ animation: 'fadeAndPop', animationspeed: 100, closeonbackgroundclick: true, dismissmodalclass: 'close-reveal-modal' }); } 

好像这个插件在点击时激活了一个“开放”类。 所以你应该检查链接,如:

 if($(".modal-link").hasClass('open')) { //do stuff when modal active } else { //do stuff when modal not active } 

更好的是:

 if($("#my-Modal").is(":visible")) { //you get the point } 

编辑

我的漂亮答案已经过去了:active ,希望这个更简单的答案可以帮助你解决真正的问题,这基本上是我如何注册模式在页面上打开,并基于此执行一些代码?

答案是从StackOverflowpost的答案中采用的 ,也取自Foundation文档 :

 $('#myModal').reveal({ animation: 'fadeAndPop', animationspeed: 100, closeonbackgroundclick: true, dismissmodalclass: 'close-reveal-modal', opened:function(){ console.log('yes'); // magic } }); 

这个opened事件只会在…你猜对了……模态被打开,这只是他们第一次访问时才会被调用。 如果由于某种原因你需要在关闭模态时停止连续动作,只需绑定到closed

 $('#myModal').reveal({ animation: 'fadeAndPop', animationspeed: 100, closeonbackgroundclick: true, dismissmodalclass: 'close-reveal-modal', opened:function(){ console.log('yes'); // magic }, closed:function(){ // witchcraft } }); 

这应该与您所寻找的一致。