Fullscreen CSS3 "Cube" Transition

I am using the code given here: Animated S / O 2 Face Cube

I want this effect to fill the entire screen of a webpage to allow a full screen transition from one div / iframe to another.

I've tried using percentages:

<style>
#experiment {
    -webkit-perspective: 1000;
}
html,body{
    margin: 0;
    height: 100%;
    width: 100%;
    overflow: hidden;
}
.cube {
    position: relative;
    height: 100%; //<----
    width: 100%; //<----
    -webkit-transition: -webkit-transform .5s linear;
    -webkit-transform-style: preserve-3d;
}
.face {
    position: absolute;
    height: 100%; //<----
    width: 100%; //<----
    color: #fff;
}
.cube .front {
 -webkit-transform: translateZ(50%); //50% should make sense, but doesn't work so I went to Javascript instead
 background-color:gray;
}

.cube .side {
 -webkit-transform: rotateX(-90deg) translateZ(50%);
 background-color:lightgray;
}

.cube:hover{
 -webkit-transform:rotateX(90deg);     
}
</style>
<div id="experiment">
    <div id="cube" class="cube" align="center">
        <div id="front" class="face front">
            front face
        </div>
        <div id="side" class="face side">
            side face
        </div>
    </div>
</div>

      

I also tried setting the dimensions using Javascript:

<script>
var w = window.innerWidth*.7, h = window.innerHeight*.7;
function Id(obj) { return document.getElementById(obj); }

Id('cube').style.width = w+'px';
Id('cube').style.height = h+'px';
Id('front').style.width = w+'px';
Id('front').style.height = h+'px';
Id('side').style.width = w+'px';
Id('side').style.height = h+'px';
Id('front').style.webkitTransform = 'translateZ('+(w/2)+'px)';
Id('side').style.webkitTransform = 'rotateX(-90deg) translateZ('+(h/2)+'px)';
</script>

      

But it -webkit-perspective

"scales" and fits the pixel into the content and pops parts of it off the screen. I couldn't find any margins that would properly offset / center the cube on the page when resizing windows.

How do I ideally focus on this cube on the page? Once I can get it centrally, I should easily fill it up to full screen.

I couldn't find any articles on perspective

that explain how to align it with any window borders.

This is what I'm looking for, in a scalable way: Example

Thank,

+3


source to share


1 answer


Well, the calculus you are asking is quite difficult.

The good news is you don't really need to do this.

Any object placed at 0 in the z direction will not be scaled by perspective. So the trick is to put the front face of the cube at z = 0px.

On the other hand, to make this responsive, we will need to use view units. Like this:

html,body{
    margin: 0;
    height: 100%;
    width: 100%;
}

#experiment {
    perspective: 200vw;
    height: 100%; 
    width: 100vw; 
    border: solid 1px blue;
}

.cube {
    position: relative;
    height: 100%; 
    width: 100vw; 
    transform-style: preserve-3d;
}
.face {
    position: absolute;
    height: 100%; 
    width: 100%; 
    color: #fff;
    transition: transform 4s linear;
}
.cube .front {
    transform: translateZ(-50vw) rotateY(0deg) translateZ(50vw); 
    transform-origin: center center;
    background-color:gray;
}

.cube .side {
    transform: translateZ(-50vw) rotateY(-90deg) translateZ(50vw);
    background-color:lightgray;
}

.cube:hover .front {
    transform: translateZ(-50vw) rotateY(90deg) translateZ(50vw); 
}

.cube:hover .side {
    transform: translateZ(-50vw) rotateY(0deg) translateZ(50vw);
}
      

<div id="experiment">
    <div id="cube" class="cube" align="center">
        <div id="front" class="face front">
            front face
        </div>
        <div id="side" class="face side">
            side face
        </div>
    </div>
</div>
      

Run code


The chain spin idea comes from Lea Verou here



The snippet above uses view units as they are the only responsive approach for z-movements. But vW doesn't work on iOS. More complex solution not using vW is below (using percentage dimension which can only be set in translateX. 2 extra rotations are also required.

