Javascript 单击按钮时暂停了requestAnimationFrame
是否可以暂停Javascript 单击按钮时暂停了requestAnimationFrame,javascript,animation,requestanimationframe,Javascript,Animation,Requestanimationframe,是否可以暂停请求动画帧? 当我单击特定按钮时,动画必须暂停。 在需要继续播放动画时,再次单击该按钮 public addProgressbar() { const progressbar = $('.progressbar__value'); const max = this.$progressMax; const time = (1000 / max) * 5; let start = null; this.progress = 0; cons
请求动画帧
?
当我单击特定按钮时,动画必须暂停。
在需要继续播放动画时,再次单击该按钮
public addProgressbar() {
const progressbar = $('.progressbar__value');
const max = this.$progressMax;
const time = (1000 / max) * 5;
let start = null;
this.progress = 0;
const step = timestamp => {
if (start === null) {
start = timestamp;
}
this.progress = timestamp - start;
progressbar[0].style.width = Math.min(this.progress / 50, 2000).toFixed() + '%';
if (this.progress < 5000) {
requestAnimationFrame(step);
}
};
setTimeout(() => {
window.requestAnimationFrame(step);
}, 1000);
}
public addProgressbar(){
常量progressbar=$('.progressbar__值');
const max=此值。$progressMax;
常数时间=(1000/最大值)*5;
让start=null;
这个进度=0;
常量步骤=时间戳=>{
如果(开始===null){
开始=时间戳;
}
this.progress=时间戳-开始;
progressbar[0].style.width=Math.min(this.progress/502000).toFixed();
如果(该进度小于5000){
请求动画帧(步骤);
}
};
设置超时(()=>{
window.requestAnimationFrame(步骤);
}, 1000);
}
如果问题中的代码有效,我不明白你为什么要问这个问题。代码很有效,不是吗?是的,我编辑了文本。代码运行得很好,但我需要暂停并继续动画的功能。requestAnimationFrame
不像setInterval
,它不会连续运行。换句话说:你不能“暂停”一个动画帧的请求,你只能取消它。可以避免请求附加动画帧。这就是你的意思吗?当我取消动画帧时,例如在50%时,我可以在50%时再次开始吗?如果是的话,那对我来说是个好办法