如何在 2 秒后停止加载动画?

我想制作一个加载屏幕和一个淡入淡出的页面,如下所示:https : //www.w3schools.com/howto/tryit.asp?filename=tryhow_css_loader5。问题是我不知道如何结束加载循环并使其褪色。


代码


        <body>

            <div class="sk-chase">

                <div class="sk-chase-dot"></div>

                <div class="sk-chase-dot"></div>

                <div class="sk-chase-dot"></div>

                <div class="sk-chase-dot"></div>

                <div class="sk-chase-dot"></div>

                <div class="sk-chase-dot"></div>

            </div>

         </body>

body {

        background-color: #636e72;

    }sk-chase {

      width: 40px;

      height: 40px;

      animation: sk-chase 2.5s infinite linear both;

      display: flex;

      justify-content: center;

      align-items: center;

      margin: 0 auto;

      top: 50%;

      left: 50%;

      position: absolute;

    }

    .sk-chase-dot {

      width: 100%;

      height: 100%;

      position: absolute;

      left: 0;

      top: 0; 

      animation: sk-chase-dot 2.0s infinite ease-in-out both; 

    }.sk-chase-dot:before {

      content: '';

      display: block;

      width: 25%;

      height: 25%;

      background-color: #fff;

      border-radius: 100%;

      animation: sk-chase-dot-before 2.0s infinite ease-in-out both; 


    @keyframes sk-chase {

      100% { transform: rotate(360deg); } 

    }

    @keyframes sk-chase-dot {

      80%, 100% { transform: rotate(360deg); } 

    }

    @keyframes sk-chase-dot-before {

      50% {

        transform: scale(0.4); 

      } 100%, 0% {

        transform: scale(1.0); 

      } 

    }


HUX布斯
浏览 169回答 2
2回答

Qyouu

animation: sk-chase 2.5s infinite linear both;Wordinfinite是你的问题。改为使用多个代表infinite。在你的情况下是1。

慕森卡

您需要 JavaScript 来告诉浏览器何时隐藏加载程序并显示内容。见下面的片段var myVar;function myFunction() {&nbsp; myVar = setTimeout(showPage, 3000);}function showPage() {&nbsp; document.querySelector("#loader").style.display = "none";&nbsp; document.querySelector("#myDiv").style.display = "block";&nbsp; document.querySelector("body").style.backgroundColor = "white";}body {&nbsp; background-color: #636e72;}.sk-chase {&nbsp; width: 40px;&nbsp; height: 40px;&nbsp; animation: sk-chase 2.5s infinite linear both;&nbsp; display: flex;&nbsp; justify-content: center;&nbsp; align-items: center;&nbsp; margin: 0 auto;&nbsp; top: calc(50% - 20px);&nbsp; left: calc(50% - 20px);&nbsp; position: absolute;}.sk-chase-dot {&nbsp; width: 100%;&nbsp; height: 100%;&nbsp; position: absolute;&nbsp; left: 0;&nbsp; top: 0;&nbsp; animation: sk-chase-dot 2.0s infinite ease-in-out both;}.sk-chase-dot:before {&nbsp; content: '';&nbsp; display: block;&nbsp; width: 25%;&nbsp; height: 25%;&nbsp; background-color: #fff;&nbsp; border-radius: 100%;&nbsp; animation: sk-chase-dot-before 2.0s infinite ease-in-out both;}.sk-chase-dot:nth-child(1) {&nbsp; animation-delay: -1.1s;}.sk-chase-dot:nth-child(2) {&nbsp; animation-delay: -1.0s;}.sk-chase-dot:nth-child(3) {&nbsp; animation-delay: -0.9s;}.sk-chase-dot:nth-child(4) {&nbsp; animation-delay: -0.8s;}.sk-chase-dot:nth-child(5) {&nbsp; animation-delay: -0.7s;}.sk-chase-dot:nth-child(6) {&nbsp; animation-delay: -0.6s;}.sk-chase-dot:nth-child(1):before {&nbsp; animation-delay: -1.1s;}.sk-chase-dot:nth-child(2):before {&nbsp; animation-delay: -1.0s;}.sk-chase-dot:nth-child(3):before {&nbsp; animation-delay: -0.9s;}.sk-chase-dot:nth-child(4):before {&nbsp; animation-delay: -0.8s;}.sk-chase-dot:nth-child(5):before {&nbsp; animation-delay: -0.7s;}.sk-chase-dot:nth-child(6):before {&nbsp; animation-delay: -0.6s;}@keyframes sk-chase {&nbsp; 100% {&nbsp; &nbsp; transform: rotate(360deg);&nbsp; }}@keyframes sk-chase-dot {&nbsp; 80%,&nbsp; 100% {&nbsp; &nbsp; transform: rotate(360deg);&nbsp; }}@keyframes sk-chase-dot-before {&nbsp; 50% {&nbsp; &nbsp; transform: scale(0.4);&nbsp; }&nbsp; 100%,&nbsp; 0% {&nbsp; &nbsp; transform: scale(1.0);&nbsp; }}<body onload="myFunction()">&nbsp; <div id="loader" class="sk-chase">&nbsp; &nbsp; <div class="sk-chase-dot"></div>&nbsp; &nbsp; <div class="sk-chase-dot"></div>&nbsp; &nbsp; <div class="sk-chase-dot"></div>&nbsp; &nbsp; <div class="sk-chase-dot"></div>&nbsp; &nbsp; <div class="sk-chase-dot"></div>&nbsp; &nbsp; <div class="sk-chase-dot"></div>&nbsp; </div>&nbsp; <div style="display:none;" id="myDiv" class="animate-bottom">&nbsp; &nbsp; <h2>Tada!</h2>&nbsp; &nbsp; <p>Some text in my newly loaded page..</p>&nbsp; </div></body>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript