【问题标题】:Can I add extra transform to a div?我可以向 div 添加额外的转换吗?
【发布时间】:2015-03-08 19:46:53
【问题描述】:

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

【问题讨论】:

  • 不使用相同的idid 只能在页面上使用一次
  • 只有一个元素和一个id
  • 是的。如果我不需要 jquery 解决方案,我不会在那里写那个标签
  • 啊,我以为你的意思是你可以将脚本重复用于其他 div
  • 对不起我的英语:/

标签: javascript jquery html css transform


【解决方案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>

【讨论】:

  • HA 用于“硬件加速”。触发 3D 变换,即使值为 0,也会导致浏览器渲染引擎将绘画卸载到 GPU。
  • 我可以参加完整的课程吗?
  • “全班”是什么意思?
猜你喜欢
  • 2010-11-12
  • 1970-01-01
  • 2014-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-27
  • 1970-01-01
相关资源
最近更新 更多