我正在递归地为 3 点设置动画。单击#myElement 后,我想停止递归。
这是我的代码:
$(document).ready(function(){
var positions = {
'pos1': {"X": $('#point_1').css('left'), "Y": $('#point_1').css('top')},
'pos2': {"X": $('#point_2').css('left'), "Y": $('#point_2').css('top')},
'pos3': {"X": $('#point_3').css('left'), "Y": $('#point_3').css('top')},
};
animate3Points(positions);
});
function animate3Points(p) {
animatePoints('#point_3', p, p.pos1);
animatePoints('#point_2', p, p.pos2);
animatePoints('#point_1', p, p.pos3);
}
function animatePoints(selector, p, pos) {
$(selector).animate({
'left': pos.X ,
'top': pos.Y
}, 2000, 'easeInOutQuad', function() {
// while #myElement has not been clicked,
// call again animate3points for recursivity :
if (selector == '#point_1') { // just to be recalled one time
p2 = changePosition(p);
animate3Points(p2);
}
// end while
});
}
function changePosition(obj) {
firstEl = obj.pos1;
obj.pos1 = obj.pos2;
obj.pos2 = obj.pos3;
obj.pos3 = firstEl;
return obj;
}
动画有效,但可能需要以另一种方式实现以停止递归。
一个主意 ?