2012-02-25 83 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>); 
}