私のウェブサイトのローダーアニメーションスタイルを作成しようとしています。読み込みアニメーションは16の棒で、順番に増減します。たとえば、最初のバーは元のサイズに戻ります。次に、次のバーは、すべてのバーが完了するまでこのプロセスを繰り返してから、プロセスを停止してページを表示します。この場合、JavaScriptは呼び出し関数では非同期であるため、私はそれを回避するという約束をしました。プロミスの使用は、前のアニメートが完了した後でバーをアニメートすることです。現在のところ、私のコードは最初のバーだけをアニメーション化し、そこで停止します。残りの部分をアニメーション化し続けることはありません。以下はすべて私のコードです:Javascript:Promise().then doesnt work
重要! JavaScriptが問題になります。 HTMLやCSSに時間を費やさないでください。
var index = -1;
function loading(){
\t var loader = document.getElementById("loader");
\t display = window.getComputedStyle(loader).display;
\t if (display == "block"){
\t \t var child = document.getElementById("loader-ul").getElementsByTagName("div");
\t \t index = index + 1;
\t \t alert("dd");
\t \t animate(child);
\t }
}
function animate(element){
\t var el = element[index];
\t var MaxHeight = false;
\t var finished = false;
\t function anim(){
\t \t return new Promise(function(resolve, reject){
\t \t \t if (finished == false){
\t \t \t \t if (MaxHeight == false){
\t \t \t \t \t var height = parseInt(window.getComputedStyle(el).height.slice(0, -2));
\t \t \t \t \t var Bot = parseFloat(window.getComputedStyle(el).bottom.slice(0, -2));
\t \t \t \t \t height = height + 1;
\t \t \t \t \t Bot = Bot + 0.5;
\t \t \t \t \t el.style.bottom = Bot + "px";
\t \t \t \t \t el.style.height = height + "px";
\t \t \t \t \t if (height <= 100){
\t \t \t \t \t \t window.requestAnimationFrame(anim);
\t \t \t \t \t }
\t \t \t \t \t else{
\t \t \t \t \t \t MaxHeight = true;
\t \t \t \t \t }
\t \t \t \t }
\t \t \t \t if (MaxHeight == true){
\t \t \t \t \t var height = parseInt(window.getComputedStyle(el).height.slice(0, -2));
\t \t \t \t \t var Bot = parseFloat(window.getComputedStyle(el).bottom.slice(0, -2));
\t \t \t \t \t height = height - 1;
\t \t \t \t \t Bot = Bot - 0.5;
\t \t \t \t \t el.style.bottom = Bot + "px";
\t \t \t \t \t el.style.height = height + "px";
\t \t \t \t \t if (height >= 50){
\t \t \t \t \t \t window.requestAnimationFrame(anim);
\t \t \t \t \t }
\t \t \t \t \t else{
\t \t \t \t \t \t MaxHeight = true;
\t \t \t \t \t \t finished = true;
\t \t \t \t \t \t el.style.bottom = 0 + "px";
\t \t \t \t \t \t el.style.height = 50 + "px";
\t \t \t \t \t }
\t \t \t \t }
\t \t \t }
\t \t \t else{
\t \t \t \t resolve();
\t \t \t }
\t \t });
\t }
\t anim().then(loading);
}
body{
\t margin: 0px;
\t padding: 0px;
\t margin: auto;
}
#loader{
\t display: block;
\t position: fixed;
\t height: 100%;
\t width: 100%;
\t background-color: white;
\t z-index: 9999;
}
#loader .center{
\t position: relative;
\t height: 50px;
\t width: 200px;
\t background-color: red;
\t top: 50%;
\t transform: translateY(-50%);
\t margin: auto;
}
#loader .center div{
\t width: 2px;
\t height: 50px;
\t background-color: blue;
\t float: left;
\t padding: 0px;
\t margin-right: 5px;
\t margin-bottom: 25px;
\t position: relative;
}
<body onload="loading()">
<div id="loader">
\t \t \t <div class="center" id="loader-ul">
\t \t \t \t <div></div>
\t \t \t \t <div></div>
\t \t \t \t <div></div>
\t \t \t \t <div></div>
\t \t \t \t <div></div>
\t \t \t \t <div></div>
\t \t \t \t <div></div>
\t \t \t \t <div></div>
\t \t \t \t <div></div>
\t \t \t \t <div></div>
\t \t \t \t <div></div>
\t \t \t \t <div></div>
\t \t \t \t <div></div>
\t \t \t \t <div></div>
\t \t \t \t <div></div>
\t \t \t \t <div></div>
\t \t \t </div>
\t \t </div>
\t </body>
私もjsfiddleへのリンクを持っている:
https://jsfiddle.net/6227jjen/
はあなたのすべてをありがとう!
注: デバッグ用にいくつかの警告を追加しました。
それが問題とクラッタあなたの質問とは無関係です、あなたのHTMLやCSSを削除してください。 –
私は同意しますが、私はsnippedを実行するためだけに追加しました。 – GeorGios
あなたのフィドルに問題があります - コンソールが表示されます:読み込みが定義されていません。 JSオプションで
をラップする必要はありません。 –