2016-11-06 59 views
1

我想更多的旋转度每有人点击一次添加到#弧度自旋#RAD-BTN添加更多度每次点击

$(document).ready(function(){ 
    $("#rad-btn").click(function(){ 
     $("#rad-spin").css("transform", "rotate(90deg)"); 
    }); 
}); 

现在它只是一个时间到90度,如果我点击再次它不会进一步到180deg(显然)。这就是我想要的。任何人都知道如何做到这一点?

回答

1
  1. 通过词汇范围,你可以做到这一点。通过这个,保持外部值index

    $(document).ready(function(){ 
        var index = 0 
        $("#rad-btn").click(function(){ 
        index++ 
        $("#rad-spin").css("transform", "rotate(" + 90 * index + "deg)"); 
        }); 
    }); 
    
  2. 通过data-$.data,你可以保存在DOM中的信息。

    $(document).ready(function(){ 
        $("#rad-spin").data('index', 0) 
        $("#rad-btn").click(function(){ 
        var index = $("#rad-spin").data('index') 
        $("#rad-spin").data('index', ++index) 
        $("#rad-spin").css("transform", "rotate(" + 90 * index + "deg)"); 
        }); 
    }); 
    
+0

感谢这个工作! :) – user3124133

+0

现在我有另一个问题,但。我想要这个按钮:#rad-btn。当它正在旋转时被禁用。(这是一个旋转的btw)ivoklerk.com/sesamestreet/rad.html# - user3124133刚刚编辑 – user3124133

+0

我无法得到您的目标。用户可能会多次单击'#rad-btn'动画结束? @ user3124133 – TomIsion

1

只是存储的角度地方。

$(document).ready(function(){ 
 
    var angle = 0; 
 
    $("#rad-btn").click(function(){ 
 
     angle+=720; 
 
     $("#rad-spin").css("transform", "rotate("+angle+"deg)"); 
 
     $(this).prop('disabled',true).delay(2000).queue(()=>$(this).prop('disabled',false).dequeue()); 
 
    }); 
 
});
#rad-spin{ 
 
    transition: transform 2s; 
 
    position: absolute; 
 
    top: 150px; 
 
    left: 50px; 
 
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<button id='rad-btn'>v. rad</button> 
 
<div id='rad-spin'>spin me round and round</div>

+0

谢谢我已经用TomIsion的解决方案修复了它。现在我还有另一个问题。我想要这个按钮:#rad-btn。要在它的旋转被禁用。(这是一个纺车BTW) http://ivoklerk.com/sesamestreet/rad.html# – user3124133

+0

各种方式来做到这一点,我添加了一个到我的回答。您可以定义在CSS过渡超时,禁用按钮,然后点击相同的延迟后启用它。 – unholybear

+0

谢谢你这个工作! :) – user3124133