在许多情况下,我希望动画可以同步执行。特别是当我想要制作一系列连续动画时。JQuery同步动画
有没有简单的方法来使jQuery animate
函数调用同步?
我想过的唯一方法是在动画完成时将标志设置为true并等待此标志。
在许多情况下,我希望动画可以同步执行。特别是当我想要制作一系列连续动画时。JQuery同步动画
有没有简单的方法来使jQuery animate
函数调用同步?
我想过的唯一方法是在动画完成时将标志设置为true并等待此标志。
jQuery无法制作同步动画。
请记住,JavaScript在浏览器的UI线程上运行。
如果制作同步动画,浏览器将冻结,直到动画完成。
为什么你需要这样做?
你应该使用jQuery的回调参数,并继续你的方法代码回调,像这样:
function doSomething() {
var thingy = whatever;
//Do things
$('something').animate({ width: 70 }, function() {
//jQuery will call this method after the animation finishes.
//You can continue your code here.
//You can even access variables from the outer function
thingy = thingy.fiddle;
});
}
这就是所谓的闭包。
我同意@SLaks对这一个。您应该使用jQuery的回调来创建您的同步动画。你基本上可以采取任何你有你的当前动画和分裂它就像这样:
$yourClass = $('.yourClass');
$yourClass.animate({
width: "70%"
}, 'slow', null, function() {
$yourClass.animate({
opacity: 0.4
}, 'slow', null, function() {
$yourClass.animate({
borderWidth: "10px"
});
});
});
你可能会理解它如何扩展到20个动作时的外观......也请参阅我对@SLaks的回应。 – 2009-10-20 20:10:38
如果您不喜欢它的外观,请不要缩进回调(或者只缩进一点)。这是做到这一点的唯一方法。 – SLaks 2009-10-20 21:38:08
jQuery提供了一个“台阶”回调其.animate()方法。你可以连接到这个做同步动画:
jQuery('#blat').animate({
// CSS to change
height: '0px'
},
{
duration: 2000,
step: function _stepCallback(now,opts) {
// Stop browser rounding errors for bounding DOM values (width, height, margin, etc.)
now = opts.now = Math.round(now);
// Manipulate the width/height of other elements as 'blat' is animated
jQuery('#foo').css({height: now+'px'});
jQuery('#bar').css({width: now+'px'});
},
complete: function _completeCallback() {
// Do some other animations when finished...
}
}
我想你应该看看jQuery的queue()方法。
队列()的文档不仅解释了jQuery动画并不真正阻止用户界面,而是实际上将它们排队在一起。
它也有办法提供,让您的动画和函数调用顺序(这是我最好的,你的意思是“同步”什么的理解),如:
$("#myThrobber")
.show("slow") // provide user feedback
.queue(myNotAnimatedMethod) // do some heavy duty processing
.hide("slow"); // provide user feedback (job's
myNotAnimatedMethod() { // or animated, just whatever you want anyhow...
// do stuff
// ...
// tells #myThrobber's ("this") queue your method "returns",
// and the next method in the queue (the "hide" animation) can be processed
$(this).dequeue();
// do more stuff here that needs not be sequentially done *before* hide()
//
}
这当然是矫枉过正与异步处理;但如果你的方法实际上是一个普通的旧的同步JavaScript方法,那可能是这样做的。
希望这有助于为我的英文不好对不起......
同步意味着'fwrite(big_data)'返回AFTER'fwrite'完成写入。异步意味着'fwrite'会立即返回,并且写入大数据是在并行/其他时间完成的。 – 2010-04-27 07:00:58
然后我想我说得对:如果你喜欢动画$(“#myThrobber”),例如在fwrite(big_data)后同步执行,你可以用下面两条语句来实现: (1)$( 。 “#myThrobber”)队列(的fwrite(big_data))动画(//无论); (2)用$(“#myThrobber”)结束你的fwrite()方法。dequeue(); 注意:我的示例在拨打 $(this).dequeue()时出错了。 它应该是: $(“#myThrobber”)。dequeue(); 忘了,因为我从代码所需的代码创建 .queue(jQuery.proxy(myNotAnimatedMethod,this)) 对不起,这种混淆。 – 2010-04-27 11:34:31
我碰到这个http://lab.gracecode.com/motion/ 真的很容易使用和组合使用jQuery的伟大工程。
编辑 链接似乎死了。如果我已经正确地追踪了那些备忘录档案,代码是https://github.com/feelinglucky/motion
我希望它有英文文档。无论如何,谢谢。 – 2010-08-01 23:05:24
@Elazar:你可以谷歌翻译它;使它有点可以理解;) – 2011-06-30 13:00:45
jQuery可以制作同步动画。看看这个:
function DoAnimations(){
$(function(){
$("#myDiv").stop().animate({ width: 70 }, 500);
$("#myDiv2").stop().animate({ width: 100 }, 500);
});
}
这是一个模块,我放在一起,以帮助顺序运行动画。
用法:
var seq = [
{ id: '#someelement', property:'opacity', initial: '0.0', value:'1.0', duration:500 },
{ id: '#somethingelse', property:'opacity', value:'1.0', duration: 500 }
];
Sequencer.runSequence(seq);
var Sequencer = (function($) {
var _api = {},
_seq = {},
_seqCount = 0,
_seqCallback = {};
function doAnimation(count, step) {
var data = _seq[count][step],
props = {};
props[data.property] = data.value
$(data.id).animate(props, data.duration, function() {
if (step+1 < _seq[count].length) {
doAnimation(count, ++step);
} else {
if (typeof _seqCallback[count] === "function") {
_seqCallback[count]();
}
}
});
}
_api.buildSequence = function(id, property, initial, steps) {
var newSeq = [],
step = {
id: id,
property: property,
initial: initial
};
$.each(steps, function(idx, s) {
step = {};
if (idx == 0) {
step.initial = initial;
}
step.id = id;
step.property = property;
step.value = s.value;
step.duration = s.duration;
newSeq.push(step);
});
return newSeq;
}
_api.initSequence = function (seq) {
$.each(seq, function(idx, s) {
if (s.initial !== undefined) {
var prop = {};
prop[s.property] = s.initial;
$(s.id).css(prop);
}
});
}
_api.initSequences = function() {
$.each(arguments, function(i, seq) {
_api.initSequence(seq);
});
}
_api.runSequence = function (seq, callback) {
//if (typeof seq === "function") return;
_seq[_seqCount] = [];
_seqCallback[_seqCount] = callback;
$.each(seq, function(idx, s) {
_seq[_seqCount].push(s);
if (s.initial !== undefined) {
var prop = {};
prop[s.property] = s.initial;
$(s.id).css(prop);
}
});
doAnimation(_seqCount, 0);
_seqCount += 1;
}
_api.runSequences = function() {
var i = 0.
args = arguments,
runNext = function() {
if (i+1 < args.length) {
i++;
if (typeof args[i] === "function") {
args[i]();
runNext();
} else {
_api.runSequence(args[i], function() {
runNext();
});
}
}
};
// first we need to set the initial values of all sequences that specify them
$.each(arguments, function(idx, seq) {
if (typeof seq !== "function") {
$.each(seq, function(idx2, seq2) {
if (seq2.initial !== undefined) {
var prop = {};
prop[seq2.property] = seq2.initial;
$(seq2.id).css(prop);
}
});
}
});
_api.runSequence(arguments[i], function(){
runNext();
});
}
return _api;
}(jQuery));
我认为你可以从setTimeout的UI线程逸出,使我可以肯定地使用非阻塞动画中的setTimeout功能有连续动画一个理智的看代码。 – 2009-10-20 20:11:29
你错了。 'setTimeout'不在不同的线程上执行回调;它等待UI线程变为空闲,然后调用UI线程上的回调。因此,Javascript开发人员不需要处理所有线程安全开发的复杂问题。 – SLaks 2009-10-20 20:38:26
测试我的答案解决方案,请 – CuSS 2010-08-25 16:19:06