简体   繁体   English

每次CSS变换都沿相同方向旋转

[英]CSS transform rotate in the same direction each time

I'm wanting to make an element on a web page rotate 360 degrees anticlockwise each time a button is clicked. 我希望每次单击按钮时,使网页上的元素逆时针旋转360度。 I've found some example code showing how to do this but the only issue is that currently it rotates in alternating directions. 我发现一些示例代码显示了如何执行此操作,但是唯一的问题是当前它以交替的方向旋转。

Is there a simple way to ensure that the element rotates in the same direction every time? 有没有简单的方法来确保元素每次都沿相同方向旋转?

Here's the example code: http://codepen.io/impressivewebs/pen/niurC 这是示例代码: http : //codepen.io/impressivewebs/pen/niurC

.

Cheers 干杯

Your example adds and removes the class that applies the transformation, which has the effect of applying the transformation when the class is added, and then undoing it when the class is removed, hence the reversal of the rotation on the second click. 您的示例添加和删除了应用转换的类,这具有在添加类时应用转换的效果,然后在删除类时撤消转换的效果,因此第二次单击时旋转方向相反。

To increment the rotation each time, you could use JavaScript to keep count of the current rotation value and increment it when the button is clicked, I've forked your example on CodePen here: http://codepen.io/anon/pen/aObMYK to illustrate. 要每次增加旋转次数,您可以使用JavaScript来保持当前旋转值的计数,并在单击按钮时增加它。我在这里在CodePen上为您的示例添加了分叉: http ://codepen.io/anon/pen/ aObMYK进行说明。 Note that I've changed the increment to 30 degrees so you can see what's going on more clearly. 请注意,我已将增量更改为30度,以便您可以更清楚地了解正在发生的情况。 If you want to have it as 360 degree rotation each time, just change the value set in counter += 30; 如果您希望每次旋转360度,只需更改counter += 30;设置的值即可counter += 30; to 360. 到360。

The trick is to increase your rotation instead of reserting it. 诀窍是增加旋转而不是重新旋转。 In the following code we will keep increasing the rotation by 360, this will make it rotate in one direction on every click. 在下面的代码中,我们将旋转增加360度,这将使它在每次单击时都向一个方向旋转。

I only changed your javascript function to achieve this: 我只是更改了您的javascript函数来实现此目的:

var angle =0;
$('button').click(function () {
   angle += 360;
  $(".box").css({'transform': 'rotate(' + angle + 'deg)'});

});

Full Code 完整代码

 var angle =0; $('button').click(function () { //$('.box').toggleClass('box-rotate'); angle += 360; $(".box").css({'transform': 'rotate(' + angle + 'deg)'}); }); 
 .box { background: lightblue; width: 100px; height: 100px; margin: 20px auto; transition: transform 1s linear; transform-origin: top left; transform-style: preserve-3D; } .box-rotate { transform: rotate(360deg); } button { display: block; margin: auto; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> <div class="box"></div> <button>click me</button> 

You're using toggleClass in your JS script. 您在JS脚本中使用toggleClass。 Toggle method is used to switch between two values. 切换方法用于在两个值之间切换。 When you use toggleClass on that element you're adding and removing box-rotate class on every button click. 在该元素上使用toggleClass时,您将在每次单击按钮时添加和删除box-rotate类。 To repeat a function on dom, you'll need javascript and in that case your code will look like 要在dom上重复功能,您需要使用javascript,在这种情况下,您的代码将如下所示

var counter = 0;

$('button').click(function () {
    var box = $('.box');
  // box.toggleClass('box-rotate');
    counter += 360;
  $('.box').css('transform', 'rotate(' + counter + 'deg)')
});

Here is the working code . 这是工作代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM