简体   繁体   English

用javascript旋转CSS3 3d立方体

[英]Rotating a CSS3 3d cube with javascript

I am trying to rotate a 3d cube with two controls - for left and right. 我试图用两个控件旋转一个三维立方体 - 左和右。 When you click on the left button, for example, I'd like to add extra 40 degrees to the rotateY value on the left. 例如,当您单击左键时,我想在左侧的rotateY值上添加额外的40度。

My question is: Since I can't work with exact values (I want everytime to add new values to the existing ones!), I guess I will have to split up the string for the transform into something like this: 我的问题是:由于我无法使用精确值(我希望每次都能为现有值添加新值!),我想我必须将变换的字符串拆分成这样的:

$(cube).css("-webkit-transform", "rotateX(-100deg) rotateZ(-50deg + add 40 degrees to the existing one!) translateZ(-30px)");

Here is the pen I am working on: 这是我正在研究的笔:

http://codepen.io/gbnikolov/pen/hHEBG http://codepen.io/gbnikolov/pen/hHEBG

The easiest way to keep track of the rotation is to do it yourself, as jQuery's css() will return the matrix, and then you'll have to calculate that etc. and it's just easier to keep track of the value in an attribute instead. 跟踪旋转的最简单方法是自己动手,因为jQuery的css()将返回矩阵,然后你必须计算等等,而且更容易跟踪属性中的值。

Keep the value in a data attribute, and increment that together with the css value, and it should be straight forward, and it's accessible from other functions as well. 将值保存在数据属性中,并将其与css值一起递增,并且应该是直接的,并且也可以从其他函数访问它。

cube.data('rot', -50);

left.click(function(){
  var deg = cube.data('rot') + 40;

  cube.css("-webkit-transform", "rotateX(-100deg) rotateZ("+deg+"deg) translateZ(-30px)");

  cube.data('rot', deg);
});

right.click(function(){
  var deg = cube.data('rot') - 40;

  cube.css("-webkit-transform", "rotateX(-100deg) rotateZ("+deg+"deg) translateZ(-30px)");

  cube.data('rot', deg);
});

CodePen CodePen

To keep track of the rotation and increment it on each click with JS, this is what you can do : 要跟踪旋转并在每次单击时使用JS增加它,您可以执行以下操作:

jQuery : jQuery:

var btn = $('.nav'),
    body = $('body'),
    cube = $(".cube"),
    left = $('.left'),
    v_transZ = -40 ;

// REST OF YOUR jQuery CODE

left.click(function(){
  v_transZ = v_transZ - 10;
  var counter = 0,
      v_trans = 'rotateX(-100deg) rotateZ('+ v_transZ +'deg) translateZ(-30px)';

  $(cube).css("-webkit-transform", v_trans);

});

As you are using CSS3 transitions, I would tend to use them also to rotate the cube and use JS only to trigger the animation with class changes on click. 当您使用CSS3过渡时,我倾向于使用它们来旋转立方体并仅使用JS来触发动画,并在单击时更改类。 But as you ask for a JS solution... 但是当你要求JS解决方案时......

Well, since you are changing the angle on only one axis this is not exactly what you are looking for, but it might give you a clue: Css Tricks 好吧,因为你只在一个轴上改变角度,这不是你想要的,但它可能会给你一个线索: Css技巧

I would try to solve the problem completely in the meantime, though! 不过,我会尝试在此期间完全解决问题!

I'm not sure whether your question is about how to keep do incremental changes (instead of hardcoded ones) or how to find a more elegant way for setting those values. 我不确定您的问题是关于如何继续进行增量更改(而不是硬编码更改)或如何找到更优雅的方式来设置这些值。

For the first one, I think the only way is to keep track of rotation values in your JavaScript code: http://codepen.io/anon/pen/dsIgn (I worked on it in Firefox, just change back -moz- to -webkit- .) 对于第一个,我认为唯一的方法是跟踪JavaScript代码中的旋转值: http-moz- (我在Firefox中工作过,只需将-moz-更改为-webkit- 。)

You have to declare a variable outside the click function and store the current value there 您必须在click函数之外声明一个变量并在那里存储当前值

Then in every click you add 40 to that variable 然后在每次单击时,将40添加到该变量

JS JS

var currentRotateZ = -50

left.click(function(){
    $(cube).css("-webkit-transform", "rotateX(-100deg) rotateZ(" + (currentRotateZ += 40) + "deg) translateZ(-30px)");
});

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

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