简体   繁体   English

HTML进度条单击并更新

[英]HTML progress bar click and update

I've added a progress bar in my javascript code, to show the progress of a music track. 我在我的javascript代码中添加了一个进度条,以显示音乐曲目的进度。

audioElement.addEventListener("timeupdate", function(){
var progress = document.getElementById("progress");
var value = 0;
if (audioElement.currentTime > 0) {
  value = Math.floor((100 / audioElement.duration) * audioElement.currentTime);
}
progress.style.width = value + "%";}, false);

The code works fine, and I can see that my bar progresses as the music progresses as well. 代码工作正常,我可以看到我的酒吧随着音乐的进展而进步。

Now I would like to be able to click anywhere on the progressbar, and be able to forward the music accordingly. 现在,我希望能够点击进度条上的任意位置,并能够相应地转发音乐。 In other words, I would like to be able to control the rewind/forward the track by clicking on the progress bar itself. 换句话说,我希望能够通过单击进度条本身来控制回放/转发音轨。 Could someone help me, and gave me some idea how to do this. 有人可以帮助我,并告诉我如何做到这一点。 I am not an expert with HTML and JavaScripting, so I would appreciate some help. 我不是HTML和JavaScripting的专家,所以我很感激一些帮助。

Thanks, --Rudy 谢谢, - 鲁迪

I would do it like this, using jQuery to make things slightly easier: 我会这样做,使用jQuery使事情稍微容易一些:

http://jsfiddle.net/LQqGS/3/ http://jsfiddle.net/LQqGS/3/

$('.clickable').bind('click', function (ev) {
    var $div = $(ev.target);
    var $display = $div.find('.display');

    var offset = $div.offset();
    var x = ev.clientX - offset.left;

    $('.progress').width(x);
});

<div class='clickable'>
    <div class='display'>
        <div class="progress"></div>
    </div>
</div>

You can then use that x coordinate to manipulate your music player as a percentage of the div width. 然后,您可以使用该x坐标以div宽度的百分比来操纵您的音乐播放器。

I came across this question today while creating a custom HTML5 video player. 今天我在创建自定义HTML5视频播放器时遇到了这个问题。 Just in regards to video instead of audio. 只是关于视频而不是音频。 The process should work the same though for your audio needs. 根据您的音频需求,该过程应该相同。

I found this article and was able to incorporate the progress bar part of it into my player. 我找到了这篇文章,并且能够将其中的进度条部分合并到我的播放器中。 https://msdn.microsoft.com/en-us/library/ie/gg589528%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396 https://msdn.microsoft.com/en-us/library/ie/gg589528%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396

Instead of using a progressbar element, like I was doing, the method I used here is to use a canvas element instead. 而不是像我一样使用progressbar元素,我在这里使用的方法是使用canvas元素。

<canvas id='progress-bar' width="200" height="20" style="border:1px solid green;">canvas not supported</canvas>

Then in your JavaScript, create a handle to reference it by 然后在您的JavaScript中,创建一个句柄来引用它

var mediaPlayer;
var progressBar;
var canvas;

When the document loads, initialize everything including the progress bar items 加载文档时,初始化包括进度条项在内的所有内容

mediaPlayer = document.getElementById('media-video');
progressBar = document.getElementById('progress-bar');
canvas = document.getElementById('progress-bar');

canvas.addEventListener("click", function(e) {

    var canvas = document.getElementById('progress-bar');

    if (!e) {
        e = window.event;
    } //get the latest windows event if it isn't set
    try {
        //calculate the current time based on position of mouse cursor in canvas box
        mediaPlayer.currentTime = mediaPlayer.duration * (e.offsetX / canvas.clientWidth);
    }
    catch (err) {
    // Fail silently but show in F12 developer tools console
        if (window.console && console.error("Error:" + err));
    }
}, true);

mediaPlayer.addEventListener('timeupdate', updateProgressBar, false);

Then create a function outside of your initialization function for the timeupdate listener to call and automatically update the progress bar for you 然后在初始化函数之外创建一个函数,以便timeupdate侦听器调用并自动为您更新进度条

function updateProgressBar() {        
    mediaPlayer = document.getElementById('media-video');
    //get current time in seconds
    var elapsedTime = Math.round(mediaPlayer.currentTime);
    //update the progress bar
    if (canvas.getContext) {
        var ctx = canvas.getContext("2d");
        //clear canvas before painting
        ctx.clearRect(0, 0, canvas.clientWidth, canvas.clientHeight);
        ctx.fillStyle = "rgb(255,0,0)";
        var fWidth = (elapsedTime / mediaPlayer.duration) * (canvas.clientWidth);
        if (fWidth > 0) {
            ctx.fillRect(0, 0, fWidth, canvas.clientHeight);
        }
    }
}

I haven't completely cleaned it up yet. 我还没有完全清理它。 Hence the redundant handles to the same id. 因此冗余处理相同的id。 But I'm sure you get the picture. 但我相信你能得到这张照片。

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

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