如何通过以圆形方式堆叠div来创建棒棒糖形状?

时间:2019-03-14 09:19:25

标签: css css3 sass css-transforms css-shapes

如何以循环方式堆叠div,使得最后一个div应该位于第一个div之下但位于第二个div之上。 CSS可能吗?任何帮助将不胜感激。

enter image description here

请找到Codepen。 提供示例代码段

<div class="frame">
  <div class="lolly-pop__wrapper">
    <div class="lollypop-top">
      <div class="lollypop-top__item"></div>
      <div class="lollypop-top__item"></div>
      <div class="lollypop-top__item"></div>
      <div class="lollypop-top__item"></div>
      <div class="lollypop-top__item"></div>
      <div class="lollypop-top__item"></div>
      <div class="lollypop-top__item"></div>
      <div class="lollypop-top__item"></div>
      <div class="lollypop-top__item"></div>
      <div class="lollypop-top__item"></div>
      <div class="lollypop-top__item"></div>
      <div class="lollypop-top__item"></div>
    </div>
  </div>
</div>


.frame {
  position: absolute;
  display: flex;
  justify-content: center;
  top: 50%;
  left: 50%;
  width: 400px;
  height: 400px;
  margin-top: -200px;
  margin-left: -200px;
  border-radius: 2px;
  box-shadow: 4px 8px 16px 0 rgba(0,0,0,0.1);
  overflow: hidden;
  background: #F5CE51;
  color: #333;
  font-family: 'Open Sans', Helvetica, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

.lollypop-top {
    position: relative;
    height: 150px;
    width: 150px;
    background-color: #fff;
    border-radius: 100%;
    overflow: hidden;

    .lollypop-top__item {
        position: absolute;
        height: 150px;
        width: 150px;
        top: -50%;
        border-radius: 100%;
        transform-origin: bottom;
        background-color: #fff;

        &:nth-child(odd) {
            background-color: #D70606;
        }

        &:nth-child(1) {
            transform: rotate(30deg);
        }

        &:nth-child(2) {
            transform: rotate(60deg);
        }

        &:nth-child(3) {
            transform: rotate(90deg);
        }

        &:nth-child(4) {
            transform: rotate(120deg);
        }

        &:nth-child(5) {
            transform: rotate(150deg);
        }

        &:nth-child(6) {
            transform: rotate(180deg);
        }

        &:nth-child(7) {
            transform: rotate(210deg);
        }

        &:nth-child(8) {
            transform: rotate(240deg);
        }

        &:nth-child(9) {
            transform: rotate(270deg);
        }

        &:nth-child(10) {
            transform: rotate(300deg);
        }

        &:nth-child(11) {
            transform: rotate(330deg);
        }

        &:nth-child(12) {
            transform: rotate(360deg);
        }
      }
    }

.frame {
  position: absolute;
  display: flex;
  justify-content: center;
  top: 50%;
  left: 50%;
  width: 400px;
  height: 400px;
  margin-top: -200px;
  margin-left: -200px;
  border-radius: 2px;
  box-shadow: 4px 8px 16px 0 rgba(0, 0, 0, 0.1);
  overflow: hidden;
  background: #F5CE51;
  color: #333;
  font-family: 'Open Sans', Helvetica, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

.lollypop-top {
  position: relative;
  height: 150px;
  width: 150px;
  background-color: #fff;
  border-radius: 100%;
  overflow: hidden;
}

.lollypop-top .lollypop-top__item {
  position: absolute;
  height: 150px;
  width: 150px;
  top: -50%;
  border-radius: 100%;
  transform-origin: bottom;
  background-color: #fff;
}

.lollypop-top .lollypop-top__item:nth-child(odd) {
  background-color: #D70606;
}

.lollypop-top .lollypop-top__item:nth-child(1) {
  transform: rotate(30deg);
}

.lollypop-top .lollypop-top__item:nth-child(2) {
  transform: rotate(60deg);
}

.lollypop-top .lollypop-top__item:nth-child(3) {
  transform: rotate(90deg);
}

.lollypop-top .lollypop-top__item:nth-child(4) {
  transform: rotate(120deg);
}

.lollypop-top .lollypop-top__item:nth-child(5) {
  transform: rotate(150deg);
}

.lollypop-top .lollypop-top__item:nth-child(6) {
  transform: rotate(180deg);
}

.lollypop-top .lollypop-top__item:nth-child(7) {
  transform: rotate(210deg);
}

.lollypop-top .lollypop-top__item:nth-child(8) {
  transform: rotate(240deg);
}

.lollypop-top .lollypop-top__item:nth-child(9) {
  transform: rotate(270deg);
}

.lollypop-top .lollypop-top__item:nth-child(10) {
  transform: rotate(300deg);
}

.lollypop-top .lollypop-top__item:nth-child(11) {
  transform: rotate(330deg);
}

.lollypop-top .lollypop-top__item:nth-child(12) {
  transform: rotate(360deg);
}
<div class="frame">
  <div class="lolly-pop__wrapper">
    <div class="lollypop-top">
      <div class="lollypop-top__item"></div>
      <div class="lollypop-top__item"></div>
      <div class="lollypop-top__item"></div>
      <div class="lollypop-top__item"></div>
      <div class="lollypop-top__item"></div>
      <div class="lollypop-top__item"></div>
      <div class="lollypop-top__item"></div>
      <div class="lollypop-top__item"></div>
      <div class="lollypop-top__item"></div>
      <div class="lollypop-top__item"></div>
      <div class="lollypop-top__item"></div>
      <div class="lollypop-top__item"></div>
    </div>
  </div>
</div>

3 个答案:

答案 0 :(得分:7)

我将考虑两个元素(伪元素)和多个径向渐变来创建此模型:

.box {
  width:150px;
  height:150px;
  border-radius:100%;
  border:1px solid;
  position:relative;
  overflow:hidden;
}
.box::before,
.box::after{
  content:"";
  position:absolute;
  top:0;
  bottom:0;
  left:0;
  right:50%;
  background:
    /*we rotate by 30deg so will use :
       sin(30deg)*R = 0.5x75px   = 37.5px 
       cos(30deg)*R = 0.866x75px = 64.95px       
       10.05px = 75px - 64.95px;
       112.5px = 75px + 37.5px
       139.95px = 75px + 64.95px
       37.5px = 75px - 37.5px
       */
    radial-gradient(circle 75px at 139.95px 37.5px,red 98%, transparent 100%),
    radial-gradient(circle 75px at 112.5px 10.05px,white 98%,transparent 100%),
    radial-gradient(circle 75px at 75px    0,      red   98%,transparent 100%),
    radial-gradient(circle 75px at 37.5px  10.05px,white 98%,transparent 100%),
    radial-gradient(circle 75px at 10.05px 37.5px ,red   98%,transparent 100%),
    radial-gradient(circle 75px at 0       75px,   white 98%,transparent 100%),
    radial-gradient(circle 75px at 10.05px 112.5px,red   98%,transparent 100%);
}

.box::after {
 transform:rotate(180deg);
 transform-origin:right center;
}
<div class="box">

</div>

为了使事情更有趣,我们可以添加CSS变量:

.box {
  --R:50px; /*Radius*/
  --c1:red; /*first color*/
  --c2:#fff; /*second color*/
  
  --g1:var(--c1) 98%, transparent 100%;
  --g2:var(--c2) 98%, transparent 100%;
  width:calc(2*var(--R));
  height:calc(2*var(--R));
  border-radius:100%;
  border:1px solid;
  position:relative;
  overflow:hidden;
  display:inline-block;
  vertical-align:middle;
}
.box::before,
.box::after{
  content:"";
  position:absolute;
  top:0;
  bottom:0;
  left:0;
  right:50%;
  background:
     /*we rotate by 30deg so will use :
       sin(30deg)*R = 0.5xR   
       cos(30deg)*R = 0.866xR 
     */
    radial-gradient(circle var(--R) at calc(var(--R) + 0.866*var(--R)) calc(var(--R) - 0.5*var(--R))  ,var(--g1)),
    radial-gradient(circle var(--R) at calc(var(--R) + 0.5*var(--R))   calc(var(--R) - 0.866*var(--R)),var(--g2)),
    radial-gradient(circle var(--R) at var(--R)                        0                              ,var(--g1)),
    radial-gradient(circle var(--R) at calc(var(--R) - 0.5*var(--R))   calc(var(--R) - 0.866*var(--R)),var(--g2)),
    radial-gradient(circle var(--R) at calc(var(--R) - 0.866*var(--R)) calc(var(--R) - 0.5*var(--R))  ,var(--g1)),
    radial-gradient(circle var(--R) at 0                               var(--R)                        ,var(--g2)),
    radial-gradient(circle var(--R) at calc(var(--R) - 0.866*var(--R)) calc(var(--R) + 0.5*var(--R))  ,var(--g1));
}

/*the same shape rotated*/
.box::after {
 transform:rotate(180deg);
 transform-origin:right center;
}
<div class="box"></div>

<div class="box" style="--R:80px;--c1:blue"></div>

<div class="box" style="--R:100px;--c1:green;--c2:yellow"></div>

<div class="box" style="--R:150px;--c1:white;--c2:pink"></div>


请注意,Safari不支持at的语法(此处为How to make radial gradients work in Safari?的解释),因此这里是另一种语法:

.box {
  --R:50px; /*Radius*/
  --c1:red; /*first color*/
  --c2:#fff; /*second color*/
  
  --g1:var(--c1) 98%, transparent 100%;
  --g2:var(--c2) 98%, transparent 100%;
  width:calc(2*var(--R));
  height:calc(2*var(--R));
  border-radius:100%;
  border:1px solid;
  position:relative;
  overflow:hidden;
  display:inline-block;
  vertical-align:middle;
}
.box::before,
.box::after{
  content:"";
  position:absolute;
  top:0;
  bottom:0;
  left:0;
  right:50%;
  background:
     /*we rotate by 30deg so will use :
       sin(30deg)*R = 0.5xR   
       cos(30deg)*R = 0.866xR 
     */
    radial-gradient(farthest-side,var(--g1)) calc(var(--R) + 0.866*var(--R) - var(--R)) calc(var(--R) - 0.5*var(--R) - var(--R)),
     
    radial-gradient(farthest-side,var(--g1)) calc(var(--R) + 0.866*var(--R) - var(--R)) calc(var(--R) - 0.5*var(--R) - var(--R)),
    radial-gradient(farthest-side,var(--g2)) calc(var(--R) + 0.5*var(--R) - var(--R))   calc(var(--R) - 0.866*var(--R) - var(--R)),
    radial-gradient(farthest-side,var(--g1)) 0 calc(-1*var(--R)),
    radial-gradient(farthest-side,var(--g2)) calc(var(--R) - 0.5*var(--R) - var(--R))   calc(var(--R) - 0.866*var(--R) - var(--R)),
    radial-gradient(farthest-side,var(--g1)) calc(var(--R) - 0.866*var(--R) - var(--R)) calc(var(--R) - 0.5*var(--R) - var(--R)),
    radial-gradient(farthest-side,var(--g2)) calc(-1*var(--R))  0,
    radial-gradient(farthest-side,var(--g1)) calc(var(--R) - 0.866*var(--R) - var(--R)) calc(var(--R) + 0.5*var(--R) - var(--R));
   background-size:calc(2*var(--R)) calc(2*var(--R));
   background-repeat:no-repeat;
}

/*the same shape rotated*/
.box::after {
 transform:rotate(180deg);
 transform-origin:right center;
}
<div class="box"></div>

<div class="box" style="--R:80px;--c1:blue"></div>

<div class="box" style="--R:100px;--c1:green;--c2:yellow"></div>

<div class="box" style="--R:150px;--c1:white;--c2:pink"></div>

答案 1 :(得分:5)

我的方法是使用可重用的SVG <symbol>,其路径由quadratic-bézier曲线形成:

#svg-lollipop path { transform-origin: 50% 50%; }

#svg-lollipop path:nth-child(2) {  transform: rotateZ(60deg); }
#svg-lollipop path:nth-child(3) {  transform: rotateZ(120deg); }
#svg-lollipop path:nth-child(4) {  transform: rotateZ(180deg); }
#svg-lollipop path:nth-child(5) {  transform: rotateZ(240deg); }
#svg-lollipop path:nth-child(6) {  transform: rotateZ(300deg); }

.lollipop {
  width: 50%;
  overflow: hidden;
  border-radius: 50%;
  position: relative;
  margin: 20px;
}

.lollipop::before {
  content: "";
  padding-bottom: 100%;
  display: block;
}

.lollipop svg {
  position: absolute;  
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  fill: currentColor;
}
<svg style="display: none;">
  <symbol id="svg-lollipop">
<path d="M150,150 Q75,185 0,150 H0 V63.6 Q75,150 150,150 z" />
<path d="M150,150 Q75,185 0,150 H0 V63.6 Q75,150 150,150 z" />
<path d="M150,150 Q75,185 0,150 H0 V63.6 Q75,150 150,150 z" />
<path d="M150,150 Q75,185 0,150 H0 V63.6 Q75,150 150,150 z" />
<path d="M150,150 Q75,185 0,150 H0 V63.6 Q75,150 150,150 z" />
<path d="M150,150 Q75,185 0,150 H0 V63.6 Q75,150 150,150 z" />
  </symbol>
</svg>

<div class="lollipop" style="background-color: #FFF; color:#E92120;">
  <svg viewBox="0 0 300 300"><use xlink:href="#svg-lollipop"></use></svg>
</div>

<div class="lollipop" style="background-color: #004991; color:#007BC1;">
  <svg viewBox="0 0 300 300"><use xlink:href="#svg-lollipop"></use></svg>
</div>


工作方式

将相同形状克隆6次并旋转以填充整个svg。在此示例中,每个彩色形状都有一个角度α = 30deg

然后从三角学中我们可以找到曲线的原点的坐标:在路径中, y坐标 63.6的取值为150 - (150 * tan(α)),因此如果需要更改形状和角度的数量,您可以轻松找到自己的原点(quadratic curves确实很容易绘制)。

最后,外包装具有一个border-radius和一个隐藏的overflow,以提供圆形形状。

最终结果也很敏感,因为外部包装器保持其1:1的宽高比。

可以通过在容器上设置background-color来更改白色区域,而可以使用color属性({{1}的fill属性}元素为方便起见设置为svg

  

codepen demo


我以后发现的东西

如果在包装纸上添加currentColor,则看起来更像是充气的沙滩气球,而不是棒棒糖。

机缘巧合

答案 2 :(得分:2)

也请查看此答案。Codepen

<div class="frame">
<div class="lolly-pop__wrapper">
<div class="lollypop-top">
    <div class="first__half">
        <div class="lollypop-top__item"></div>
        <div class="lollypop-top__item"></div>
        <div class="lollypop-top__item"></div>
        <div class="lollypop-top__item"></div>
        <div class="lollypop-top__item"></div>
        <div class="lollypop-top__item"></div>
        <div class="lollypop-top__item"></div>
        <div class="lollypop-top__item"></div>
        <div class="lollypop-top__item"></div>
        <div class="lollypop-top__item"></div>
        <div class="lollypop-top__item"></div>
        <div class="lollypop-top__item"></div>
        <div class="lollypop-top__item"></div>
        <div class="lollypop-top__item"></div>
    </div>
        <div class="second__half">
        <div class="lollypop-top__item"></div>
        <div class="lollypop-top__item"></div>
        <div class="lollypop-top__item"></div>
        <div class="lollypop-top__item"></div>
        <div class="lollypop-top__item"></div>
        <div class="lollypop-top__item"></div>
        <div class="lollypop-top__item"></div>
        <div class="lollypop-top__item"></div>
        <div class="lollypop-top__item"></div>
        <div class="lollypop-top__item"></div>
        <div class="lollypop-top__item"></div>
        <div class="lollypop-top__item"></div>
        <div class="lollypop-top__item"></div>
        <div class="lollypop-top__item"></div>
    </div>
</div>
</div>
</div>


.frame {
  position: absolute;
  display: flex;
  justify-content: center;
  top: 50%;
  left: 50%;
  width: 400px;
  height: 400px;
  margin-top: -200px;
  margin-left: -200px;
  border-radius: 2px;
  box-shadow: 4px 8px 16px 0 rgba(0,0,0,0.1);
  overflow: hidden;
  background: #F5CE51;
  color: #333;
  font-family: 'Open Sans', Helvetica, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

.lollypop-top {
    position: relative;
    height: 150px;
    width: 150px;
    background-color: #ccc;
    border-radius: 100%;
    overflow: hidden;

.lollypop-top__item {
    position: absolute;
    height: 150px;
    width: 150px;
    top: -50%;
    border-radius: 100%;
    transform-origin: bottom;
    background-color: #fff;

    &:nth-child(odd) {
        background-color: #D70606;
    }

    &:nth-child(1) {
        transform: rotate(30deg);
    }

    &:nth-child(2) {
        transform: rotate(60deg);
    }

    &:nth-child(3) {
        transform: rotate(90deg);
    }

    &:nth-child(4) {
        transform: rotate(120deg);
    }

    &:nth-child(5) {
        transform: rotate(150deg);
    }

    &:nth-child(6) {
        transform: rotate(180deg);
    }

    &:nth-child(7) {
        transform: rotate(210deg);
    }

    &:nth-child(8) {
        transform: rotate(240deg);
    }

    &:nth-child(9) {
        transform: rotate(270deg);
    }

    &:nth-child(10) {
        transform: rotate(300deg);
    }

    &:nth-child(11) {
        transform: rotate(330deg);
    }

    &:nth-child(12) {
        transform: rotate(360deg);
    }

    &:nth-child(13) {
        transform: rotate(390deg);
    }

    &:nth-child(14) {
        transform: rotate(420deg);
    }
  }
}

.first__half,
.second__half {
    position: absolute;
    left: 0;
    top: 0;
    height: 100%;
    overflow: hidden;
}

.first__half {
    width: 50%;
}

.second__half {
    right: 0;
    width: 50%;
    transform: rotate(180deg);
    transform-origin:right center;
}