假设你有一个很长的动画,你正在改变width
:jQuery的动画:改变动画参数中旬动画
var myTargetWidth = 500;
$(el).animate({ "width" : myTargetWidth }, 5000);
动画是异步的所以你的代码继续。 。 。 几秒钟后,您决定将目标width
更改为300
。 。 。 此时动画仍在运行。 。 。
如何将targetWidth更改为运行动画上的其他值?
假设你有一个很长的动画,你正在改变width
:jQuery的动画:改变动画参数中旬动画
var myTargetWidth = 500;
$(el).animate({ "width" : myTargetWidth }, 5000);
动画是异步的所以你的代码继续。 。 。 几秒钟后,您决定将目标width
更改为300
。 。 。 此时动画仍在运行。 。 。
如何将targetWidth更改为运行动画上的其他值?
一种选择是使用了jQuery animate
函数(API) 提到step
功能动画正在运行时,检查的条件。
实施例的jsfiddle:http://jsfiddle.net/GweLA/13/
JS
var myTargetWidth = 500;
$(document).ready(function(){
$('.sample').animate({ "width" : myTargetWidth },{
duration : 5000,
step: function(now, fx) {
if($(this).width() > 200){
myTargetWidth = 300;
$(this).stop().animate({ "width" : myTargetWidth },1000);
}
}
});
});
CSS
.sample{
width:20px;
height:100px;
background-color:#cccccc;
}
HTML
<div class="sample">
width is supposed to be animated till 500 but it stops at 300
</div>
解决方案2:
一些研究,我发现,我们可以通过修改传递给阶跃函数来控制动画fx
参数的start
和end
属性之后。这种平滑的动画,但不是一个很干净的做法。
例的jsfiddle:http://jsfiddle.net/GweLA/57/
JS
var myTargetWidth = 500;
var isExecuted = false;
$(document).ready(function(){
$('.sample').animate({ "width" : myTargetWidth },{
duration : 5000,
queue : false,
step: function(now, fx) {
//So that fx.start and fx.end is set only once
if($(this).width() > 200 && $(this).width() < 203){
if(!isExecuted){
fx.start = now-65;
fx.end = 300;
}
isExecuted = true;
}
}
});
});
您可以使用组合.stop() - 停止动画。
:animated选择 - 如果当前元素被动画,检查..
试试这个
HTML
<div class="a">
</div>
<button id="check">Check Animation </button>
的Javascript
var myTargetWidth = 300;
var $el = $('.a')
$el.animate({
"width": myTargetWidth
}, 5000);
$('#check').on('click', function() {
var newHeight = 300;
if ($('.a:animated')) {
$el.stop().animate({
"height": newHeight
}, 5000);
}
});
综观jQuery的github上effects.js你将不得不转让后改变变量问题
来源:https://github.com/jquery/jquery/blob/master/src/effects.js
注意:注意.animate的分配新建分配FY和对象创建动画(工作马)
animate: function(prop, speed, easing, callback) {
var empty = jQuery.isEmptyObject(prop),
optall = jQuery.speed(speed, easing, callback),
doAnimation = function() {
// Operate on a copy of prop so per-property easing won't be lost
var anim = Animation(this, jQuery.extend({}, prop), optall);
// Empty animations resolve immediately
if (empty) {
anim.stop(true);
}
};
return empty || optall.queue === false ?
this.each(doAnimation) :
this.queue(optall.queue, doAnimation);
}
//动画在e ffects.js
function Animation(elem, properties, options) {
var result,
index = 0,
length = animationPrefilters.length,
deferred = jQuery.Deferred().always(function() {
// don't match elem in the :animated selector
delete tick.elem;
}),
tick = function() {
var currentTime = fxNow || createFxNow(),
remaining = Math.max(0, animation.startTime + animation.duration - currentTime),
// archaic crash bug won't allow us to use 1 - (0.5 || 0) (#12497)
temp = remaining/animation.duration || 0,
percent = 1 - temp,
index = 0,
length = animation.tweens.length;
for (; index < length ; index++) {
animation.tweens[ index ].run(percent);
}
deferred.notifyWith(elem, [ animation, percent, remaining ]);
if (percent < 1 && length) {
return remaining;
} else {
deferred.resolveWith(elem, [ animation ]);
return false;
}
},
animation = deferred.promise({
elem: elem,
props: jQuery.extend({}, properties),
opts: jQuery.extend(true, { specialEasing: {} }, options),
originalProperties: properties,
originalOptions: options,
startTime: fxNow || createFxNow(),
duration: options.duration,
tweens: [],
createTween: function(prop, end) {
var tween = jQuery.Tween(elem, animation.opts, prop, end,
animation.opts.specialEasing[ prop ] || animation.opts.easing);
animation.tweens.push(tween);
return tween;
},
stop: function(gotoEnd) {
var index = 0,
// if we are going to the end, we want to run all the tweens
// otherwise we skip this part
length = gotoEnd ? animation.tweens.length : 0;
for (; index < length ; index++) {
animation.tweens[ index ].run(1);
}
// resolve when we played the last frame
// otherwise, reject
if (gotoEnd) {
deferred.resolveWith(elem, [ animation, gotoEnd ]);
} else {
deferred.rejectWith(elem, [ animation, gotoEnd ]);
}
return this;
}
}),
props = animation.props;
propFilter(props, animation.opts.specialEasing);
for (; index < length ; index++) {
result = animationPrefilters[ index ].call(animation, elem, props, animation.opts);
if (result) {
return result;
}
}
createTweens(animation, props);
if (jQuery.isFunction(animation.opts.start)) {
animation.opts.start.call(elem, animation);
}
jQuery.fx.timer(
jQuery.extend(tick, {
anim: animation,
queue: animation.opts.queue,
elem: elem
})
);
// attach callbacks from options
return animation.progress(animation.opts.progress)
.done(animation.opts.done, animation.opts.complete)
.fail(animation.opts.fail)
.always(animation.opts.always);
}
因此,我认为你将不得不.stop与改变的变量进行重新排队的动画,你在访问中关闭任何变量的希望不大(有人纠正我,请)
我加入这里的例子:
尝试点击 '点击这里' 元素中旬动画...
我使用clearQueue()
和stop()
函数。
CSS #block {width:100px; height:100px;背景:红色; }
HTML
<p>
<a id="clickme">Click here</a>
</p>
JQUERY
$('#clickme').not('.again').on('mouseup',function() {
$(this).addClass('again');
$('#block').animate({
width: '400'
}, 5000, function() {
// Animation complete.
});
});
$('.again').live('mousedown', function() {
$(this).removeClass('again');
$('#block').clearQueue().animate({
width: '200',
}, 500, function() {
$('#block').stop(true, false);
});
});
<div id="block" />
你尝试过改变myTargetWidth中旬动画,看看会发生什么?我认为它不会做任何事情,因为内部它将被存储,在这种情况下,您将不得不查看是否可以解密jQuery源以查看如何访问该中间流参数。 –
是的,如果是的话,你应该接受一个正确的答案!你有很多开放的问题@eeejay – superUntitled
是的eeejay ...标记为您工作的答案是正确的,这是整个网站的重点。另外看看提到步骤(而不是.stop)的答案......它几乎肯定比你的问题更适合.stop回答 –