jQuery动画系统是基于队列机制的,当对一个元素应用多个动画时,默认情况下这些动画会按顺序执行。但有时会出现多个动画同时执行的情况,这通常是由于以下原因:
stop()
方法中断了当前动画animate()
方法的queue: false
选项当另一个动画正在进行时,新的jQuery动画可能不会按预期执行,主要原因是:
stop()
方法时参数设置不正确// 默认情况下动画会排队执行
$("#element").animate({left: '100px'}, 1000);
$("#element").animate({top: '100px'}, 1000); // 会等上一个动画完成
// 强制立即执行(不推荐,可能导致冲突)
$("#element").animate({left: '100px'}, {duration: 1000, queue: false});
$("#element").animate({top: '100px'}, {duration: 1000, queue: false});
// 停止当前动画,清除队列,跳转到结束状态
$("#element").stop(true, true);
// 停止当前动画,保留队列
$("#element").stop();
// 停止当前动画,清除队列,保留当前状态
$("#element").stop(true);
$("#element").animate({left: '100px'}, 1000, function() {
// 第一个动画完成后执行第二个动画
$(this).animate({top: '100px'}, 1000);
});
$("#element").animate({left: '100px'}, 1000)
.promise()
.done(function() {
$(this).animate({top: '100px'}, 1000);
});
stop(true)
清除所有动画通过合理管理jQuery动画队列和正确使用动画控制方法,可以避免动画冲突问题,实现流畅的用户体验。
没有搜到相关的文章