2014-09-04 98 views
0

我有這個腳本,當用戶第一次訪問該網站時顯示一條消息,然後淡出,然後另一段在短暫延遲後消失。是否可以延遲window.load?

$(window).load(function() { 
    $(".greeting").delay(1500).fadeOut("500"); 
    $('.content').delay(2500).animate({opacity: '1'}, 500); 
}); 

HTML/PHP(WordPress的)

<div class="greeting"> 
    <div class="greeting-inner"> 
    <img id="greeting-img" src=""> 
    </div> 
</div> 
<div class="wrap"> 
    <header class="header"> 
    <div class="logo"> 
    <h1><a href="<?php echo home_url(); ?>/"><img src="<?php echo get_stylesheet_directory_uri(); ?>/img/logo.svg" onerror="this.src=<?php echo get_stylesheet_directory_uri(); ?>/img/logo.png;this.onerror=null;" alt="<?php bloginfo('name'); ?>"></a></h1> 
    </div> 
    </header> 
    <nav class="nav"> 
    <?php wp_nav_menu(array( 
    'theme_location' => 'main-nav', 
    'container' => false, 
    'items_wrap' => '<ul>%3$s</li>',));?> 
    </nav> 
<section class="content" role="main"> 
// Website content 

基本上我有當頁面加載其中火其他腳本,但內容其實沒有得到顯示在2500ms的,所以一切在2500ms是晚期。有沒有辦法延遲窗口負載,所以這不會發生?

+0

試過'setTimeout();'? – 2014-09-04 15:37:54

+0

提供了一個小提琴請:) – 2014-09-04 15:37:55

+0

http://jsfiddle.net/yaL3Lwwo/我不得不使用document.ready不window.load由於某種原因它不會觸發? – Jordan 2014-09-04 15:45:45

回答

0

您可以使用setTimeout的,如果你是完全地肯定的時機:

setTimeout(function() { 
    ... 
},"2500"); 

,但它可能是更好的等待腳本加載,所以加一:在

$(function(){ 
//do stuff 
}); 

腳本

0

不可以延遲窗口load(),因爲它是由瀏覽器觸發的。在完成animate()之後,您必須設置「其他腳本」才能執行。

function MyFunction() { //Just an example, of course 
    //other scripts 
} 

$(window).load(function() { 
    $(".greeting").delay(1500).fadeOut("500"); 

    //The last parameter is the callback that is fired after the animation 
    $('.content').delay(2500).animate({opacity: '1'}, 500, MyFunction); 
}); 
0

窗口加載事件在完整加載完整頁面時執行,包括所有框架,對象和圖像。那麼2500毫秒的來源究竟是什麼?如果您的其他腳本正在處理內容,那麼您所要做的就是在完成運行後,從其他腳本內部調用上述兩行代碼。

查看更多信息http://4loc.wordpress.com/2009/04/28/documentready-vs-windowload/

相關問題