简体   繁体   English

如何制作可与计时器配合使用的交互式向后进度栏?

[英]How do I make an interactive backwards progress bar that works with a timer?

Is there any way that I could be able to make a progress bar go backwards? 有什么办法可以使进度条向后退? I have seen an example that told me to turn the progress bar 180 degrees, but that seems like a very dirty way to do it. 我看到了一个示例,该示例告诉我将进度条旋转180度,但这似乎是一种非常肮脏的方法。 What I'm looking for is so the progress bar can be time controlled, and so that a by pressing a button, I would add three seconds to the timer, thus pulling back the bar a bit (Say the timer is at 7 seconds, pressing the button would add a second, bringing it to 8 seconds thus pulling back the timer a little bit). 我要寻找的是进度条可以控制的时间,因此,通过按一个按钮,我可以将计时器增加三秒钟,从而将进度条向后拉一点(例如,计时器为7秒,按下按钮会增加一秒钟,使其达到8秒钟,从而将计时器稍微拉回一点)。 If I could incorporate this with a countdown timer as well, it would be cool, but not 100% needed. 如果我也可以将其与倒数计时器结合使用,那将很酷,但并不需要100%。 Here is what I have so far. 这是我到目前为止所拥有的。 Changes that need to be made is so that the timer goes backwards instead of forwards, and so I could be able to add time on to it by pressing a button. 需要进行的更改是使计时器后退而不是前进,因此我可以通过按一个按钮来增加时间。

HTML: HTML:

Javascript: 使用Javascript:

$(document).ready(function(){
var interval = 2, //How much to increase the progressbar per frame
    updatesPerSecond = 1000/60, //Set the nr of updates per second (fps)
    progress =  $('progress'),
    animator = function(){
        progress.val(progress.val()+interval);
        $('#val').text(progress.val());
        if ( progress.val()+interval < progress.attr('max')){
           setTimeout(animator, updatesPerSecond);
        } else { 
            $('#val').text('Done');
            progress.val(progress.attr('max'));
        }
    }

setTimeout(animator, updatesPerSecond);
});

JSFiddle 的jsfiddle

Simply start with progress.val at its maximum, and decrement it. 只需从progress.val最大化开始,然后递减。 When it gets to 0, you're done. 当它变为0时,您就完成了。

1st: - We will create a new function for reverse 第一:-我们将为reverse创建一个新功能

2nd: - Add min="0" attribute to the progress bar 第二名:-将min="0"属性添加到进度条

3rd: - change progress value to 200 progress.val('200'); 第三:-将进度值更改为200 progress.val('200'); before running the setTimeout() 在运行setTimeout()

Finally: - change + to - on progress.val() - interval 最后:-将+更改为- progress.val() - interval

 $(document).ready(function(){ var interval = 2, //How much to increase the progressbar per frame updatesPerSecond = 1000/60, //Set the nr of updates per second (fps) progress = $('progress'), animator = function(){ progress.val(progress.val()+interval); $('#val').text(progress.val()); if ( progress.val()+interval < progress.attr('max')){ setTimeout(animator, updatesPerSecond); } else { $('#val').text('Done'); progress.val(progress.attr('max')); } }, reverse = function(){ progress.val(progress.val() - interval); $('#val').text(progress.val()); if ( progress.val() - interval > progress.attr('min')){ setTimeout(reverse, updatesPerSecond); } else { $('#val').text('Done'); progress.val(progress.attr('min')); } } progress.val('200'); setTimeout(reverse, updatesPerSecond); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <progress max="200" min="0" value="1"></progress> <div id="val"></div> 

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

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