Background image causes the page to flash when loaded

3

I'm creating a site by loading any of the 6 pages of a category on the site that each have a background image averaging 40-70kb, the area where the image is flashing. The page always blinks when you click or refresh the page, what can I do?

    
asked by anonymous 27.06.2014 / 22:43

2 answers

4

The image itself is not flashing. What happens is that due to the high number of requests your page requires to be made, there is a slight delay between the time the page is served and the contents arrive, more properly the image.

Delay is small enough to cause the impression to blink, but that is not the case.

If you have a slow connection you will be able to observe that the page is indeed served with a white area where the image should be and soon after it appears.

Solution

Reduces the number of requests you are making at the scripts level, either at the style level or at the image level:

  • CSS

    All CSS files can be downloaded in a single browser request:

    Example in PHP

       
    <?php
    header('Content-type: text/css; charset=UTF-8');
    
    ob_start("compress");
    
    function compress($buffer) {
    
      /* remove comentários */
      $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
    
      /* remove tabulaçõess, espaços, quebras de linhas */
      $buffer = str_replace(array("\r\n", "\r", "\n", "\t", '  ', '    ', '    '), '', $buffer);
    
      return $buffer;
    }
    
    $cssFiles = realpath(dirname(__FILE__)).'/*.css';
    
    foreach(glob($cssFiles) as $file) {
      include($file);
    }
    
    ob_end_flush();
    ?>
    

    With the example above, you can have a PHP file inside your folder where you have the various CSS and instead of calling all the .CSS on the site, you call only the PHP file:

       
    <link type="text/css" media="screen" rel="stylesheet" href="http://meusite.com/css/css.php">
    
      

    For your particular case, you spend 4 requests for 1 only.

  • JS

    The example given for CSS can also be applied to script files:

    Example in PHP

       
    <?php
    header('Content-Type: text/javascript; charset=UTF-8');
    
    ob_start();
    
    $files = realpath(dirname(__FILE__)).'/*.js';
    
    foreach(glob($files) as $file) {
      include($file);
    }
    
    ob_end_flush();
    ?>
    

    With the example above, you can have a PHP file inside your folder where you have the various scripts and instead of calling all .JS on the site, you call only the PHP file: / p>    

    <script src="http://meusite.com/js/js.php"type="text/javascript">
    
      

    For your particular case, you spend 4 requests for 1 only.

  • Images

    You have a lot of images to display on the same page, but that does not mean they have to be separate files.

    Using CSS sprites, you can have an image composed of several small images that are later positioned in the appropriate place through CSS.

    As an example, this part of your header:

    Itcouldbeasinglefile:

    One of many websites that generates this kind of images so you do not have to do much.

      

    For your particular case, you spend 8 requests for 1 only.

  • CDNs

    You can use CDNs to increase the number of concurrent requests ( requests ) that browsers do because the lower limit that is affecting your page is the limit imposed on the number of files downloaded simultaneously by each address.

    Sample Bootstrap Twitter CDN you are using:

       
    <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
    

    With this address, the base CSS for Twitter Bootstrap can be downloaded simultaneously with other files on the server where your web site is located, thus speeding up the entire page loading process.

  • With the tips above you'll be able to:

    • Reduce the number of requests currently from 37 to 24;
    • Compress CSS, thus contributing to more effective page loading;
    • The browser downloads more files at the same time, which will allow for a lower page availability time;
    • Finish the image blinking.
    28.06.2014 / 02:52
    0

    What is happening is normal. You are making a request and the entire page is sent and a whole page is returned. Ideally, you should use Ajax calls and send and receive just what you need. Background for example would not change, so it would not "wink."

        
    28.06.2014 / 08:26