2011-08-05 83 views

回答

3

您应该在点击按钮后立即禁用按钮,然后执行动画。

你说你想让它只能点击一次,但是你要等到动画完成才能禁用按钮。这就是为什么程序没有按照你所希望的那样工作,因为在动画过程中,按钮可以再次被点击。

这里有一个如何确保按钮只能点击一次:

$("#next").click(function(){ 

    var pos = parseInt($("#box").css("left")), 
     newPos = pos + 100; 

    // disable button immediately after the click 
    // if you wait to disable, you give the user the chance to click twice 
    $("#next").unbind('click'); 
    $("#next").attr("disabled", "disabled"); 

    $("#box").animate({ 
     "left" : newPos //move the box to the right by 100px 
    }, { 
     duration: 150 
    });   
}) 

Working example

unbind()可能没有必要在很多浏览器,但它可以确保click事件处理程序实际上从#next中删除。

+1

感谢@Peter我忘了,用户仍然可以通过点击,而在进度动画。 – fortuneRice

1

固定它来看看这个

http://jsfiddle.net/zs2uf/5/

代码

$("#next").click(function(){ 


    var pos = parseInt($("#box").css("left")); 
    var newPos = pos + 100; 

    if($("#box:animated").length == 0){ 

     $("#box").animate({ 
      "left" : newPos //move the box to the right by 100px 
     }, { 
      duration: 150, 
      complete: function(){ 

      //after moving the box by 100px, disable the next button, 
      //so that the box can move no further than 100px 
      $("#next").attr("disabled", true); 

      } 
     }); 
    } 





    //problem: 
    //if you click the Next button twice really fast, 
    //the box moves twice before the button is disabled 

}) 
+0

谢谢@ShankarSangoli我以前不知道:动画选择器。 – fortuneRice

相关问题