How to create a sphere in css?

前端 未结 5 2111
臣服心动
臣服心动 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:01

    You might want to use 3D rotated circles:

    This uses rotated circles to look like a spherical grid. the lesser no. of elements, the better performance.

    Some elements have been rotated in X axis, and others in Y axis. I have filled different colours to show this:

    #cont {
      perspective: 10000px;
      transform-style: preserve-3d;
      -webkit-animation: rotat 1s linear infinite;
      animation: rotat 10s linear infinite;
      transform-origin: 50% 50% 50%;
    }
    .circ {
      height: 200px;
      width: 200px;
      border: 2px solid black;
      border-radius: 50%;
      position: absolute;
      top: 50px;
      left: 50%;
      margin-left: -100px;
      transform-origin: 50%;
      transform-style: preserve-3d;
      background: orange;
    }
    .circ:nth-child(1) {
      transform: rotateX(0deg);
    }
    .circ:nth-child(2) {
      transform: rotateX(30deg);
    }
    .circ:nth-child(3) {
      transform: rotateX(60deg);
    }
    .circ:nth-child(4) {
      transform: rotateX(90deg);
    }
    .circ:nth-child(5) {
      transform: rotateX(120deg);
    }
    .circ:nth-child(6) {
      transform: rotateX(150deg);
    }
    .circ:nth-child(7) {
      transform: rotateX(180deg);
    }
    /*other side rotated*/
    
    .circ:nth-child(8) {
      transform: rotateY(30deg);
    }
    .circ:nth-child(9) {
      transform: rotateY(60deg);
    }
    .circ:nth-child(10) {
      transform: rotateY(90deg);
    }
    .circ:nth-child(11) {
      transform: rotateY(120deg);
    }
    .circ:nth-child(12) {
      transform: rotateY(150deg);
    }
    .circ:nth-child(13) {
      transform: rotateY(180deg);
    }
    .oth {
      background: crimson;
    }
    @-webkit-keyframes rotat {
      0% {
        -webkit-transform: rotateY(0deg) translateX(0);
      }
      100% {
        -webkit-transform: rotateY(360deg);
      }
    }
    @keyframes rotat {
      0% {
        transform: rotateY(0deg);
      }
      100% {
        transform: rotateY(360deg);
      }
    }

    You can also rotate some elements in Z direction, but that will make it even more buggy. Now if you fill the same colours in circles, it almost looks like a sphere:

    #cont {
      perspective: 10000px;
      transform-style: preserve-3d;
      -webkit-animation: rotat 1s linear infinite;
      animation: rotat 10s linear infinite;
      transform-origin: 50% 50% 50%;
    }
    .circ {
      height: 200px;
      width: 200px;
      border: 2px solid black;
      border-radius: 50%;
      position: absolute;
      top: 50px;
      left: 50%;
      margin-left: -100px;
      transform-origin: 50%;
      transform-style: preserve-3d;
      background: crimson;
    }
    .circ:nth-child(1) {
      transform: rotateX(0deg);
    }
    .circ:nth-child(2) {
      transform: rotateX(30deg);
    }
    .circ:nth-child(3) {
      transform: rotateX(60deg);
    }
    .circ:nth-child(4) {
      transform: rotateX(90deg);
    }
    .circ:nth-child(5) {
      transform: rotateX(120deg);
    }
    .circ:nth-child(6) {
      transform: rotateX(150deg);
    }
    .circ:nth-child(7) {
      transform: rotateX(180deg);
    }
    /*other side rotated*/
    
    .circ:nth-child(8) {
      transform: rotateY(30deg);
    }
    .circ:nth-child(9) {
      transform: rotateY(60deg);
    }
    .circ:nth-child(10) {
      transform: rotateY(90deg);
    }
    .circ:nth-child(11) {
      transform: rotateY(120deg);
    }
    .circ:nth-child(12) {
      transform: rotateY(150deg);
    }
    .circ:nth-child(13) {
      transform: rotateY(180deg);
    }
    .o {
      border: none;
    }
    @-webkit-keyframes rotat {
      0% {
        -webkit-transform: rotateY(0deg);
      }
      100% {
        -webkit-transform: rotateY(360deg);
      }
    }
    @keyframes rotat {
      0% {
        transform: rotateY(0deg);
      }
      100% {
        transform: rotateY(360deg);
      }
    }

提交回复
热议问题