サイト・コンテンツのロード中にローディング・スクリーンを表示する方法

mp3や画像を大量に含むサイトを制作しているのですが、すべてのコンテンツの読み込み中にローディングGIFを表示したいのです。

しかし、使いたいアニメーションGIFはあります。

何か提案はありますか?

ソリューション

通常、ajax経由でコンテンツをロードし、readystatechangedイベントをリッスンして、ロード中のGIFやコンテンツでDOMを更新する。

現在、どのようにコンテンツをロードしていますか?

コードはこのようになります:

function load(url) {
    // display loading image here...
    document.getElementById('loadingImg').visible = true;
    // request your data...
    var req = new XMLHttpRequest();
    req.open("POST", url, true);

    req.onreadystatechange = function () {
        if (req.readyState == 4 && req.status == 200) {
            // content is loaded...hide the gif and display the content...
            if (req.responseText) {
                document.getElementById('content').innerHTML = req.responseText;
                document.getElementById('loadingImg').visible = false;
            }
        }
    };
    request.send(vars);
}

サードパーティのjavascriptライブラリはたくさんあり、あなたの生活を便利にしてくれるかもしれませんが、本当に必要なのは上記だけです。

解説 (2)

あなたはAJAXでこれをしたくないと言いました。 AJAXはこれには最適ですが、``全体がロードされるのを待つ間に1つのDIVを表示する方法があります。 それは次のようなものです:




      .layer1_class { position: absolute; z-index: 1; top: 100px; left: 0px; visibility: visible; }
      .layer2_class { position: absolute; z-index: 2; top: 10px; left: 10px; visibility: hidden }

    <script>
      function downLoad(){
        if (document.all){
            document.all["layer1"].style.visibility="hidden";
            document.all["layer2"].style.visibility="visible";
        } else if (document.getElementById){
            node = document.getElementById("layer1").style.visibility='hidden';
            node = document.getElementById("layer2").style.visibility='visible';
        }
      }
    </script>


    <div id="layer1" class="layer1_class">
      <table width="100%">
        <tr>
          <td align="center"><strong><em>Please wait while this page is loading...</em></strong></p></td>
        </tr>
      </table>
    </div>
    <div id="layer2" class="layer2_class">
        <script type="text/javascript">
                alert('Just holding things up here.  While you are reading this, the body of the page is not loading and the onload event is being delayed');
        </script>
        Final content.      
    </div>

onloadイベントはページ全体が読み込まれるまで発生しません。 つまり、レイヤー2の <DIV> はページの読み込みが終わるまで表示されません。

解説 (1)

jQueryはどうですか? シンプル。..

$(window).load(function() {      //Do the code in the {}s when the window has loaded 
  $("#loader").fadeOut("fast");  //Fade out the #loader div
});

そしてHTML ...

<div id="loader"></div>

そしてCSS ...

#loader {
      width: 100%;
      height: 100%;
      background-color: white;
      margin: 0;
}

次に、ローダーの「div」にGIFと必要なテキストを配置し、ページが読み込まれるとフェードアウトします。

解説 (0)

まず、divにローディングイメージを設定します。 次に、div要素を取得します。 次に、cssを編集して可視性を「非表示」にする関数を設定します。 次に、 < body>で、オンロードを関数名に配置します。

解説 (0)

#純粋なcssメソッド

これをコードの上部に配置します(ヘッダータグの前)。

 .loader {
  position: fixed;
  background-color: #FFF;
  opacity: 1;
  height: 100%;
  width: 100%;
  top: 0;
  left: 0;
  z-index: 10;
}
<div class="loader">
  Your Content For Load Screen
</div>

そして、他のすべてのコードの後のこの最下位(/ htmlタグを除く)。


.loader {
    -webkit-animation: load-out 1s;
    animation: load-out 1s;
    -webkit-animation-fill-mode: forwards;
    animation-fill-mode: forwards;
}

@-webkit-keyframes load-out {
    from {
        top: 0;
        opacity: 1;
    }

    to {
        top: 100%;
        opacity: 0;
    }
}

@keyframes load-out {
    from {
        top: 0;
        opacity: 1;
    }

    to {
        top: 100%;
        opacity: 0;
    }
}

これは常に100%の時間で機能します。

解説 (0)

HTML5では「< progress>」要素を使用できます。 ソースコードとライブデモについては、このページを参照してください。 http://purpledesign.in/blog/super-cool-loading-bar-html5/

これが進行状況要素です。..

これには、20から始まる荷重値があります。 もちろん、要素だけでは不十分です。 スクリプトが読み込まれるときに移動する必要があります。 そのためにはJQueryが必要です。 以下は、0から100への進行を開始し、定義された時間枠で何かを実行する単純なJQueryスクリプトです。

<script>
        $(document).ready(function() {
         if(!Modernizr.meter){
         alert('Sorry your brower does not support HTML5 progress bar');
         } else {
         var progressbar = $('#progressbar'),
         max = progressbar.attr('max'),
         time = (1000/max)*10, 
         value = progressbar.val();
        var loading = function() {
        value += 1;
        addValue = progressbar.val(value);
        $('.progress-value').html(value + '%');
        if (value == max) {
        clearInterval(animate);
        //Do Something
 }
if (value == 16) {
//Do something 
}
if (value == 38) {
//Do something
}
if (value == 55) {
//Do something 
}
if (value == 72) {
//Do something 
}
if (value == 1) {
//Do something 
}
if (value == 86) {
//Do something 
    }

};
var animate = setInterval(function() {
loading();
}, time);
};
});
</script>

これをHTMLファイルに追加します。

<div class="demo-wrapper html5-progress-bar">
<div class="progress-bar-wrapper">

 <span class="progress-value">0%</span>
</div>
 </div>

これがあなたにスタートを与えることを願っています。

解説 (0)

これを行うには、実はとても簡単な方法がある。コードは次のようなものだ:

<script type="test/javascript">

    function showcontent(x){

      if(window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
      } else {
        xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
      }

      xmlhttp.onreadystatechange = function() {
        if(xmlhttp.readyState == 1) {
            document.getElementById('content').innerHTML = "";
        }
        if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
          document.getElementById('content').innerHTML = xmlhttp.responseText;
        } 
      }

      xmlhttp.open('POST', x+'.html', true);
      xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
      xmlhttp.send(null);

    }

そしてHTMLでは


<div id="content"></div> 

私のページでもそのようにしたところ、とてもうまくいきました。

解説 (0)