代码之家  ›  专栏  ›  技术社区  ›  Shadi Almosri

如何显示页面加载div直到页面完成加载

  •  113
  • Shadi Almosri  · 技术社区  · 16 年前

    我在我们的网站上有一个部分,加载很慢,因为它正在做一些密集的电话。
    你知道我怎么得到一个 div 说一些类似于“加载”的话,在页面准备就绪时显示,然后在一切准备就绪时消失?

    10 回复  |  直到 11 年前
        1
  •  192
  •   Eddie C.    10 年前

    我需要这个,在网上做了一些研究之后,我想到了这个(jquery需要):

    在body标记后的第一个位置添加以下内容:

    <div id="loading">
      <img id="loading-image" src="images/ajax-loader.gif" alt="Loading..." />
    </div>
    

    然后将div和图像的样式类添加到css中:

    #loading {
       width: 100%;
       height: 100%;
       top: 0;
       left: 0;
       position: fixed;
       display: block;
       opacity: 0.7;
       background-color: #fff;
       z-index: 99;
       text-align: center;
    }
    
    #loading-image {
      position: absolute;
      top: 100px;
      left: 240px;
      z-index: 100;
    }
    

    最后将这个javascript添加到您的页面中(最好在页面的末尾,在关闭之前 <body> 当然是标签):

    <script language="javascript" type="text/javascript">
         $(window).load(function() {
         $('#loading').hide();
      });
    </script>
    

    然后使用帮助样式类调整加载图像的位置和加载div的背景色。

    就这样,工作得很好但你当然要有一个 ajax-loader.gif 在某个地方。

        2
  •  27
  •   Ced    7 年前

    window.onload = function(){ document.getElementById("loading").style.display = "none" }
    #loading {width: 100%;height: 100%;top: 0px;left: 0px;position: fixed;display: block; z-index: 99}
    
    #loading-image {position: absolute;top: 40%;left: 45%;z-index: 100} 
    <div id="loading">
    <img id="loading-image" src="img/loading.gif" alt="Loading..." />
    </div>  

    最简单的页面加载图像 fadeout 在JS中创建的效果:

        3
  •  26
  •   Victor Stoddard    7 年前

    此脚本将添加一个div,在页面加载时覆盖整个窗口。它将自动显示一个只加载css的微调器。它将等待窗口(而不是文档)完成加载,然后它将等待可选的额外几秒钟。

    • 使用jquery 3(它有一个新的窗口加载事件)
    • 不需要图像,但很容易添加
    • 更改延迟以获取更多品牌或说明
    • 唯一的依赖项是jquery。

    CSS加载程序代码来自 https://projects.lukehaas.me/css-loaders

        
    $('body').append('<div style="" id="loadingDiv"><div class="loader">Loading...</div></div>');
    $(window).on('load', function(){
      setTimeout(removeLoader, 2000); //wait for page load PLUS two seconds.
    });
    function removeLoader(){
        $( "#loadingDiv" ).fadeOut(500, function() {
          // fadeOut complete. Remove the loading div
          $( "#loadingDiv" ).remove(); //makes page more lightweight 
      });  
    }
            .loader,
            .loader:after {
                border-radius: 50%;
                width: 10em;
                height: 10em;
            }
            .loader {            
                margin: 60px auto;
                font-size: 10px;
                position: relative;
                text-indent: -9999em;
                border-top: 1.1em solid rgba(255, 255, 255, 0.2);
                border-right: 1.1em solid rgba(255, 255, 255, 0.2);
                border-bottom: 1.1em solid rgba(255, 255, 255, 0.2);
                border-left: 1.1em solid #ffffff;
                -webkit-transform: translateZ(0);
                -ms-transform: translateZ(0);
                transform: translateZ(0);
                -webkit-animation: load8 1.1s infinite linear;
                animation: load8 1.1s infinite linear;
            }
            @-webkit-keyframes load8 {
                0% {
                    -webkit-transform: rotate(0deg);
                    transform: rotate(0deg);
                }
                100% {
                    -webkit-transform: rotate(360deg);
                    transform: rotate(360deg);
                }
            }
            @keyframes load8 {
                0% {
                    -webkit-transform: rotate(0deg);
                    transform: rotate(0deg);
                }
                100% {
                    -webkit-transform: rotate(360deg);
                    transform: rotate(360deg);
                }
            }
            #loadingDiv {
                position:absolute;;
                top:0;
                left:0;
                width:100%;
                height:100%;
                background-color:#000;
            }
    This script will add a div that covers the entire window as the page loads. It will show a CSS-only loading spinner automatically. It will wait until the window (not the document) finishes loading.
    
      <ul>
        <li>Works with jQuery 3, which has a new window load event</li>
        <li>No image needed but it's easy to add one</li>
        <li>Change the delay for branding or instructions</li>
        <li>Only dependency is jQuery.</li>
      </ul>
    
    Place the script below at the bottom of the body.
    
    CSS loader code from https://projects.lukehaas.me/css-loaders
    
    <!-- Place the script below at the bottom of the body -->
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        4
  •  7
  •   Roshana Pitigala Laxmansinghsodhanohdiyala    8 年前

    我有下面另一个简单的解决方案,这对我来说非常有效。

    首先,创建一个名为 洛克昂 类,该类在加载gif时是透明覆盖的,如下所示

    .LockOn {
        display: block;
        visibility: visible;
        position: absolute;
        z-index: 999;
        top: 0px;
        left: 0px;
        width: 105%;
        height: 105%;
        background-color:white;
        vertical-align:bottom;
        padding-top: 20%; 
        filter: alpha(opacity=75); 
        opacity: 0.75; 
        font-size:large;
        color:blue;
        font-style:italic;
        font-weight:400;
        background-image: url("../Common/loadingGIF.gif");
        background-repeat: no-repeat;
        background-attachment: fixed;
        background-position: center;
    }
    

    现在我们需要用这个类创建div,当页面被加载时,这个类覆盖整个页面作为覆盖

    <div id="coverScreen"  class="LockOn">
    </div>
    

    现在我们需要在页面准备就绪时隐藏此封面,以便在页面准备就绪之前限制用户单击/触发任何事件

    $(window).on('load', function () {
    $("#coverScreen").hide();
    });
    

    无论何时加载页面,上述解决方案都可以。

    现在的问题是在页面加载之后,每当我们单击一个按钮或一个需要很长时间的事件时,我们需要在客户端单击事件中显示它,如下所示

    $("#ucNoteGrid_grdViewNotes_ctl01_btnPrint").click(function () {
    $("#coverScreen").show();
    });
    

    这意味着当我们点击这个打印按钮(这将需要很长时间才能给出报告)时,它会显示我们的封面,上面有gif this 结果,一旦页面准备就绪,窗口加载功能将启动,并在屏幕完全加载后隐藏封面屏幕。

        5
  •  6
  •   Amber    16 年前

    默认内容为 display:none 然后有一个事件处理程序将其设置为 display:block 或者在满载之后。然后有一个设置为 显示:块 其中包含“加载”,并将其设置为 显示:无 在与以前相同的事件处理程序中。

        6
  •  3
  •   Ced    7 年前

    我的博客将100%的工作。

    function showLoader()
    {
        $(".loader").fadeIn("slow");
    }
    function hideLoader()
    {
        $(".loader").fadeOut("slow");
    }
    .loader {
        position: fixed;
        left: 0px;
        top: 0px;
        width: 100%;
        height: 100%;
        z-index: 9999;
        background: url('pageLoader2.gif') 50% 50% no-repeat rgb(249,249,249);
        opacity: .8;
    }
    <div class="loader">
        7
  •  2
  •   ann user874538    11 年前

    好吧,这很大程度上取决于您如何加载“密集型调用”中所需的元素,我最初的想法是,您是通过ajax进行这些加载的如果是这样的话,那么可以使用“beforesend”选项进行如下ajax调用:

    $.ajax({
      type: 'GET',
      url: "some.php",
      data: "name=John&location=Boston",
    
      beforeSend: function(xhr){           <---- use this option here
         $('.select_element_you_want_to_load_into').html('Loading...');
      },
    
      success: function(msg){
         $('.select_element_you_want_to_load_into').html(msg);
      }
    });
    

    编辑 我知道,在这种情况下,使用 'display:block'/'display:none' 以上选项与 $(document).ready(...) 从jquery可能是一条路要走。这个 $(document).ready() 函数在执行前等待加载整个文档结构( 但它不会等待所有媒体加载 )中。你会这样做:

    $(document).ready( function() {
      $('table#with_slow_data').show();
      $('div#loading image or text').hide();
    });
    
        8
  •  1
  •   Steve Harrison    16 年前

    创建一个 <div> 元素,该元素包含加载消息,将 <分区> 一个id,然后当您的内容完成加载时,隐藏 <分区> :

    $("#myElement").css("display", "none");
    

    …或者用普通的javascript:

    document.getElementById("myElement").style.display = "none";
    
        9
  •  1
  •   rybo111    10 年前

    下面是我最终使用的jQuery,它监视所有ajax启动/停止,因此您不需要将其添加到每个ajax调用中:

    $(document).ajaxStart(function(){
        $("#loading").removeClass('hide');
    }).ajaxStop(function(){
        $("#loading").addClass('hide');
    });
    

    用于装载容器和内容的css(主要来自mehyaa的答案),以及 hide 班级:

    #loading {
       width: 100%;
       height: 100%;
       top: 0px;
       left: 0px;
       position: fixed;
       display: block;
       opacity: 0.7;
       background-color: #fff;
       z-index: 99;
       text-align: center;
    }
    
    #loading-content {
      position: absolute;
      top: 50%;
      left: 50%;
      text-align: center;
      z-index: 100;
    }
    
    .hide{
      display: none;
    }
    

    HTML

    <div id="loading" class="hide">
      <div id="loading-content">
        Loading...
      </div>
    </div>
    
        10
  •  0
  •   Francisco Presencia    12 年前

    基于@mehyaa答案,但要短得多:

    HTML(紧接着 <body> ):

    <img id = "loading" src = "loading.gif" alt = "Loading indicator">
    

    CSS:

    #loading {
      position: absolute;
      top: 50%;
      left: 50%;
      width: 32px;
      height: 32px;
      /* 1/2 of the height and width of the actual gif */
      margin: -16px 0 0 -16px;
      z-index: 100;
      }
    

    javascript(jquery,因为我已经在使用它):

    $(window).load(function() {
      $('#loading').remove();
      });