【每日一练】233—JS实现一款加载动画效果

每日一练10个月前发布 admin
1,537 0

第233练最终效果如下:

【每日一练】233—JS实现一款加载动画效果

HTML:

<!DOCTYPE html>
<html lang=”en”>
  <head>
    <meta charset=”UTF-8″ />
    <meta http-equiv=”X-UA-Compatible” content=”IE=edge” />
    <meta name=”viewport” content=”width=device-width, initial-scale=1.0″ />
    <title>第233练</title>
    <link rel=”stylesheet” href=”index.css” />
  </head>
  <body>
    <div class=”container”>
      <h1>Loading</h1>
      <div class=”counter”>0%</div>
      <hr class=”loading-bar-back” />
      <hr class=”loading-bar-front” />
    </div>
    <script src=”index.js”></script>
  </body>
</html>

CSS:

body {
    margin: 0;
    display: flex;
    justify-content: center;
    height: 100vh;
    align-items: center;
    background-color: black;
    color: lightgray;
    font-family: cursive;
  }
  .container {
    text-align: center;
    width: 400px;
    position: relative;
  }
  .counter {
    font-size: 40px;
  }
  .loading-bar-back {
    position: absolute;
    height: 8px;
    background-color: lightgray;
    width: 100%;
    border-radius: 5px;
    border: none;
  }
  .loading-bar-front {
    position: absolute;
    height: 8px;
    background-color: #00a6bc;
    width: 0%;
    border-radius: 5px;
    border: none;
  }

JS:

const counterEl = document.querySelector(“.counter”);
const barEl = document.querySelector(“.loading-bar-front”);
let idx = 0;
updateNum();
function updateNum() {
  counterEl.innerText = idx + “%”;
  barEl.style.width = idx + “%”;
  idx++;
  if (idx < 101) {
    setTimeout(updateNum, 20);
  }
}

 

© 版权声明

相关文章

暂无评论

暂无评论...