CSS: carousel & precedence of translate & rotate

 

 

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    body {
      perspective: 500px;
    }

    section {
      position: relative;
      width: 200px;
      height: 100px;
      margin: 200px auto;
      transform-style: preserve-3d;
      transform: rotateX(-25deg);
    }

    section div {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      background: url(/images/pig.jpg) no-repeat;
    }

    /* 先translate, 后rotate */
    /* div:nth-child(1) {
      transform: translateZ(100px);
    }

    div:nth-child(2) {
      transform: translateX(100px) rotateY(90deg);
    }

    div:nth-child(3) {
      transform: translateZ(-100px) rotateY(180deg);
    }

    div:nth-child(4) {
      transform: translateX(-100px) rotateY(270deg);
    } */

    /* 先rotate, 后translate */
    div:nth-child(1) {
      transform: translateZ(100px);
    }

    div:nth-child(2) {
      transform: rotateY(90deg) translateZ(100px);
    }

    div:nth-child(3) {
      transform: rotateY(180deg) translateZ(100px);
    }

    div:nth-child(4) {
      transform: rotateY(270deg) translateZ(100px);
    }
  </style>
</head>

<body>
  <section>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
  </section>
</body>

</html>

 

Carousel:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    body {
      /* perspective 小到一定程度,视角会进入carousel内部 */
      perspective: 800px;
    }

    section {
      position: relative;
      width: 200px;
      height: 300px;
      margin: 200px auto;
      font-size: 50px;
      line-height: 300px;
      text-align: center;
      color: aliceblue;
      transform-style: preserve-3d;
      /* transform: rotateX(-25deg); */
      animation-name: carousel;
      animation-duration: 3s;
      animation-timing-function: linear;
      animation-iteration-count: infinite;
    }

    section:hover {
      animation-play-state: paused;
    }

    section div {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      background: url(/images/pig.jpg) no-repeat;
    }

    @keyframes carousel {
      0% {
        transform: rotateY(0);
      }

      100% {
        transform: rotateY(360deg);
      }
    }

    div:nth-child(1) {
      transform: translateZ(300px);
    }

    div:nth-child(2) {
      transform: rotateY(45deg) translateZ(300px);
    }

    div:nth-child(3) {
      transform: rotateY(90deg) translateZ(300px);
    }

    div:nth-child(4) {
      transform: rotateY(135deg) translateZ(300px);
    }

    div:nth-child(5) {
      transform: rotateY(180deg) translateZ(300px);
    }

    div:nth-child(6) {
      transform: rotateY(225deg) translateZ(300px);
    }

    div:nth-child(7) {
      transform: rotateY(270deg) translateZ(300px);
    }

    div:nth-child(8) {
      transform: rotateY(315deg) translateZ(300px);
    }
  </style>
</head>

<body>
  <section>
    <div>0001</div>
    <div>0002</div>
    <div>0003</div>
    <div>0004</div>
    <div>0005</div>
    <div>0006</div>
    <div>0007</div>
    <div>0008</div>
  </section>
</body>

</html>

 

posted @ 2022-02-08 15:49  ascertain  阅读(58)  评论(0编辑  收藏  举报