简体   繁体   English

jQuery导致“白色”加载

[英]jquery causing “white” on load

I've searched this a bit on the forum, and can't seem to figure out how to fix the common issue of white flashing when a new page loads. 我在论坛上进行了搜索,似乎无法弄清楚如何在加载新页面时解决常见的白色闪烁问题。

I've tried Chris Coyer's example of placing this above my other javascript: 我已经尝试过Chris Coyer的将其放置在其他javascript之上的示例:

    // Prevent variables from being global
(function () {

      /*
          1. Inject CSS which makes iframe invisible
      */

    var div = document.createElement('div'),
        ref = document.getElementsByTagName('base')[0] ||
              document.getElementsByTagName('script')[0];

    div.innerHTML = '&shy;<style> iframe { visibility: hidden; } </style>';

    ref.parentNode.insertBefore(div, ref);

    /*
        2. When window loads, remove that CSS,
           making iframe visible again
    */

    window.onload = function() {
        div.parentNode.removeChild(div);
    }

})();

but, still flashing. 但是,仍然闪烁。 I'm a noob to javascript, so I'm not really sure what I'm doing wrong. 我对JavaScript不熟悉,所以我不太确定自己在做什么错。

The page that is the worst is: http://thegoodgirlsnyc.com/test/new/index3_7.php 最糟糕的页面是: http : //thegoodgirlsnyc.com/test/new/index3_7.php

If you're using Jquery try wrapping your function with: 如果您使用的是Jquery,请尝试使用以下方法包装函数:

$(document).ready(function(){
 /* Code goes here */
});

This will execute your code when the document is ready (fully loaded) 当文档准备就绪(完全加载)时,这将执行您的代码

The problem is that you're executing tasks throughout the pageload. 问题在于您正在整个页面加载中执行任务。 You're getting the flashing because code is being run in-time with page load. 您正在闪烁,因为代码正在与页面加载一起及时运行。

You need to abstract the running of the code behind a unified point. 您需要在统一点后面抽象代码的运行。 This is where things like $(document).ready() are nice from jQuery. jQuery就是$(document).ready()类的地方。 It happens upon DOM completion, but before window load completion. 它发生在DOM完成之后,但在window加载完成之前。 This means it happens before visual, which prevents screen flash Javascriptiong. 这意味着它发生在视觉之前,从而防止了屏幕闪烁。

It seems like you're worried about the global namespace: 似乎您担心全局名称空间:

jQuery( function($){
    $('iframe').hide();

    $(document).ready( function(){
        $('iframe').show();
    });
});

This is an easy solution that alleviates the namespacing issue, and you won't have to refactor current code. 这是一个缓解命名空间问题的简单解决方案,并且您无需重构当前代码。 So my answer is two parts: 所以我的回答分为两个部分:

  1. Use jQuery. 使用jQuery。

  2. Use namespacing. 使用命名空间。

If you still get screen flashing using the above, you need to set the display to none in the stylesheet for the iframe element. 如果仍然使用上述方法使屏幕闪烁,则需要在iframe元素的样式表中将display设置为none

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

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