css3 translate in percent

我与影子孤独终老i 提交于 2019-12-18 13:19:09

问题


I am stuck on defining an css3 cube completely with percent.

Here a short example in Codepen

http://codepen.io/anon/pen/detAB

As you can see the cube faces have 100% width and height of its parent element, which works perfect. Now i am trying to translate the bottom face 50% down and 50% back.

with pixel values this is no problem

transform: rotateX(-90deg) translateZ(50px) translateY(50px);

but with percent nothing happens

transform: rotateX(-90deg) translateZ(50%) translateY(50%);

is there any other way? or am I missing something?


回答1:


The percentage there is not of the parent container in the way you might expect but of the element itself. The spec describes it as:

[The percentage] refer[s] to the size of the element's box

Regarding %s, the spec says:

Note that values are not allowed in the translateZ translation-value, and if present will cause the propery value to be invalid.

Though, it seems that instead, they aren't valid in any of them for Chrome at least.

Sorry :(




回答2:


The best I've found is by doing a bit of javascript.

Instead of using the translateZ() value, I've used the transform-origin: x y z for the axis to be at the center of the cube.

The point is that the cube can turn on its center (and not turn on a center of the main face and translate z...)

Here is the jQuery function (eventually to apply on $(document).ready() and $(window).resize()) :

function get50() {
  var half_width = $('#front').width() / 2;
  $('#front').css({
    transformOrigin : '50% 50% -' + half_width + 'px'
  });
}

You can see a DEMO here...



来源:https://stackoverflow.com/questions/13103828/css3-translate-in-percent

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!