2
我以递归方式动画3分。我想在点击#myElement后停止递归。Jquery:停止递归与事件
这里是我的代码:
$(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;
}
动画作品,但也许需要以另一种方式来实现停止递归。
一个想法?