formular to calculate width/height (relative to parent) of container with translateZ inside of parent container with perspective

前端 未结 3 656
借酒劲吻你
借酒劲吻你 2020-12-16 16:00

What is the formular to calculate the widths/heights of child elements with translateZ inside of parent container with set perspective (keyword: &

相关标签:
3条回答
  • 2020-12-16 16:23

    You've already solved your problem. Your code does exactly what you need it to do, it's just a CSS layout issue now.

    https://codepen.io/anon/pen/xLWGzp?editors=0100

    Because of the perspective changes, if you hang everything off the x-axis center everything will begin to line up properly:

    (I'm just adding in the code changes here, I've left everything else the same)

    #projection
        perspective-origin: center top
    
    .pro
        transform-origin: center top
    

    Now everything's lining up better, but it's still a bit off - you can change the $width variable to anything other than 100% to see the problem (60% is a good one)

    So the problem now is just due to the positioning of the elements, when you set position: absolute they're default positioned to the left, change the width and add scale and transform and you get this equal-width/not-equal-position, so center them by adding:

    #projection
        position: relative
    
    .pro
        left: 50%
        margin-left: $width * -.5
    

    (info here as to why that works to center: https://css-tricks.com/quick-css-trick-how-to-center-an-object-exactly-in-the-center/)

    So now jiggle $width around to double-check, I tested it from 20% up to 150% and it works fine.

    0 讨论(0)
  • 2020-12-16 16:31

    I have changed the style slightly, to make things more visible.

    The result seems ok for me. May be I am misunderstanding something?

    html, body {
      height: 100%;
      overflow: hidden;
      width: 100%;
    }
    
    #projection {
      perspective: 1px;
      perspective-origin: 0 0;
      height: 50%;
      overflow: visible;
      width: 50%;
      margin-left: 50px;
      background-color: grey;
    }
    
    .pro {
      transform: scale(1) translate(0px, 0px) translateZ(0px);
      height: 50%;
      position: absolute;
      transform-origin: 0 0;
      transform-style: preserve-3d;
      width: 100%;
    }
    
    .pro--1 {
      transform: scale(4) translate(0px, 0px) translateZ(-3px);
      width: 110%;
    }
    
    .pro--2 {
      transform: scale(3) translate(0px, 120%) translateZ(-2px);
      width: 110%;
    }
    
    .pro--3 {
      transform: scale(2) translate(0px, 240%) translateZ(-1px);
      width: 110%;
    }
    
    .pro--1 {
      background: rgba(0, 0, 75, 0.5);
      color: blue;
      line-height: 1em;
      text-align: center;
    }
    
    .pro--2 {
      background: rgba(0, 75, 0, 0.5);
      color: green;
      line-height: 4em;
    }
    
    .pro--3 {
      background: rgba(75, 0, 0, 0.5);
      color: red;
      line-height: 7em;
    }
    <div id="projection">
      <div class="pro pro--1">pro--1</div>
      <div class="pro pro--2">pro--2</div>
      <div class="pro pro--3">pro--3</div>
    </div>

    0 讨论(0)
  • 2020-12-16 16:42

    The formular to calculate the widths/heights of child elements with translateZ inside of parent container with set perspective is:
    $scale = 1 + (translateZ * -1) / perspective.


    Where to throw this formular at transform: scale( $scale ).
    perspective is parents perspective value and translateZ is elements translateZ value as well. See snippet at the bottom or SASS mockup as you like.


    So actually the answer was given in the question!
    While this is the correct answer to the asked question this didn't solve my specific problem at all so here is a follow-up-question.


    Credits: @Trolleymusic who at least opened my eyes somehow with his answer.


    CSS example:

    /* formular: 1 + (translateZ * -1) / perspective */
    
    #parent {
      perspective: 10px;
    }
    
    .child--1 { /* 1 + (-100 * -1) / 10 */
      transform: translateZ(-100px) scale( 11 );
    }
    
    .child--2 { /* 1 + (-200 * -1) / 10 */
      transform: translateZ(-200px) scale( 21 );
    }
    
    .child--3 { /* 1 + (1 * -1) / 10 */
      transform: translateZ(1px) scale( 0.9 );
    }
    
    0 讨论(0)
提交回复
热议问题