2010-06-10 78 views
1

这里是我到目前为止,但它加载的时候了:如何在页面加载5秒后显示Colorbox?

<script type="text/javascript"> 
var pid = <?php echo $_GET['id']; ?>; 
var rlink = "<?php echo $domain; ?>share.php?pid=" + pid; 
$(document).ready(function(){ 
    setTimeout($(".url_tag_small").colorbox({ href: rlink, width: "620px", height: "354px", opacity: 0.0, transition: "none", initialWidth: "620px", initialHeight: "354px", iframe: true, title: true, open: true}), 5000); 
}); 
</script> 

我在做什么错?

回答

3

它在setTimeout调用初始化时执行您的回调,而不是在定时器关闭时执行。更改为:

var pid = <?php echo $_GET['id']; ?>, 
    rlink = "<?php echo $domain; ?>share.php?pid=" + pid; 

$(document).ready(function(){ 
    setTimeout(function(){ 
     $(".url_tag_small").colorbox({ 
     href: rlink, 
     width: "620px", 
     height: "354px", 
     opacity: 0.0, 
     transition: "none", 
     initialWidth: "620px", 
     initialHeight: "354px", 
     iframe: true, 
     title: true, 
     open: true 
     }) 
    }, 5000); 
}); 
+0

+1 - 与我的回答相同,但更适合jQuery风格。 – 2010-06-10 14:39:06

+0

工作完美!谢谢! – mike 2010-06-10 15:00:15

+0

@mike肯定的事情!你有99%,只需要额外的功能。 – 2010-06-10 15:08:00

1

您在调用setTimeout时调用函数,而不是将它传递给将打开colorbox的函数的引用。

$(document).ready(function(){ 
    setTimeout(function() { 
     $(".url_tag_small").colorbox({ href: rlink, width: "620px", height: "354px", opacity: 0.0, transition: "none", initialWidth: "620px", initialHeight: "354px", iframe: true, title: true, open: true}) 
    }, 5000); 

}); 

基本上,你的版本,而不是通过在函数中说,“这就是我希望你以后5000毫秒做”,被编码调用.colorbox事情调用setTimeout之前。