【发布时间】:2020-12-22 18:45:03
【问题描述】:
我不太确定我在这里使用 requestAnimationFrame 出了什么问题,因为它似乎可以正常工作,但它抛出了这个错误。
未捕获的 TypeError:无法在“窗口”上执行“requestAnimationFrame”:作为参数 1 提供的回调不是函数。
我正在尝试每 3000 毫秒将一个元素旋转 90 度,然后淡入/淡出其他一些元素。这可能不是最优雅的方式,我怀疑不是,我对 JS 的理解相当基础。因此,如果有人能解释导致此错误的原因,我将不胜感激。
var iteration = 0,
degrees = 0;
function rotate_by_90(degrees, iteration) {
if(iteration <= 3) {
var deg = degrees - 90,
new_deg = deg;
setTimeout(function() {
$('#rotate').css('transform', 'translateZ(-60px) rotateX(' + deg + 'deg');
console.log(new_deg + ' degrees');
iteration++;
console.log(iteration + ' iteration');
requestAnimationFrame(rotate_by_90(new_deg, iteration));
}, 3000);
} else {
$('#rotating_text').fadeOut();
$('#animation_overlay').fadeIn();
}
};
requestAnimationFrame(rotate_by_90(degrees,iteration));
HTML
<div id="rotating_text" class="rotating_text">
<span>Some text </span>
<div class="rotate_container">
<div id="rotate" class="rotate">
<span>thing 1</span>
<span>thing 2</span>
<span>thing 3</span>
<span>thing 4</span>
</div>
</div>
</div>
<div id="animation_overlay" class="animation_overlay">
<span>Some stuff</span>
</div>
另外,如果有人可以提出更好的方法(包括在整个事情完成后循环重新开始,那将不胜感激!)
【问题讨论】:
标签: javascript jquery typeerror requestanimationframe