简体   繁体   English

使用百分比值更新进度栏?

[英]Update Progress Bar Using Percentage Values?

I created a custom player built to be responsive. 我创建了一个自定义播放器来响应。 So far, everything works fine, except I can't seem to get the audio to play at the spot where the user clicked on the progress bar. 到目前为止,一切正常,除了我似乎无法在用户单击进度条的地方播放音频。 All tutorials I've seen for this use pixel values, whereas my player uses percentages, so when I set barSize = 100 + '%'; 我见过的所有教程都使用像素值,而我的播放器使用百分比,所以当我设置barSize = 100 + '%'; it breaks the code and I get an error message saying "Failed to set the currentTime property on HTMLMediaElement : The provided double value is non-finite". 它破坏了代码,并且我收到一条错误消息:“无法在HTMLMediaElement上设置currentTime属性:提供的double值是非限定的”。

HTML HTML

<!-- PROGRESS BAR -->
<div id="progress_bar">
    <div id="bar"></div>
</div>

JS CODE (for progress bar) JS CODE(用于进度栏)

// PROGRESS BAR

// audio display
var progress = document.getElementById('bar');
//click area
var audioBar = document.getElementById('progress_bar');
var barSize = 100 + '%';

audio.addEventListener('timeupdate', updateProgress, false);


function updateProgress() {
    if (audio.currentTime > 0) {
        var value = 0;
        value = Math.floor((100 / audio.duration) * audio.currentTime);

        progress.style.width = value + '%';
    }       
    else{
        $('#play').html('<img src="img/play.png" width="70" height="70" />');

        $(curTimeDisplay).html('0:00');

        progress.style.width = 0;
    }

}

// CHANGING TIME ON PROGRESS BAR

$('#progress_bar').bind('click', function (e) {
    var $div = $(e.target);
    var $display = $div.find('#progress_bar');

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

    //$('#bar').width(x);

    var newtime = x*audio.duration/barSize;

    audio.currentTime = newtime;

    progress.style.width = x + '%';
});

CSS CSS

#progress_bar {
border:1px solid #500012;
margin:1em 0;
cursor:pointer;
background:#f2f2f2;
width:100%;
height:20px;
display:inline-block;
padding:0;
}

#bar {
background:#77001A;
height:20px;
display:inline-block;
float:left;
width:0%;
}

Any help is appreciated :) Thanks! 任何帮助表示赞赏:)谢谢!

The clue is in the error message. 提示在错误消息中。 It wants a double value and your barSize var is calculated as a string when you initialize it. 它需要一个双精度值,并且您将barSize var初始化时作为字符串进行计算。 This happens when you add the + '%' to any number - javascript will change that from a numeric to a string value. 当您将+'%'添加到任何数字时,就会发生这种情况-javascript会将其从数字更改为字符串值。 Doing math operations on a string will give you 'NaN'. 对字符串进行数学运算将得到“ NaN”。

But in this case, you want to advance the media proportionate to where the click occurred, so replace barSize with the pixel width of the total bar area. 但是在这种情况下,您想按点击发生的位置按比例增加媒体,因此将barSize替换为整个bar区域的像素宽度。 $('#progress_bar').width() should give you what you need. $('#progress_bar')。width()应该给您您所需要的。 You will probably want to move the operands of the expression around like so... 您可能希望像这样移动表达式的操作数。

Replace 更换

var newtime = x*audio.duration/barSize;

With

var newtime = (x / $('#progress_bar').width()) * audio.duration;

Get to know the console and debugging tools in your browser. 了解浏览器中的控制台和调试工具。 Adding breakpoints and watches is super helpful with code like this. 添加断点和监视对于这样的代码非常有帮助。

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

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