Javascript计时器进度条

2023-11-24

我有定时器功能,有进度条。它工作得很好,但我想制作一个平滑的进度条动画,帧速率为 60 FPS

function started(duration) {
    var TotalSeconds = 40;
    var documentWidth = $(document).width();
    var start = Date.now();
    var intervalSetted = null;

    function timer() {
        var diff = duration - (((Date.now() - start) / 1000) | 0);
        var seconds = (diff % 60) | 0;
        seconds = seconds < 10 ? "0" + seconds : seconds;
        $('#timer').html("00:" + seconds);
        var progresBarWidth = (seconds * documentWidth / TotalSeconds);

        $('#progress').css({
            width: progresBarWidth + 'px'
        });

        if (diff <= 0) {
            clearInterval(intervalSetted);
        }
    }

    timer();
    intervalSetted = setInterval(timer, 1000);
}

started(40);

如何实现 60FPS 动画?

这是我的代码的演示:JSFiddle!


您可以使用 CSS3 动画。 我编写了一些示例代码,其中显示了一个进度条,该进度条使用您可以选择的持续时间进行倒计时。您还可以在动画完成时进行回调。

CSS3 动画是硬件加速的,因此您在使用它时将获得最流畅的体验。

/*
 *  Creates a progressbar.
 *  @param id the id of the div we want to transform in a progressbar
 *  @param duration the duration of the timer example: '10s'
 *  @param callback, optional function which is called when the progressbar reaches 0.
 */
function createProgressbar(id, duration, callback) {
  // We select the div that we want to turn into a progressbar
  var progressbar = document.getElementById(id);
  progressbar.className = 'progressbar';

  // We create the div that changes width to show progress
  var progressbarinner = document.createElement('div');
  progressbarinner.className = 'inner';

  // Now we set the animation parameters
  progressbarinner.style.animationDuration = duration;

  // Eventually couple a callback
  if (typeof(callback) === 'function') {
    progressbarinner.addEventListener('animationend', callback);
  }

  // Append the progressbar to the main progressbardiv
  progressbar.appendChild(progressbarinner);

  // When everything is set up we start the animation
  progressbarinner.style.animationPlayState = 'running';
}

addEventListener('load', function() {
  createProgressbar('progressbar1', '40s');
  createProgressbar('progressbar2', '30s');
  createProgressbar('progressbar3', '20s', function() {
    alert('20s progressbar is finished!');
  });
  createProgressbar('progressbar4', '10s', function() {
    alert('10s progressbar is finished!');
  });
});
.progressbar {
  width: 80%;
  margin: 25px auto;
  border: solid 1px #000;
}
.progressbar .inner {
  height: 15px;
  animation: progressbar-countdown;
  /* Placeholder, this will be updated using javascript */
  animation-duration: 40s;
  /* We stop in the end */
  animation-iteration-count: 1;
  /* Stay on pause when the animation is finished finished */
  animation-fill-mode: forwards;
  /* We start paused, we start the animation using javascript */
  animation-play-state: paused;
  /* We want a linear animation, ease-out is standard */
  animation-timing-function: linear;
}
@keyframes progressbar-countdown {
  0% {
    width: 100%;
    background: #0F0;
  }
  100% {
    width: 0%;
    background: #F00;
  }
}
<div id='progressbar1'></div>
<div id='progressbar2'></div>
<div id='progressbar3'></div>
<div id='progressbar4'></div>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Javascript计时器进度条 的相关文章

