How to create a sphere in css?

前端 未结 5 2124
臣服心动
臣服心动 2020-12-29 12:27

I have trying to create a 3D sphere using just pure css, but I\'ve been unable to generate the shape required. I\'ve seen a cylinder but I can\'t find any reference to creat

5条回答
  •  再見小時候
    2020-12-29 13:25

    Sphere like shape

    There is no actual 3D shapes in html5
    But you can stack 2D shapes on top of each other.
    With this in mind you can create pretty a close representation of a sphere.

    .container {
      perspective: 1000px;
      //transform-style: preserve-3d;
      width: 300px;
      height: 300px;
      border: 5px solid pink;
    }
    .circ {
      transform-style: preserve-3d;
      border: 5px solid firebrick;
      border-radius: 50%;
      position: absolute;
      top: 50%;
      left: 50%;
      transform-origin: center;
      transform: translateX(-50%) translateY(-50%);
      transition: transform 2s linear;
    }
    .circ:nth-of-type(1) {
      height: 10%;
      width: 10%;
      transform: translateX(-50%) translateY(-50%) rotateX(40deg) rotateY(40deg) translateZ(-55px);
    }
    .circ:nth-of-type(2) {
      height: 20%;
      width: 20%;
      transform: translatex(-50%) translateY(-50%) rotateX(40deg) rotateY(40deg) translateZ(-45px);
    }
    .circ:nth-of-type(3) {
      height: 30%;
      width: 30%;
      transform: translatex(-50%) translateY(-50%) rotateX(40deg) rotateY(40deg) translateZ(-25px);
    }
    .circ:nth-of-type(4) {
      height: 40%;
      width: 40%;
      transform: translatex(-50%) translateY(-50%) rotateX(40deg) rotateY(40deg) translateZ(-10px);
    }
    .circ:nth-of-type(5) {
      height: 40%;
      width: 40%;
      transform: translatex(-50%) translateY(-50%) rotateX(40deg) rotateY(40deg) translateZ(10px);
    }
    .circ:nth-of-type(6) {
      height: 30%;
      width: 30%;
      transform: translatex(-50%) translateY(-50%) rotateX(40deg) rotateY(40deg) translateZ(25px);
    }
    .circ:nth-of-type(7) {
      height: 20%;
      width: 20%;
      transform: translatex(-50%) translateY(-50%) rotateX(40deg) rotateY(40deg) translateZ(45px);
    }
    .circ:nth-of-type(8) {
      height: 10%;
      width: 10%;
      transform: translatex(calc(-50%)) translateY(-50%) rotateX(40deg) rotateY(40deg) translateZ(55px);
    }
    /*ANIMATION*/
    
    .container:hover .circ:nth-of-type(1) {
      transform: translateX(-50%) translateY(-50%) rotateX(180deg) rotateY(40deg) translateZ(-60px);
    }
    .container:hover .circ:nth-of-type(2) {
      transform: translatex(-50%) translateY(-50%) rotateX(180deg) rotateY(40deg) translateZ(-40px);
    }
    .container:hover .circ:nth-of-type(3) {
      transform: translatex(-50%) translateY(-50%) rotateX(180deg) rotateY(40deg) translateZ(-20px);
    }
    .container:hover .circ:nth-of-type(4) {
      transform: translatex(-50%) translateY(-50%) rotateX(180deg) rotateY(40deg) translateZ(0px);
    }
    .container:hover .circ:nth-of-type(5) {
      transform: translatex(-50%) translateY(-50%) rotateX(180deg) rotateY(40deg) translateZ(20px);
    }
    .container:hover .circ:nth-of-type(6) {
      transform: translatex(-50%) translateY(-50%) rotateX(180deg) rotateY(40deg) translateZ(40px);
    }
    .container:hover .circ:nth-of-type(7) {
      transform: translatex(-50%) translateY(-50%) rotateX(180deg) rotateY(40deg) translateZ(60px);
    }
    .container:hover .circ:nth-of-type(8) {
      transform: translatex(-50%) translateY(-50%) rotateX(180deg) rotateY(40deg) translateZ(70px);
    }
    .container:hover {
      background-color: #f2f2f2;
    }

提交回复
热议问题