简体   繁体   English

是否可以延迟window.load?

[英]Is it possible to delay window.load?

I've got this script which shows a message when the user first goes to the website, then it fades out, then another section fades in after a short delay. 我有这个脚本,该脚本在用户第一次访问该网站时显示一条消息,然后淡出,然后在短暂的延迟后淡出另一个部分。

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

HTML/PHP (WordPress) 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

Basically I have other scripts which fire when the page has loaded, but the content actually doesn't get shown for 2500ms so everything is 2500ms late. 基本上,我还有其他脚本会在页面加载后触发,但是实际上2500ms不会显示内容,所以一切都晚了2500ms。 Is there a way to delay the window load so this won't happen? 有没有办法延迟窗口加载,这样就不会发生?

You can use setTimeout if you are completly sure of the timing: 如果完全确定时间,可以使用setTimeout:

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

But It could be better to wait for the script to load, so add a: 但是最好等待脚本加载,因此添加:

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

on the scripts 在剧本上

No. You cannot delay the window load() , for it is fired by the browser. 不能。您不能延迟window load() ,因为它是由浏览器触发的。 You would have to set your "other scripts" to be executed after your animate() finishes. 您必须设置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);
});

The window load event executes when the complete page is fully loaded, including all frames, objects and images. 整个页面(包括所有框架,对象和图像)完全加载后,将执行窗口加载事件。 So what is the source of the 2500ms exactly? 那么2500ms的确切来源是什么? If it is your other scripts doing stuff with the content then all you have to do is call your 2 lines of code above from inside your other scripts, after they are done running. 如果您的其他脚本正在处理内容,那么您要做的就是在其他脚本运行完毕后,从您的其他脚本内部调用上面的两行代码。

See here for more info http://4loc.wordpress.com/2009/04/28/documentready-vs-windowload/ 详情请参阅此处http://4loc.wordpress.com/2009/04/28/documentready-vs-windowload/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM