javascript + jquery + setinterval + animation
javascript + jquery + setinterval + animation
我在使用setInterval和jquery animate时遇到了问题。 这是我的代码:
function slides1() {
...
$("table#agah1").animate({
"left": first1
}, "slow");
$("table#agah2").animate({
"left": first2
}, "slow");
}
$(function () {
cyc = setInterval("slides1()", 3000);
});
当切换到另一个浏览器选项卡并在一段时间后返回时,动画会毫不拖延地继续执行此操作,在我离开选项卡的时间内,然后正确操作。我也添加这些没有任何运气:
$(window).focus(function () {
jQuery.fx.off = false;
cyc = setInterval("slides1()", 3000);
});
$(window).blur(function () {
jQuery.fx.off = true;
window.clearInterval(cyc);
});
较新版本的jQuery使用
requestAnimationFrame
回调来处理效果,浏览器不会在隐藏选项卡上处理效果。
与此同时,您的setInterval
事件仍在发生,导致更多动画排队。
setInterval
来计划动画,不如使用最后一个动画的"完成回调"来触发下一个循环,必要时使用setTimeout
。
function slides1() {
...
$("table#agah1").animate({
"left": first1
}, "slow");
$("table#agah2").animate({
"left": first2
}, "slow", function() {
setTimeout(slides1, 2000); // start again 2s after this finishes
});
}
$(function () {
setTimeout(slides1, 3000); // nb: not "slides1()"
});
这将确保动画间延迟与动画本身之间存在紧密耦合,并避免setTimeout
与动画不同步的任何问题。
相关文章:
- paper.js animation along a path
- Jquery animation/fadeIn/fadeOut
- Animation cuts off multiple lines of text in <p>
- jquery animation.scrollTop()在mdl链接中不起作用
- Stop ::-webkit-progress-value animation
- 将 JQuery Animation 转换为 Javascript/CSS3
- d3 animation wordCloud flyin
- Angular Animation类中Greensock JavaScript的非平滑tweening
- AngularJS location.hash animation
- Geocoding和Animation.drop与超时不兼容
- Jquery Ajax beforeSend loading animation
- 函数jQuery.animation()运行了两次行,但不起作用
- CSS animation and Jquery2
- SVG animation with SMIL
- CSS animation and Jquery
- jQuery / CSS Animation - FF和Chrome中的暂停会跳回并启动不正确
- JavaScript animation
- jquery .append iframe animation
- Snap.svg animation stop() 不会停止动画
- javascript google map setcenter and setzoom with animation