Rotating CSS cube on fixed axes

后端 未结 2 1558
孤城傲影
孤城傲影 2020-12-18 09:02

I have a cube constructed using CSS. It\'s made of 6 faces and each face is transformed to form one face of the cube, and all the 6 faces are under one

2条回答
  •  眼角桃花
    2020-12-18 09:33

    use rotate3d

    It's relatively easy to use, but you would still need to link up your current tracking script to the right parameters

    You can control the rotation amount (in terms of degrees) and which axis is affected (x,y,z). You can select one more at the same time.

    Example 1 - rotate X axis:

    #cube-wrapper {
      position: absolute;
      left: 50%;
      top: 50%;
      perspective: 1500px;
    }
    
    .cube {
      position: relative;
      transform-style: preserve-3d;
      animation-name: rotate;
      animation-duration: 30s;
      animation-timing-function: linear;
      animation-iteration-count: infinite;
    }
    
    @keyframes rotate {
      0% {
        transform: rotate3d(0, 0, 0, 0);
      }
      100% {
        transform: rotate3d(1, 0, 0, 360deg); /*controls rotation amount on one axis) */
        ;
      }
    }
    
    
    /* Size and border color for each face */
    
    .face {
      position: absolute;
      width: 200px;
      height: 200px;
      border: solid green 3px;
    }
    
    
    /* Transforming every face into their correct positions */
    
    #front_face {
      transform: translateX(-100px) translateY(-100px) translateZ(100px);
      background: rgba(255, 0, 0, 0.5);
    }
    
    #back_face {
      transform: translateX(-100px) translateY(-100px) translateZ(-100px);
      background: rgba(255, 0, 255, 0.5);
    }
    
    #right_face {
      transform: translateY(-100px) rotateY(90deg);
      background: rgba(255, 255, 0, 0.5);
    }
    
    #left_face {
      transform: translateY(-100px) translateX(-200px) rotateY(90deg);
      background: rgba(0, 255, 0, 0.5);
    }
    
    #top_face {
      transform: translateX(-100px) translateY(-200px) rotateX(90deg);
      background: rgba(0, 255, 255, 0.5);
    }
    
    #bottom_face {
      transform: translateX(-100px) rotateX(90deg);
      background: rgba(255, 255, 255, 0.5);
    }
    
    .cube {
      transform: rotateX(90deg) rotateY(90deg);
    }
    
    
    
      3D Cube in PureScript
      
      
    
    
    
      
      

    Example 2 - rotate Y axis:

    #cube-wrapper {
      position: absolute;
      left: 50%;
      top: 50%;
      perspective: 1500px;
    }
    
    .cube {
      position: relative;
      transform-style: preserve-3d;
      animation-name: rotate;
      animation-duration: 30s;
      animation-timing-function: linear;
      animation-iteration-count: infinite;
    }
    
    @keyframes rotate {
      0% {
        transform: rotate3d(0, 0, 0, 0);
      }
      100% {
        transform: rotate3d(0, 1, 0, 360deg); /*controls rotation amount on one axis) */
        ;
      }
    }
    
    
    /* Size and border color for each face */
    
    .face {
      position: absolute;
      width: 200px;
      height: 200px;
      border: solid green 3px;
    }
    
    
    /* Transforming every face into their correct positions */
    
    #front_face {
      transform: translateX(-100px) translateY(-100px) translateZ(100px);
      background: rgba(255, 0, 0, 0.5);
    }
    
    #back_face {
      transform: translateX(-100px) translateY(-100px) translateZ(-100px);
      background: rgba(255, 0, 255, 0.5);
    }
    
    #right_face {
      transform: translateY(-100px) rotateY(90deg);
      background: rgba(255, 255, 0, 0.5);
    }
    
    #left_face {
      transform: translateY(-100px) translateX(-200px) rotateY(90deg);
      background: rgba(0, 255, 0, 0.5);
    }
    
    #top_face {
      transform: translateX(-100px) translateY(-200px) rotateX(90deg);
      background: rgba(0, 255, 255, 0.5);
    }
    
    #bottom_face {
      transform: translateX(-100px) rotateX(90deg);
      background: rgba(255, 255, 255, 0.5);
    }
    
    .cube {
      transform: rotateX(90deg) rotateY(90deg);
    }
    
    
    
      3D Cube in PureScript
      
      
    
    
    
      
      

    Example 3 - rotate Z axis:

    #cube-wrapper {
      position: absolute;
      left: 50%;
      top: 50%;
      perspective: 1500px;
    }
    
    .cube {
      position: relative;
      transform-style: preserve-3d;
      animation-name: rotate;
      animation-duration: 30s;
      animation-timing-function: linear;
      animation-iteration-count: infinite;
    }
    
    @keyframes rotate {
      0% {
        transform: rotate3d(0, 0, 0, 0);
      }
      100% {
        transform: rotate3d(0, 0, 1, 360deg); /*controls rotation amount on one axis) */
        ;
      }
    }
    
    
    /* Size and border color for each face */
    
    .face {
      position: absolute;
      width: 200px;
      height: 200px;
      border: solid green 3px;
    }
    
    
    /* Transforming every face into their correct positions */
    
    #front_face {
      transform: translateX(-100px) translateY(-100px) translateZ(100px);
      background: rgba(255, 0, 0, 0.5);
    }
    
    #back_face {
      transform: translateX(-100px) translateY(-100px) translateZ(-100px);
      background: rgba(255, 0, 255, 0.5);
    }
    
    #right_face {
      transform: translateY(-100px) rotateY(90deg);
      background: rgba(255, 255, 0, 0.5);
    }
    
    #left_face {
      transform: translateY(-100px) translateX(-200px) rotateY(90deg);
      background: rgba(0, 255, 0, 0.5);
    }
    
    #top_face {
      transform: translateX(-100px) translateY(-200px) rotateX(90deg);
      background: rgba(0, 255, 255, 0.5);
    }
    
    #bottom_face {
      transform: translateX(-100px) rotateX(90deg);
      background: rgba(255, 255, 255, 0.5);
    }
    
    .cube {
      transform: rotateX(90deg) rotateY(90deg);
    }
    
    
    
      3D Cube in PureScript
      
      
    
    
    
      
      

    Example 4 - rotate X,Y, and Z at the same time:

    #cube-wrapper {
      position: absolute;
      left: 50%;
      top: 50%;
      perspective: 1500px;
    }
    
    .cube {
      position: relative;
      transform-style: preserve-3d;
      animation-name: rotate;
      animation-duration: 30s;
      animation-timing-function: linear;
      animation-iteration-count: infinite;
    }
    
    @keyframes rotate {
      0% {
        transform: rotate3d(0, 0, 0, 0);
      }
      100% {
        transform: rotate3d(1, 1, 1, 360deg); /*controls rotation amount on one axis) */
        ;
      }
    }
    
    
    /* Size and border color for each face */
    
    .face {
      position: absolute;
      width: 200px;
      height: 200px;
      border: solid green 3px;
    }
    
    
    /* Transforming every face into their correct positions */
    
    #front_face {
      transform: translateX(-100px) translateY(-100px) translateZ(100px);
      background: rgba(255, 0, 0, 0.5);
    }
    
    #back_face {
      transform: translateX(-100px) translateY(-100px) translateZ(-100px);
      background: rgba(255, 0, 255, 0.5);
    }
    
    #right_face {
      transform: translateY(-100px) rotateY(90deg);
      background: rgba(255, 255, 0, 0.5);
    }
    
    #left_face {
      transform: translateY(-100px) translateX(-200px) rotateY(90deg);
      background: rgba(0, 255, 0, 0.5);
    }
    
    #top_face {
      transform: translateX(-100px) translateY(-200px) rotateX(90deg);
      background: rgba(0, 255, 255, 0.5);
    }
    
    #bottom_face {
      transform: translateX(-100px) rotateX(90deg);
      background: rgba(255, 255, 255, 0.5);
    }
    
    .cube {
      transform: rotateX(90deg) rotateY(90deg);
    }
    
    
    
      3D Cube in PureScript
      
      
    
    
    
      
      

提交回复
热议问题