我可以为div添加额外的变换吗?

时间:2015-01-10 16:21:08

标签: javascript jquery html css transform

那么,如果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>

1 个答案:

答案 0 :(得分:1)

您可以做的是依靠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和/或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>

相关问题