简体   繁体   English

如何使我的音频搜索栏可点击并跳转到我点击的位置?

[英]How can I make my audio seek bar clickable and skip to where I click it?

I have a custom audio seek bar I would like to make it clickable and make the audio jump to that section.我有一个自定义音频搜索栏,我想让它可点击并使音频跳转到该部分。 This is my HTML code of the audio seek bar这是我的音频搜索栏的 HTML 代码

<audio src="" preload="auto"  controls id="audioPlayer" ontimeupdate="updateTrackTime(this);" style="width: 0px; visibility: hidden ;"></audio>
<div class="p-bar">
   <div class="progress1"  id="progress1"></div>
</div>

This is my CSS code:这是我的 CSS 代码:

.p-bar{
    flex: ;
    width: 25%;
    height: 4px;
    background: #333333;
}
.progress1 {    
  height:3px;
  background: whitesmoke;
  transition: width .1s linear;
  cursor: pointer;
  justify-self: left;
  left: 0;
  box-shadow: 1px 1px 2px  white;
}

This is my JavaScript code of the seek bar:这是我的搜索栏的 JavaScript 代码:

var track = document.getElementById('audioPlayer');

var controlBtn = document.getElementById('play-pause');


var timer;
var percent = 0;
var audio = document.getElementById("audioPlayer");
audio.addEventListener("playing", function(_event) {
  var duration = _event.target.duration;
  advance(duration, audio);
});
audio.addEventListener("pause", function(_event) {
  clearTimeout(timer);
});


var advance = function(duration, element) {
  var progress = document.getElementById("progress1");
  increment = 10/duration
  percent = Math.min(increment * element.currentTime * 10, 100);
  progress.style.width = percent+'%'
  startTimer(duration, element);
}
var startTimer = function(duration, element){ 
  if(percent < 100) {
    timer = setTimeout(function (){advance(duration, element)}, 100);
  }
}

You can make any HTML element clickable by using a "click" event handler .您可以使用“单击”事件处理程序使任何 HTML 元素可单击 Once you're inside the click event handler, the MouseEvent passed to the handler will have about five different ways to get the x position of the mouse click, so you can determine how far left on the bar the user clicked.一旦您进入 click 事件处理程序,传递给处理程序的MouseEvent将有大约五种不同的方式来获取鼠标单击的 x 位置,因此您可以确定用户单击的栏的左侧有多远。 You may need to try a few of them to see which works for you, but I'd suggest starting with offsetX.您可能需要尝试其中的一些以查看哪种适合您,但我建议从 offsetX 开始。 From there you should be able to calculate the percentage to skip the media to based on the percentage along the progress item the user clicked.从那里您应该能够根据用户单击的进度项目的百分比计算跳过媒体的百分比。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM