3D立方体中的css3变换原点问题

时间:2014-12-18 11:22:50

标签: html css css3 css-animations css-transforms

我创建了css-cube并在:hover上旋转。

但它的旋转是基于立方体的一面!

我想从中心like in this example旋转它。我正在尝试属性,但没有得到理想的结果。

我还尝试在一个立方体内放置一个中间飞机,但在那种情况下悬停不起作用!



.contain {
    width: 300px;
    height: 300px;
    -webkit-perspective: 500px;
    perspective: 500px;
    position: absolute;
}
.main {
    position:relative;
    width:100px;
    height:100px;
    margin:100px 100px;
    background:#07a;
    overflow:visible;
    transition: all linear,transform cubic-bezier(0.4, 0.25, 0.14, 1.5),background cubic-bezier(0.4, 0.25, 0.14, 1.5);
    transition-duration: 700ms;
    -moz-transform-style: preserve-3d;
    -webkit-transform-style: preserve-3d;
    transform-style: preserve-3d;
    transform-origin: center center;
}

.main:hover{
    transform:rotateY(180deg);
}

.top, .right, .left, .bottom,.lid{
    position:absolute;
    width:100px;
    height:100px;
    z-indexd:999;
    transition:  all 1s ease;
}
.top {
    background:crimson;
    top:-100px;
    transform-origin : 50% 100%;
    transform:rotateX(-90deg);
}
.bottom {
    background:crimson;
    bottom:-100px;
    transform-origin :100% 0%;
    transform:rotateX(90deg);
}
.left {
    background:#ccc;
    left:-100px;
    transform-origin :100% 0%;
    transform:rotateY(90deg);
}
.right {
    background:#ccc;
    right:-100px;
    transform-origin : 0% 0%;
    transform:rotateY(-90deg);
}
.lid {
    background:#07a;
    transform: translateZ(170px);
    transform-origin : 0% 0%;
    transform:translateZ(100px);
}

<div class="contain">
            <div class="main">
                <div class="lid"></div>
                <div class="top"></div>
                <div class="right"></div>
                <div class="left"></div>
                <div class="bottom"></div>
            </div>
        </div>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:3)

问题是您需要将变换原点设置在多维数据集的中心,而多维数据集是一个3d元素。你错过了第三维!

所以它应该是

transform-origin: center center 50px;

因为你的立方体是100px

&#13;
&#13;
.contain {
    width: 300px;
    height: 300px;
    -webkit-perspective: 500px;
    perspective: 500px;
    position: absolute;
}
.main {
    position:relative;
    width:100px;
    height:100px;
    margin:100px 100px;
    background:#07a;
    overflow:visible;
    transition: all linear,transform cubic-bezier(0.4, 0.25, 0.14, 1.5),background cubic-bezier(0.4, 0.25, 0.14, 1.5);
    transition-duration: 700ms;
    -moz-transform-style: preserve-3d;
    -webkit-transform-style: preserve-3d;
    transform-style: preserve-3d;
    transform-origin: center center 50px;
}

.main:hover{
    transform:rotateY(180deg);
}

.top, .right, .left, .bottom,.lid{
    position:absolute;
    width:100px;
    height:100px;
    z-indexd:999;
    transition:  all 1s ease;
}
.top {
    background:crimson;
    top:-100px;
    transform-origin : 50% 100%;
    transform:rotateX(-90deg);
}
.bottom {
    background:crimson;
    bottom:-100px;
    transform-origin :100% 0%;
    transform:rotateX(90deg);
}
.left {
    background:#ccc;
    left:-100px;
    transform-origin :100% 0%;
    transform:rotateY(90deg);
}
.right {
    background:#ccc;
    right:-100px;
    transform-origin : 0% 0%;
    transform:rotateY(-90deg);
}
.lid {
    background:#07a;
    transform: translateZ(170px);
    transform-origin : 0% 0%;
    transform:translateZ(100px);
}
&#13;
<div class="contain">
            <div class="main">
                <div class="lid"></div>
                <div class="top"></div>
                <div class="right"></div>
                <div class="left"></div>
                <div class="bottom"></div>
            </div>
        </div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

我添加了一个translateZ来移动旋转轴,它看起来更加集中,但仍然不像Desandro的前身。

我阅读了文档,我认为你应该结帐this!它解释了一些关于orgins和观点......

<强> EDIT1: 整合translateZ而不是变换原点(现在它是完美的!!)

.contain {
    width: 300px;
    height: 300px;
    -webkit-perspective:666px;
    perspective: 666px;
    position: absolute;
}
.main {
    position:relative;
    width:100px;
    height:100px;
    margin:100px 100px;
    background:#07a;
    overflow:visible;
    transition: all 1s ease;
    -moz-transform-style: preserve-3d;
    -webkit-transform-style: preserve-3d;
    transform-style: preserve-3d;
    transform:translateZ(-50px)
   
    
}

.main:hover{
    transform:  translateZ(-50px) rotateY(180deg);
    
    
}

.top, .right, .left, .bottom,.lid,.front{
    position:absolute;
    width:100px;
    height:100px;
    z-index:999;
    transition:  all 1s ease;
}
.front{
    background:yellow;
    transform:rotateY( 0deg ) translateZ( 50px );
}
.left {
    background:red;
    transform:rotateY(90deg) translateZ( 50px );
}
.right {
    background:purple;
    right:-100px;
    //transform-origin : 0% 0%;
    transform:rotateY(-90deg) translateZ( 150px );
}
.lid {
    background:green;
    transform:rotateY(180deg) translateZ( 50px );
   
}
<div class="contain">
            <div class="main">
                <div class="front"></div>
                <div class="lid"></div>
                <div class="right"></div>
                <div class="left"></div>
             </div>
        </div>

BTW CSS-transformations摇滚!!

答案 2 :(得分:0)

我尝试在“.main:hover”中添加“translateZ(-70px)”,我认为它正在旋转居中。

这将使你的立方体旋转时使一些像素向左平移并使其感觉居中。