【问题标题】:Animate HTML5 Progress Bar with JavaScript使用 JavaScript 动画 HTML5 进度条
【发布时间】:2012-08-17 11:36:24
【问题描述】:

我正在尝试做一个简单的 HTML5 进度条。当用户单击一个按钮时,我希望它(onclick)开始动画,以便它在三秒内从 0-100(满)。它应该是一些非常基本的 Javascript,我希望将其放入文档的头部。

与此非常相似: HTML5 progress bar animation

我只需要它 onclick 而不是 onload 和一个按钮。它只是一个标准的 HTML 进度条,所以它看起来像这样:

<progress id="progress" value="0" min="0" max="100">0%</progress>
<input type="button" value="button" onclick="[make_progress_bar_go_from_0_to_100]">

【问题讨论】:

    标签: javascript html animation


    【解决方案1】:

    您所需要的只是您发布的问题中的这个示例: http://jsfiddle.net/526hM/

    将它附加到 onload 的部分是这样的:

    $(document).ready(function(){
        ...
        setTimeout(animator, updatesPerSecond);
    }
    

    您需要做的就是将此animator 函数附加到按钮上。要让效果在 3 秒内发生,请更新时间变量。

    HTML:

    <progress max="200" value="0"></progress>
    <button id="theButton">Start!</button>
    

    脚本:

    $(document).ready(function(){
        var msecsPerUpdate = 1000/60; // # of milliseconds between updates, this gives 60fps
        var progress =  $('progress');
        var duration = 3;             // secs to animate for
        var interval = progress.attr('max')/(duration*1000/msecsPerUpdate);
    
        var animator = function(){
                progress.val(progress.val() + interval);
                if (progress.val() + interval < progress.attr('max')){
                   setTimeout(animator, msecsPerUpdate);
                } else {
                    progress.val(progress.attr('max'));
                }
            }
    
        $('#theButton').click(function(e) {
            e.preventDefault();
            animator();
        });
    });​
    

    演示:http://jsfiddle.net/526hM/28/

    【讨论】:

    • 我不能让它工作,我不知道为什么。 jsfiddle 中的代码直接插入到嵌套在脚本标签中的文档的头部,但是它仍然在刷新我的页面并且什么也不做。 lrroberts0122.github.com/WDF/html5_form.html 是一个包含我所有代码的页面,包括插入的 js。感谢您的帮助。
    • 你的页面上有 jQuery 吗?如果页面也可以公开访问,您也可以发布页面。
    • 您需要在您的页面中包含 jQuery,它是一个使 JavaScript 编码更容易的库。您需要做的就是在头中上面的脚本之前包含另一个脚本标签:&lt;script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"&gt;&lt;/script&gt;
    • 嗯。它仍然无法正常工作。它不断刷新它。此处更新版本:creativezeus.com/html5_form.html
    • 那个链接失效了。我已经将 jQuery 添加到我自己的页面版本中并且它可以工作,唯一的问题是通过按下按钮提交 html 的表单(我不知道 buttons 这样做了,直到)。要解决这个问题,您必须阻止点击时的默认操作,因为我已经修改了上面的代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-19
    • 2013-04-03
    • 2013-11-18
    • 2014-04-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多