随机推荐

  • Python scapy 导入错误

    如果我在 python 源文件中包含以下行 from scapy all import 我收到这个错误 from scapy all import ImportError No module named all 这在 Console 和 I
  • 使用点路径键字符串访问 Ruby 哈希

    Rails I18n 库将 YAML 文件转换为可使用 t 函数通过点路径调用访问的数据结构 t one two three four 有谁知道如何使用 Ruby Hash 来做到这一点 或者只能直接通过 YAML 对象实现 只需在路径中的
  • MOV AX,CS 和 MOV DS,AX 的概念

    谁能解释一下这三个指令的功能吗 ORG 1000H MOV AX CS MOV DS AX 理论上我知道代码 数据和额外的段是什么 但是 它们在这个程序中是如何实现的 为什么整个段被移到另一个段中 MOV AX CS and MOV DS
  • 为什么识别 XOR 运算符的反向传播神经网络需要偏置神经元?

    我发布了一个question昨天关于我的 XOR 运算符的反向传播神经网络遇到的问题 我做了更多的工作 意识到这可能与没有偏置神经元有关 我的问题是 偏置神经元的一般作用是什么 它在识别 XOR 运算符的反向传播神经网络中的作用是什么 是否
  • nginx 上游代理的后备

    我在图像服务器集群前面有一个 nginx 实例 upstream img farm 1 server 10 0 1 1 server 10 0 1 2 server 10 0 1 3 server 10 0 1 4 etc location
  • 模块模式-如何将一个模块的代码拆分到不同的js文件中?

    对于模块模式 我正在做类似的事情 function namespace tons of code blabla window myGlobalNamespace 如何拆分代码 我可以想到几种方法 例如使用命名空间的层次结构 或者通过以下方式
  • 对数据帧的成对行进行操作

    我在 R 中有一个数据框 我想对所有行对执行计算 有没有比使用嵌套 for 循环更简单的方法 为了使这一点具体化 考虑一个包含十行的数据框 我想计算所有 45 个可能的对之间的分数差 gt data frame ID 1 10 Score
  • Visual Studio 是否可以默认折叠摘要部分

    Visual Studio 是否可以默认折叠方法和类的摘要部分 或者是否有一个命令可以折叠所有摘要部分而不折叠方法本身 我在下面提供了一个摘要部分的示例 Collapsed example Expanded Example 您将必须使用宏
  • Android 蓝牙 - 无法连接

    我正在开发一个使用蓝牙连接到设备并发送 接收数据的应用程序 我正在使用 Nexus One 手机进行所有测试 我从未能够建立从我的手机到任何设备的 SPP 串行端口 连接 但是 我have能够使用相当于 PuTTY 的 Mac 从设备 我的
  • 如何获得有关 antlr4-maven-plugin 的帮助

    antlr4 maven plugin 似乎不是 Antlr4 网站上的文档 这可能不会给你带来任何好处 就像对我一样 尝试这个 mvn org antlr antlr4 maven plugin help Ddetail true 生产
  • 跨 Java 和 Javascript 模板语言?

    似乎有很多 Java 模板语言 例如 JSP JSTL Freemarker Velocity 等 和 Javascript 模板语言 例如 Mustache Ext 的 XTemplate Jquery 模板等 但是是否有一种模板语言具有
  • 绘制圆锥时断开的曲面

    我想用 python 绘制曲面 z 1 x y 和 4z x y 我写了这段代码 from mpl toolkits mplot3d import axes3d import matplotlib pyplot as plt import
  • 如何在打印从文件读取的行时跳过额外的换行符?

    我正在从 stdin 读取 python 程序的输入 我已将一个文件对象分配给 stdin 输入的行数事先是未知的 有时程序可能只有 1 行 100 行 甚至根本没有行 import sys sys stdin open Input txt
  • 使用自定义键盘中可见的候选视图重新调整 UI

    我正在使用自定义键盘 我在 onCreateCandidatesView 中设置了 setCandidatesViewShown true 函数 问题是 UI 没有正确调整 任何帮助将不胜感激 以下是我所做的 Override public
  • 为什么是 HTML 十进制和 HTML 十六进制?

    我已经尝试在 Google 上搜索很长一段时间来寻找为什么 HTML 实体可以以 HTML 十进制或 HTML 十六进制编译的答案 所以我的问题是 HTML 十进制和 HTML 十六进制有什么区别 为什么有两个系统做同样的事情 最初 HTM
  • HTTP Get:只下载标头? (不支持HEAD)

    在我的代码中 我使用一些 Http Get 请求以流的形式下载一些文件 我使用以下代码 public String getClassName String url throws ClientProtocolException IOExcep
  • 来自 MongoDB ISODate 的 Pandas DatetimeIndex

    我在处理时间 时区时遇到一些困难 我有以下形式的原始 JSON 数据 Date 28 Sep 2009 00 00 00 然后将该数据加载到 MongoDB 中 并将日期的字符串表示形式转换为JavaScript 日期对象 这个转换为UTC
  • 是否可以将新文件写入 iOS 应用程序中的捆绑资源目录?

    是否可以将新文件写入 iOS 应用程序中的捆绑资源目录 不 无法在捆绑目录中写入 因为捆绑目录是使用 SSL 证书签名的代码 并且您无法破坏它 但是你可以轻松地在 iPhone 应用程序的文档目录中写入 您可以使用以下命令获取文档目录路径
  • 将强制转换应用于整数和浮点除法的结果:这里发生了什么?

    我是一个初学者 有些东西对我来说没有多大意义 请好心人解释一下我哪里出错了 如果之前有人问过这个问题 我很抱歉 这里小数点的存在意味着这些是使用浮点除法来计算的 System out println 1 3 0 this prints 0
  • Javascript计时器进度条

    我有定时器功能 有进度条 它工作得很好 但我想制作一个平滑的进度条动画 帧速率为 60 FPS function started duration var TotalSeconds 40 var documentWidth document