2015-07-03 93 views
0

我有以下代码如何在关闭fancybox时停止页面重定向?

<script type="text/javascript"> 
     jQuery(document).ready(function() { 
      beginCartnotification(); 
     }); 
     setTimeout(function() { 
      window.location.href = "http://www.someurl.com"; 
     }, 5000); 
    </script> 


<script type="text/javascript"> 
     jQuery.fancybox({ 
      'width': '300', 
      'height': '90', 
      'padding': '40px', 
      'autoDimensions': false, 
      'border': '1px solid #005195', 
      'autoScale': false, 
      'transitionIn': 'fade', 
      'transitionOut': 'fade', 
      'showCloseButton': true, 
      'type': 'inline', 
      'href': '#cartnotification-popup', 
      'onClosed': function(){ //perform some task on closing the fancybox.} 
     }); 
    } 
</script> 

现在我想要做的是,如果对看中框中关闭按钮用户点击,页面不应该重定向。

如果用户在5秒后没有点击关闭按钮,用户应该被重定向到另一个页面。

我该怎么实现里面关闭函数?

http://fancybox.net/api

回答

2

只需添加一个setTimout的fancyboxonComplete财产,一旦显示的内容将被调用。如果在关闭fancybox之前达到超时,请重新定向。

onClose属性函数中清除超时值,以确保window.loaction更改不会被触发。

同时将fancybox设置添加到jQuery的.ready()方法中,以确保在文档准备就绪后立即进行初始化。

<script type="text/javascript"> 
jQuery(document).ready(function() { 

    beginCartnotification(); 

    // ADD A VAR TO HOLD YOUR TIMER. 
    var timer; 

    jQuery.fancybox({ 
    'width': '300', 
    'height': '90', 
    'padding': '40px', 
    'autoDimensions': false, 
    'border': '1px solid #005195', 
    'autoScale': false, 
    'transitionIn': 'fade', 
    'transitionOut': 'fade', 
    'showCloseButton': true, 
    'type': 'inline', 
    'href': '#cartnotification-popup', 
    'onComplete': function() { 
     timer = setTimeout(function() { 
     // REDIRECTING TAKES PLACE AFTER 5 SEC. 
     window.location.href = "http://www.someurl.com"; 
     }, 5000); 
    }, 
    'onClosed': function() { 
     // CLEAR THE TIMEOUT !!! 
     clearTimeout(timer); 
    } 
    }); 

}); 
</script> 
+0

加一,不过,我想保持'。就绪()'方法中的fancybox初始化脚本。另外请注意,您的代码末尾可能会出现杂乱的右括号'}' – JFK

+0

@JFK绝对正确。 Thx编辑了这篇文章。 – DavidDomain

相关问题