我可以向 div 添加额外的转换吗?

Can I add extra transform to a div?

那么,如果 div 已经有一个变换,我可以添加到现有的旋转吗? 像这样:

<div style="width: 200px; height: 200px; background-color: red; transform: rotateX(90deg) rotateY(10deg)" id="myDiv"></div>
<script>
document.addEventListener("click",function(){
document.getElementById("myDiv").style.transformRotateX += 10deg;
})
</script>

你能做的就是依靠velocity.js,它允许你累积修改单个变换属性。优点是 velocity.js 依赖于 JavaScript 而不是 jQuery 的动画效果,使其更高效并防止布局抖动。

var ele = document.getElementById("myDiv");

// Establish initial transformation onload
Velocity(
  ele,
  {
    rotateX: '90deg',
    rotateY: '10deg'
  },
  {
    duration: 0
  });

// Progressively manipulate rotateX property upon each click
document.addEventListener("click",function(){
  Velocity(
    ele,
    {
      translateZ: 0, // Force HA by animating a 3D property
      rotateX: "+=10deg"
    },
    {
      duration: 0
    }
  );
});
<script src="http://cdnjs.cloudflare.com/ajax/libs/velocity/1.1.0/velocity.min.js"></script>
<div style="width: 200px; height: 200px; background-color: red;" id="myDiv"></div>


当然,如果你想依靠 jQuery and/or Zepto 来简化元素选择,那也是可以的:

$(function() {
  // Establish initial transformation onload
  $('#myDiv').velocity({
    rotateX: '90deg',
    rotateY: '10deg'
  }, {
    duration: 0;
  });
  
  // Progressively manipulate rotateX property upon each click
  $('#myDiv').click(function() {
    $(this).velocity({
      rotateX: '+=10deg'
    }, {
      duration: 0
    });
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/velocity/1.1.0/velocity.min.js"></script>
<div style="width: 200px; height: 200px; background-color: red;" id="myDiv"></div>