CSS、JavaScript实现进度条效果HTML

10 篇文章 0 订阅

CSS、JavaScript实现进度条效果HTML

效果图

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

代码如下:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>ProgressBar</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    body {
      position: relative;
    }

    .container {
      max-width: 600px;
      margin: 0 auto;
      text-align: center;
      margin-top: 50px;
    }

    #progress {
      position: relative;
      width: 100%;
      height: 30px;
      /* background: rgb(42, 138, 248); */
      background-image: linear-gradient(110.6deg, rgb(179, 157, 219) 7%, rgb(150, 159, 222) 47.7%, rgb(24, 255, 255) 100.6%);
      /* background: linear-gradient(111.4deg, rgb(238, 113, 113) 1%, rgb(246, 215, 148) 58%); */
      border-radius: 10px;
    }

    #bar {
      width: 0%;
      height: 30px;
      /* margin-top: 1px; */
      /* background: purple; */
      /* background-image: linear-gradient(to right, #e66465, #9198e5); */
      background: linear-gradient(to right, #9fe1fa, #f4edc9);
      border-radius: 10px;
    }

    #text-progress {
      position: absolute;
      right: 2%;
      top: 0;
      color: #FFFFFF;
      height: 30px;
      line-height: 30px;
    }

    .btn {
      background: #3860FF;
      border: none;
      line-height: 32px;
      height: 32px;
      width: 80px;
      text-align: center;
      color: #FFFFFF;
      font-size: 14px;
      border-radius: 6px;

      position: absolute;
      left: calc(50% - 40px);
      margin-top: 40px;
      cursor: pointer;
    }

    .btn:hover {
      opacity: 0.6;
      transition: all 0.6s;
    }

    .reset {
      background: #ff9100;
    }

    .stop {
      background: #ff2d2d;
    }
  </style>

</head>

<body>
  <div class="container">
    <div id="progress">
      <div id="bar"></div>
      <h3 id="text-progress">0%</h3>
    </div>
  </div>
  <input class="btn" type="button" id="btn" value="点击开始" onclick="action()">
</body>
<script>
  var obj = 0;
  var iSpeed = 0;
  var btnVal = document.getElementById('btn');

  function action() {
    if (obj == 0 && iSpeed < 100) {
      obj = setInterval(function () {
        iSpeed += 1;
        if (iSpeed >= 100) {    // 设置达到多少进度后停止
          clearInterval(obj);
          btnVal.value = '点击重置';
          btnVal.setAttribute('class', 'btn reset');
        }
        bar.style.width = iSpeed + '%';
        document.getElementById('text-progress').innerHTML = iSpeed + '%';
      }, 100);    // 1s后函数执行一次

      btnVal.value = '点击停止';
      btnVal.setAttribute('class', 'btn stop');
    } else if (iSpeed >= 100) {
      clearInterval(obj);
      obj = 0;
      iSpeed = 0;
      obj = setInterval(function () {
        iSpeed += 1;
        if (iSpeed >= 100) {    // 设置达到多少进度后停止
          clearInterval(obj);
        }
        bar.style.width = iSpeed + '%';
        document.getElementById('text-progress').innerHTML = iSpeed + '%';
      }, 100);

      btnVal.value = '点击停止';
      btnVal.setAttribute('class', 'btn stop');
    } else {
      clearInterval(obj);
      obj = 0;
      btnVal.value = '点击开始';
      btnVal.setAttribute('class', 'btn');
    }
  }
</script>

</html>
  • 30
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是添加进度条的大致操作步骤: 1.首先在 HTML 中添加音乐播放器和进度条的相关元素,如下所示: ``` <audio id="myAudio" src="music.mp3"></audio> <div id="progressBar"> <div id="progress"></div> </div> ``` 其中,`<audio>` 元素用于播放音乐,`id` 属性为 `"myAudio"`,`src` 属性为音乐文件的路径。`<div>` 元素用于显示进度条,`id` 属性为 `"progressBar"`。 2.在 CSS 中为进度条和进度条背景添加样式,如下所示: ``` #progressBar { width: 100%; height: 10px; background-color: #ccc; } #progress { width: 0%; height: 100%; background-color: #f00; } ``` 其中,`#progressBar` 表示进度条背景,设置宽度为 100%,高度为 10px,背景颜色为灰色;`#progress` 表示进度条,设置宽度为 0%,高度为 100%,背景颜色为红色。 3.在 JavaScript 中获取音乐播放器和进度条元素,如下所示: ``` const audio = document.getElementById('myAudio'); const progressBar = document.getElementById('progressBar'); const progress = document.getElementById('progress'); ``` 其中,`audio` 表示音乐播放器,`progressBar` 表示进度条背景,`progress` 表示进度条。 4.在 JavaScript 中为音乐播放器添加事件监听器,监听音乐播放进度的变化,并更新进度条的显示,如下所示: ``` audio.addEventListener('timeupdate', function() { const percent = (audio.currentTime / audio.duration) * 100; progress.style.width = `${percent}%`; }); ``` 其中,`timeupdate` 事件表示音乐播放进度发生变化,每次变化时触发回调函数,使用 `audio.currentTime` 获取当前播放时间,使用 `audio.duration` 获取音乐总时长,计算出当前播放进度的百分比,并将进度条的宽度设置为该百分比。 5.在 JavaScript 中为进度条添加事件监听器,监听用户点击事件,根据用户点击位置更新音乐播放进度进度条的显示,如下所示: ``` progressBar.addEventListener('click', function(event) { const percent = (event.offsetX / progressBar.clientWidth) * 100; audio.currentTime = (percent / 100) * audio.duration; progress.style.width = `${percent}%`; }); ``` 其中,`click` 事件表示用户点击事件,每次点击时触发回调函数,使用 `event.offsetX` 获取用户点击位置相对于进度条背景的横坐标,使用 `progressBar.clientWidth` 获取进度条背景的宽度,计算出用户点击位置的百分比,并根据该百分比更新音乐播放进度进度条的显示。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值