html,body{
    margin: 0;
    height: 100%;
    width: 100%;
}

#experiment {
    perspective: 2000px;
    height: 100%; 
    width: 100%; 
  overflow: hidden;
}

.cube {
    position: relative;
    height: 100%; 
    width: 100%; 
    transform-style: preserve-3d;
}
.face {
    position: absolute;
    height: 100%; 
    width: 100%; 
    color: #fff;
    transition: transform 4s linear;
}
.cube .front {

    transform:  rotateY(-90deg) translateX(-50%) rotateY(0deg) translateX(50%) rotateY(90deg);
    transform-origin: center center;
    background-color:gray;
}

.cube .side {
    transform:  rotateY(-90deg) translateX(-50%) rotateY(-90deg) translateX(50%) rotateY(90deg); 
    background-color:lightgray;
}

.cube:hover .front {
    transform:  rotateY(-90deg) translateX(-50%) rotateY(90deg) translateX(50%) rotateY(90deg);
}

.cube:hover .side {
    transform:  rotateY(-90deg) translateX(-50%) rotateY(0deg) translateX(50%) rotateY(90deg); 
}
      

<div id="experiment">
    <div id="cube" class="cube" align="center">
        <div id="front" class="face front">
            front face
        </div>
        <div id="side" class="face side">
            side face
        </div>
    </div>
</div>
      

Run code


Here's a small demo trying to show how the transformation works. just watch it.

.demo {
    height: 20px; 
    width: 400px; 
    background-color: lightblue;
    position: absolute;
    top: 200px;
}

.demo:after {
    content: "";
    position: absolute;
    background-color: red;
    width: 10px;
    height: 10px;
    left: 50%;
    top: 200px;
    border-radius: 50%;
}

#demo {
    -webkit-animation: demo 25s infinite;
    z-index: 2;
}

@-webkit-keyframes demo {
    0% {transform:  rotate(  0deg) translateX(  0%) rotate( 0deg) translateX( 0%) rotate( 0deg); }
   20% {transform:  rotate(  0deg) translateX(  0%) rotate( 0deg) translateX( 0%) rotate(90deg); }
   40% {transform:  rotate(  0deg) translateX(  0%) rotate( 0deg) translateX(50%) rotate(90deg); }
   60% {transform:  rotate(  0deg) translateX(  0%) rotate(30deg) translateX(50%) rotate(90deg); }
   80% {transform:  rotate(  0deg) translateX(-50%) rotate(30deg) translateX(50%) rotate(90deg); }
  100% {transform:  rotate(-90deg) translateX(-50%) rotate(30deg) translateX(50%) rotate(90deg); }
}

#text {
    width: 400px;
}

#text:after {
  content: "";
  background-color: rgba(200, 0, 0, 0.2);
  width: 300px;
  height: 1em;
  position: absolute;
  top: 15px;
  -webkit-animation: text 25s infinite;
}


@-webkit-keyframes text {
   10% {transform:  translateY(  0px)}
   30% {transform:  translateY( 20px)}
   50% {transform:  translateY( 40px)}
   70% {transform:  translateY( 58px)}
   90% {transform:  translateY( 76px)}
  100% {transform:  translateY( 92px)}
}

span {
  margin-left: 400px;
  top: 20px;
  width: 200px;
  border: solid 1px blue;
  position: absolute;
}
      

<div class="demo" id="demo"></div>
<div class="demo" id="demo2"></div>
<div id="text">
<ul id="phases">
<li >ROTATE to get the x axis </li>
<li >MOVE in the X axis (but really in Z)</li>
<li >ROTATE the wanted rotation </li>
<li >MOVE back in the X axis </li>
<li >ROTATE back the x axis trick </li>
<li >DONE </li>
</ul> 
</div>
<span>Do a rotation of 30deg around a point that is at a distance equal to 50% width in the z axis - Showed as a red point</span>
      

Run code


+7


source







All Articles