2012-02-25 200 views
1

我有一个测试网站(index.html),打开一个弹出窗口(popup.html),并在三秒钟后关闭它。下面是index.html的头:如何在x秒后自动关闭fancybox/lightbox/... popup?

<script language="javascript" type="text/javascript"> 
function popup(url) { 
    newwindow=window.open(url,'name','height=70,width=300'); 
    if (window.focus) {newwindow.focus()} 
    return false; 
} 
</script> 

这里的index.html的身体:

<a href="#" onClick="return popup('popup.html')">Open in pop-up</a> 

这里的popup.html的头:

<script> 
    var howLong = 3000; 
    t = null; 
    function closeMe(){ 
     t = setTimeout("self.close()",howLong); 
    } 
</script> 

这里的弹出的身体.html:

<body onLoad="closeMe();self.focus()"> 
<p>Closing in 3 seconds</p> 
</body> 

我想使用linghtbox/fancybox /任何...显示该弹出窗口,并在3秒后再次关闭它。我该怎么做?我尝试了各种各样的东西,没有任何工作。实现这个最简单的方法是什么?

在此先感谢!

+0

的fancybox等灯箱有一个被称为开放的回调函数,以及一个公共的方法来关闭它,你尝试过触发回调函数内关闭方法(带超时)? – mariogl 2012-02-25 12:17:02

+1

检查[documentation](http://fancybox.net/api)。 $ .fancybox.close();与setTimeout应该足够你。 – 2012-02-25 12:40:10

回答

7

您可以使用Fancybox(version 1.3.xversion 2.x)打开您的外部文件html并在几秒钟后关闭它。

有关的fancybox v1.3.x,你的HTML应该是这样的:

<a href="page.html" class="fancybox">open fancybox and close it automatically after 3 seconds</a> 

和脚本:

$(document).ready(function() { 
$(".fancybox").fancybox({ 
    'width': 640, // or whatever 
    'height': 320, 
    'type': 'iframe', 
    'onComplete': function(){ 
    setTimeout(function() {$.fancybox.close(); },3000); // 3000 = 3 secs 
    } 
}); 
}); // ready 

有关的fancybox V2.X,在html(公告class):

<a href="page.html" class="fancybox fancybox.iframe">open fancybox and close it automatically after 3 seconds</a> 

和脚本:

$(document).ready(function() { 
$(".fancybox").fancybox({ 
    width: 640, // or whatever 
    height: 320, 
    afterLoad: function(){ 
    setTimeout(function() {$.fancybox.close(); },3000); // 3000 = 3 secs 
    } 
}); 
}); // ready 
0

的onComplete或后负荷是不适合我的工作,所以我下面的代码添加父页面

window.closeFancyBox = function() { 
     $.fancybox.close(); 
}; 

,并从弹出页面,我呼吁下面的代码上

$(window).load(function(){ setTimeout(function() {window.parent.closeFancyBox(); },3000); // 3000 = 3 secs });

0

创建一个函数关闭所有fancybox afterload并为该函数设置超时。

$(document).ready(function() { 
    closefancybox(); 
}); 

function closefancybox() { 
    $.fancybox({ 
     afterLoad: function() {$.fancybox.close();} 
    }); 
    setTimeout(function() { closefancybox(); }, <time_inter>); 
}