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ライブラリはたくさんあり、あなたの生活を便利にしてくれるかもしれませんが、本当に必要なのは上記だけです。
あなたはAJAXでこれをしたくないと言いました。 AJAXはこれには最適ですが、<body>
全体がロードされるのを待つ間に1つのDIVを表示する方法があります。 それは次のようなものです:
<html>
<head>
<style media="screen" type="text/css">
.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 }
</style>
<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>
</head>
<body onload="downLoad()">
<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>
</body>
</html>
onloadイベントはページ全体が読み込まれるまで発生しません。 つまり、レイヤー2の <DIV>
はページの読み込みが終わるまで表示されません。
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と必要なテキストを配置し、ページが読み込まれるとフェードアウトします。
#純粋なcssメソッド。
これをコードの上部に配置します(ヘッダータグの前)。
<。!-スニペットを開始:js hide:false -->。
<style> .loader {
position: fixed;
background-color: #FFF;
opacity: 1;
height: 100%;
width: 100%;
top: 0;
left: 0;
z-index: 10;
}
</style>
<div class="loader">
Your Content For Load Screen
</div>
<。!-終了スニペット-->。
そして、他のすべてのコードの後のこの最下位(/ htmlタグを除く)。
<。!-スニペットを開始:js hide:false -->。
<style>
.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;
}
}
</style>
<。!-終了スニペット-->。
これは常に100%の時間で機能します。
HTML5では「< progress>」要素を使用できます。 ソースコードとライブデモについては、このページを参照してください。 http://purpledesign.in/blog/super-cool-loading-bar-html5/。
これが進行状況要素です。..
<progress id="progressbar" value="20" max="100"></progress>
これには、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">
<progress id="progressbar" value="0" max="100"></progress>
<span class="progress-value">0%</span>
</div>
</div>
これがあなたにスタートを与えることを願っています。
これを行うには、実はとても簡単な方法がある。コードは次のようなものだ:
<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 = "<img src='loading.gif' />";
}
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では
<body onload="showcontent(main)"> <!-- onload optional -->
<div id="content"><img src="loading.gif"></div> <!-- leave img out if not onload -->
</body>
私のページでもそのようにしたところ、とてもうまくいきました。