自定义音频进度条固定在中间无法移动的技术求助
音频进度条无法随播放进度移动的解决方法
核心问题:拼写错误
代码中多处将currentTime误写为curentTime(缺少字母r),这直接导致进度条无法正确获取或设置音频播放时间,是进度条固定在中间的根本原因。
次要问题:定时器触发时机错误
原代码中用if (song.play())启动定时器存在两个问题:
song.play()返回的是Promise对象,并非布尔值,条件判断不成立- 浏览器默认阻止页面自动播放音频,这段代码块根本不会执行
修正后的完整代码
JavaScript 部分
let progress = document.getElementById("progress"); let song = document.getElementById("song"); let ctrIcon = document.getElementById("ctrlIcon"); let updateProgressInterval; song.onloadedmetadata = function () { progress.max = song.duration; progress.value = song.currentTime; // 修正拼写错误 }; function playPause() { if (ctrIcon.classList.contains("fa-pause")) { song.pause(); ctrIcon.classList.remove("fa-pause"); ctrIcon.classList.add("fa-play"); clearInterval(updateProgressInterval); // 暂停时清除定时器 } else { song.play(); ctrIcon.classList.add("fa-pause"); ctrIcon.classList.remove("fa-play"); // 播放时启动定时器更新进度 updateProgressInterval = setInterval(() => { progress.value = song.currentTime; // 修正拼写错误 }, 100); // 缩短间隔让进度更新更流畅 } } progress.onchange = function () { song.play(); song.currentTime = progress.value; // 修正拼写错误 ctrIcon.classList.add("fa-pause"); ctrIcon.classList.remove("fa-play"); // 拖动进度后重启定时器,确保进度同步 clearInterval(updateProgressInterval); updateProgressInterval = setInterval(() => { progress.value = song.currentTime; }, 100); };
CSS 部分(无需修改)
* { margin: 0; padding: 0; font-family: "Poppins", "sans-serif"; box-sizing: border-box; } .container { width: 100%; height: 100vh; background: #333; display: flex; align-items: center; justify-content: center; flex-wrap: wrap; } .music-player { background: #ffe0e5; width: 400px; padding: 25px 35px; text-align: center; } nav { display: flex; justify-content: space-between; margin-bottom: 30px; } nav .circle { border-radius: 50%; width: 40px; height: 40px; line-height: 40px; background: #fff; color: #f53192; box-shadow: 0 5px 10px rgba(255, 26, 26, 0.22); } .song-img { width: 220px; border-radius: 50%; border: 8px solid #fff; box-shadow: 0 10px 60px rgba(255, 26, 26, 0.22); } .music-player h1 { font-size: 20px; font-weight: 400px; color: #333; margin-top: 20px; } .music-player p { font-size: 14px; color: #333; } #progress { -webkit-appearance: none; width: 100%; height: 6px; background: #f53192; border-radius: 4px; cursor: pointer; margin: 40px 0; } #progress::-webkit-slider-thumb { -webkit-appearance: none; background-color: #f53192; width: 30px; height: 30px; border-radius: 50%; border: 8px solid #fff; box-shadow: 0 5px 5px rgba(255, 26, 26, 0.22); } .controls { display: flex; justify-content: center; align-items: center; } .controls div { width: 60px; height: 60px; margin: 20px; background: #fff; display: inline-flex; align-items: center; justify-content: center; border-radius: 50px; color: #f53192; box-shadow: 0 10px 20px rgba(222, 26, 26, 0.22); cursor: pointer; } .controls div:nth-child(2) { transform: scale(1.5); background: #f53192; color: #fff; }
HTML 部分(无需修改)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Music Player</title> <link rel="stylesheet" href="style.css" /> <script defer src="music.js"></script> <script src="https://kit.fontawesome.com/347a59382b.js" crossorigin="anonymous" ></script> </head> <body> <div class="container"> <div class="music-player"> <nav> <div class="circle"> <i class="fa-solid fa-angle-left"></i> </div> <div class="circle"> <i class="fa-solid fa-bars"></i> </div> </nav> <img src="468-thumbnail.png" class="song-img" /> <h1>Despacito</h1> <p>Luis Fonso Jamil</p> <audio controls id="song"> <source src="audio.mp3" type="audio/mpeg" /> </audio> <input type="range" id="progress" /> <div class="controls"> <div><i class="fa-solid fa-backward"></i></div> <div onclick="playPause()"> <i class="fa-solid fa-play" id="ctrlIcon"></i> </div> <div><i class="fa-solid fa-forward"></i></div> </div> </div> </div> </body> </html>
额外优化说明
- 新增
updateProgressInterval变量管理定时器,暂停时清除定时器避免资源浪费 - 将定时器间隔从500ms缩短至100ms,提升进度条更新流畅度
- 拖动进度条后重启定时器,确保播放进度与进度条持续同步
内容的提问来源于stack exchange,提问作者Jamil Rahman Mowahid
相关产品推荐
相关产品推荐

