2011-03-07 38 views
2

我试图做一个3阶段的动画,其中打开框最小化,他们都向左滑动,并单击框扩展。最后的动画,选中的方框展开,当它被称为第二个动画的参数时重复3次,但如果我单独调用它,它会正常工作。jQuery animate()重复3x

其在http://st-catherineschool.org/xbox/

$("#box2").click(
    function(){ 
     box = 2; 
     if(boxopen==box)return; 
     $("#box"+boxopen).animate({height:"-=30%", width:"-=10%", top:"+=20%", fontSize:"14px"}, 500, null, 
     function(){ 
     $("section").animate({right:"+=25%"},500,null, 
     function(){ 
      $("#box"+box).animate({height:"+=30%", width:"+=10%", top:"-=20%"}, 500); 
      $("#box"+boxopen).css("z-index", "8"); 
      $("#box"+box).css("z-level", "9"); 
      boxopen = box; 
     }); 
     }); 
    }); 

回答

0

我认为你正在寻找jQuery#stop(),它停在匹配的元素任何当前正在运行的动画。尝试使用.animate()之前调用.stop(),像这样:

// instead of: 
$('section').animate(…); 
// try this: 
$('section').stop().animate(…); 
+0

这不是停止动画的问题,它是DOM Bubbling的一个问题! – RobertPitt

+0

添加.stop()没有改变,我不认为它的冒泡,因为答案1也没有帮助。 – austin

+1

我的不好,你是对的,你只是把它与你的例子中的错误选择器。当我把它放在(“#box”+ box)中时,AKA#box2在最后一个函数中,它的工作原理就是我想要的。 – austin

1

我不知道你的HTML的样子,但它可能是你的点击冒泡到其他元素,造成这种现象主持。为了防止这种情况,使用stopPropagation()

EX:

$("#box2").click(
    function(e){ 
     e.stopPropagation(); 
     box = 2; 
     ..... 
+0

完整的网页是在http://st-catherineschool.org/xbox/但#BOX2是 – austin

+0

e.stopPropagation空

()改变不了什么在第一个函数中,但是如果我将它放在其他函数中,那些函数中的动画就不会发生 – austin