HTML加載動畫實現


在頁面加載完成之前或者執行某操作時,先顯示要顯示的加載動畫。

實現原理

先定義一個加載動畫元素(最好是純CSS實現,圖片的話可能慢一點),當頁面未加載完成之前,先使其“可見”,當頁面加載完成后,再使其“不可見”。重要的一點就是怎樣知道頁面或者元素加載完成了,詳情可以看一下:

https://blog.csdn.net/weixin_43670802/article/details/105875167

具體實現

加載動畫頁面來自:codePen
作者:@majci23

加載動畫頁面HTML
<div id="loading">
    <div id="loading_bg">
        <div class="loader">Loading...</div>
    </div>
</div>
加載動畫頁面CSS
/*********LoadingPage*************/

#loading {
  position: absolute;
  background-color: #FFF;:
  top: 0px;
  left: 0px;
  width: 100%;
  height: 100%;
  z-index: 9999;
}
#loading_bg{
  background-color: rgba(0,0,0,0.7);
}
//body {
//  background: #eaecfa;
//}

.loader {
  width: 250px;
  height: 50px;
  line-height: 50px;
  text-align: center;
  position: absolute;
  top: 50%;
  left: 50%;
  -webkit-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
  font-family: helvetica, arial, sans-serif;
  text-transform: uppercase;
  font-weight: 900;
  color: #ce4233;
  letter-spacing: 0.2em;
}
.loader::before, .loader::after {
  content: "";
  display: block;
  width: 15px;
  height: 15px;
  background: #ce4233;
  position: absolute;
  -webkit-animation: load .7s infinite alternate ease-in-out;
  animation: load .7s infinite alternate ease-in-out;
}
.loader::before {
  top: 0;
}
.loader::after {
  bottom: 0;
}

@-webkit-keyframes load {
  0% {
    left: 0;
    height: 30px;
    width: 15px;
  }
  50% {
    height: 8px;
    width: 40px;
  }
  100% {
    left: 235px;
    height: 30px;
    width: 15px;
  }
}

@keyframes load {
  0% {
    left: 0;
    height: 30px;
    width: 15px;
  }
  50% {
    height: 8px;
    width: 40px;
  }
  100% {
    left: 235px;
    height: 30px;
    width: 15px;
  }
}

/********************************/
JS操作
//Loading頁面
document.onreadystatechange=function () {
          if (document.readyState=="complete"){
               loadingFade();
          }
}
function loadingFade() {
     var opacity=1;
     //var loadingPage=document.getElementById('loading');
     var loadingBackground=document.getElementById('loading_bg');
     var time=setInterval(function () {
          if (opacity<=0){
               clearInterval(time);
               //loadingPage.remove();
               $('#loading').remove();
          }

          loadingBackground.style.opacity=opacity;
          opacity-=0.4;
     },100);
}

參考
https://blog.csdn.net/qq_39036844/article/details/82454349?depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-2&utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-2
https://www.runoob.com/jsref/prop-doc-readystate.html

https://developer.mozilla.org/zh-CN/docs/Web/API/Document/readyState
https://www.cnblogs.com/sunny-sl/p/7977898.html
https://www.cnblogs.com/passlogs/p/6844065.html
https://blog.csdn.net/xiaokui_wingfly/article/details/51502209?depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-2&utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-2


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM