- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
请参阅以下代码片段。
它会在单击按钮时创建加载程序。但是动画并不流畅。
我最近读到有关requestAnimationFrame
函数的内容,它可以完成这项工作。但是我如何使用它来完全替换 setInterval
,因为无法在 requestAnimationFrame
函数中指定时间。
可以和setInterval
一起使用吗?
let idx = 1;
const timetoEnd = 5000;
function ProgressBar(width){
this.width = width || 0;
this.id = `pBar-${idx++}`;
this.create = () => {
let pBar = document.createElement('div');
pBar.id = this.id;
pBar.className = `p-bar`;
pBar.innerHTML = `<div class="loader"></div>`;
return pBar;
};
this.animator = () => {
let element = document.querySelector(`#${(this.id)} div`);
if(this.width < 100){
this.width++;
element.style.width = `${this.width}%`;
} else {
clearInterval(this.interval);
}
};
this.animate = () => {
this.interval = setInterval(this.animator, timetoEnd/100);
}
}
function addLoader (){
let bar1 = new ProgressBar(40);
let container = document.querySelector("#container");
container.appendChild(bar1.create());
bar1.animate();
}
.p-bar{
width: 400px;
height: 20px;
background: 1px solid #ccc;
margin: 10px;
overflow:hidden;
border-radius: 4px;
}
.p-bar .loader{
width: 0;
background: #1565C0;
height: 100%;
}
<input type="button" value="Add loader" onclick="addLoader()" />
<div id="container"></div>
最佳答案
你是对的,requestAnimationFrame
是在做动画时避免 UI 堵塞的推荐方法。
您可以记住开始时的绝对开始时间,而不是尝试在每一帧都这样做。然后,只需根据开始时间和当前时间之间的增量时间计算宽度即可。
此外,document.querySelector
被认为是一个相对“繁重”的操作,因此我添加了 this.element
以避免在每一帧执行它。
这里是如何计算新的宽度:((100 - this.startWidth)/timetoEnd) * deltaT + this.startWidth
100 - this.startWidth
是我们必须设置动画的总宽度(100 - this.startWidth)/timetoEnd
是每秒必须添加到 (1) 的宽度((100 - this.startWidth)/timetoEnd) * deltaT
是我们必须添加到 (1) 的宽度this.startWidth
px 移动到框架的宽度另请注意,其中一些计算是恒定的,不必在每一帧上计算,我将其留作练习:)
这是您稍微修改过的代码:
let idx = 1;
const timetoEnd = 5000;
function ProgressBar(startWidth){
this.startWidth = startWidth || 0;
this.id = `pBar-${idx++}`;
this.create = () => {
let pBar = document.createElement('div');
pBar.id = this.id;
pBar.className = `p-bar`;
pBar.innerHTML = `<div class="loader"></div>`;
return pBar;
};
this.animator = () => {
const deltaT = Math.min(new Date().getTime() - this.start, timetoEnd);
if(deltaT < timetoEnd){
const width = ((100 - this.startWidth) / timetoEnd) * deltaT + this.startWidth;
this.element.style.width = `${width}%`;
requestAnimationFrame(this.animator.bind(this))
}
};
this.animate = () => {
this.element = document.querySelector(`#${(this.id)} div`);
this.start = new Date().getTime();
this.animator();
}
}
function addLoader (){
let bar1 = new ProgressBar(40);
let container = document.querySelector("#container");
container.appendChild(bar1.create());
bar1.animate();
}
.p-bar{
width: 400px;
height: 20px;
background: 1px solid #ccc;
margin: 10px;
overflow:hidden;
border-radius: 4px;
}
.p-bar .loader{
width: 0;
background: #1565C0;
height: 100%;
}
<input type="button" value="Add loader" onclick="addLoader()" />
<div id="container"></div>
关于javascript - 如何将 requestAnimationFrame 与 setInterval 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52270477/
假设我们有一个功能/模块,可以增强网站。所以这并不是真正必要的,它使用 requestAnimationFrame,即 not supported in older browsers比如IE8/9。我
“requestAnimationFrame”方法在以下示例中不起作用。控制台不显示任何错误消息。那么这段代码的错误是什么?HTML:
我有以下 JavaScript: function animate() { // call other function offset += 3; if (offset > 15) offset =
我有一些基本脚本可以将文本左右滚动,我尝试将其从 timeout 转换为 requestAnimationFrame,但是,我无法制作它有效。 function slideHorizontal(e,
我遇到了这个奇怪的问题,我递增了 1,但是,弹出 javascript 窗口时出现的增量显示我已递增了 9 或 13,这两者都来自于我是否分别增加 1 或 -1。这是怎么回事? 这是 requestA
我试图了解有关 requestAnimationFrame 的一件事。 在下面的示例代码中,我运行了 60 次,这应该与您通常获得的典型每秒 60 帧相匹配,因此它应该持续 1 秒。然而我故意通过调用
有人可以分享一下在动画循环中应该在哪里进行 RequestAnimationFrame 调用吗?它应该在循环的开头还是循环的结尾?我已经多次看到它在循环开始时使用它,但是位于循环底部不是更好吗?这样在
我尝试为很多 svg 路径设置动画,但功能不起作用 错误: Uncaught RangeError: Maximum call stack size exceeded function dashOf
我有一张滑动图片沿着屏幕缓慢移动。一旦它们离开屏幕,它们就会背靠背靠在丝带上。因此,当您在网页上时一切正常,但当您离开并且其中一张图像超出范围时。它会被放回错误的地方。我认为这可能与 requestA
我在使用 requestAnimationFrame() 时遇到了性能问题。 考虑以下代码。这是一个简单的循环,每次时间增量大于 20 毫秒时,都会打印自上一帧以来的时间。 const glob_ti
根据我的阅读,requestAnimationFrame 可以判断浏览器何时失去焦点。发生这种情况时是否会触发某种事件?我正在寻找暂停和恢复与 requestAnimationFrame 相关的代码。
let pf = document.querySelectorAll('.pf'); for (let i of pf) { Object.assign(i.style, { left:
我最近看了 Jake Archibald 的演讲。他在演讲中举了一个例子,他说使用 requestAnimationFrame 两次来延迟应用 CSS 样式来执行 CSS 动画。 参见https://
我正在使用 requestAnimationFrame 为视频流设置动画,我知道请求动画在后台不起作用,所以有什么方法可以让它在后台运行。 我知道我们也可以使用 setInterval 或 setTi
我是编程新手,面对这个功能: function update(time = 0) { console.log(time); requestAnimationFrame(update);
我正在尝试使用 JavaScript 和 requestAnimationFrame 制作一个简单的计时器(从 0 开始计数)。单击某些内容时,我想从 0 开始计时。目前我的代码在单击按钮时显示计时器
如果我有以下代码: window.requestAnimationFrame(animation1); //animation1 takes more than 16.67ms, misses the
我有一个带有 requesetAnimationFrame 的动画,当我重播它时,它变得越来越快,直到它变得不可见。 function tirer(){ var missile=documen
我有一个问题: document.querySelector('div').addEventListener('mousedown', function(){ hello = true
我有一种情况,我想在 1 秒内将 600px 宽的 div 的宽度设置为 0px。为此,我可以使用 requestAnimationFrame()。但我真的不能 100% 确定动画是否需要 1 秒。
我是一名优秀的程序员,十分优秀!