2013-02-28 107 views
0

我在学习jQuery。我有这样的代码:jQuery变量变化

$(document).ready(function() { 
var status = false; 
$("#red").click(function() { 
    if(status == false) { 
     $("#red").animate({ 
      width: "800px", 
      height: "800px" 
     }, 1000); 
     $("#text").delay(1000).fadeTo(1000, 1.0); 
     status = true; 

    } 
    else if(status == true) { 
     $("#text").fadeTo(1000, 0.0); 
     $("#red").delay(1000).animate({ 
      width: "200px", 
      height: "200px" 
     }, 1000); 
     status = false; 
    } 
}); 

});

现在,我希望点击不堆叠。我想只在一秒钟后改变变量“状态”的值,然后点击不会叠加。我该怎么做?

谢谢!

回答

1

你可以当动画已经完成了全局变量设置的状态,并添加一个额外的参数到,如果: -

var check; 

$(document).ready(function() { 
var status = false; 
$("#red").click(function() { 
if(status == false && check == true) { 
    $("#red").animate({ 
     width: "800px", 
     height: "800px" 
    }, 1000, function() { 
    // Animation complete. 
    check = true; 

    }); 


    $("#text").delay(1000).fadeTo(1000, 1.0); 
    status = true; 

} 
else if(status == true && check == true) { 
    $("#text").fadeTo(1000, 0.0); 
    $("#red").delay(1000).animate({ 
     width: "200px", 
     height: "200px" 
    }, 1000, , function() { 
    // Animation complete. 
    check = true; 

    }); 
    status = false; 
} 

});

+0

不需要全局var然后,我只是在完成动画后设置“status”的值。像魅力一样工作,感谢兄弟! – 2013-02-28 16:19:58

+0

不用担心男人,很高兴能为您服务,也许您可​​以标记为答案? :d – KryptoniteDove 2013-02-28 16:20:48