森伯斯特效应与css3梯度

时间:2014-12-17 13:37:15

标签: css css3 sass gradient

我一直在环顾四周,尝试了几天,但我似乎无法100%正确...我试图用css3渐变实现以下效果:

sunburst done in illustrator

我最接近的是 DEMO

html {
  background:
    linear-gradient(80deg, transparent 50%, #ddd 50%, #ddd),
    linear-gradient(90deg, transparent 50%, #ddd 50%, #ddd),
    linear-gradient(82deg, transparent 50%, #eee 50%, #eee),
    linear-gradient(67deg, transparent 50%, #ddd 50%, #ddd),
    linear-gradient(52deg, transparent 50%, #eee 50%, #eee),
    linear-gradient(37deg, transparent 50%, #ddd 50%, #ddd),
    linear-gradient(22deg, transparent 50%, #eee 50%, #eee),
    linear-gradient(7deg, transparent 50%, #ddd 50%, #ddd),
    linear-gradient(-8deg, transparent 50%, #eee 50%, #eee),
    linear-gradient(-23deg, transparent 50%, #ddd 50%, #ddd),
    linear-gradient(-38deg, transparent 50%, #eee 50%, #eee),
    linear-gradient(-53deg, transparent 50%, #ddd 50%, #ddd),
    linear-gradient(-68deg, transparent 50%, #eee 50%, #eee),
    linear-gradient(-83deg, transparent 50%, #ddd 50%, #ddd);
  background-position: center -100%;
  background-color: #eee;
  background-size: 100% 200%;
  min-height: 100%;
}

我会继续尝试..但是,任何帮助都会得到很大的赞赏。

更新

必须有一个更好/可重用的方法来做这个......看看scss解决方案,这是我到目前为止:

.sunburst {
  @for $ray from 1 through 26 {
    $color: #eee;
    $degree: 7;
    @if $ray%2 == 0 {
      $color: #ddd;
    }
    background:linear-gradient($degree+deg, transparent 50%, $color 50%, $color),
  }
}

现在它只是它背后的实际数学我试图弄清楚...试图从pow.js窃取逻辑,但如果你在数学方面和我一样糟糕的话有点困难......

5 个答案:

答案 0 :(得分:5)

您可以使用:before:after:伪元素来获得此效果。



html, body {
  width: 100%;
  height: 100%;
  margin: 0;
}
#grad {
  position: relative;
  width: 100%;
  height: 100%;
}
#grad:after, #grad:before {
  content: '';
  position: absolute;
  background: linear-gradient(90deg, transparent 50%, black 50%, black), linear-gradient(82deg, transparent 50%, #12E0DB 50%, #12E0DB), linear-gradient(67deg, transparent 50%, #000000 50%, #000000), linear-gradient(52deg, transparent 50%, #12E0DB 50%, #12E0DB), linear-gradient(37deg, transparent 50%, #000000 50%, #000000), linear-gradient(22deg, transparent 50%, #12E0DB 50%, #12E0DB), linear-gradient(7deg, transparent 50%, #000000 50%, #000000), linear-gradient(-8deg, transparent 50%, #12E0DB 50%, #12E0DB), linear-gradient(-23deg, transparent 50%, #000000 50%, #000000), linear-gradient(-38deg, transparent 50%, #12E0DB 50%, #12E0DB), linear-gradient(-53deg, transparent 50%, #000000 50%, #000000), linear-gradient(-68deg, transparent 50%, #12E0DB 50%, #12E0DB), linear-gradient(-83deg, transparent 50%, #000000 50%, #000000), linear-gradient(-90deg, transparent 50%, #12E0DB 50%, #12E0DB);
  background-position: 0% 0%;
  background-size: 200% 100%;
  height: 100%;
  width: 50%;
}
#grad:before {
  left: 50%;
  transform: rotate(180deg);
}

<div id="grad"></div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

您的background-postition设置为center -100%;。只需使用

background-position: center center;

现在进行此更改并且您提供的代码已经完成了一半的图像;)。只需添加更多linear-gradients

的后半部分

答案 2 :(得分:1)

@ chipChocolate.py 提供了出色的解决方案!这是基于他的改进。

  1. 在Firefox transparent中表现得像rgba(0,0,0,0),在边缘留下一条细灰线。更改为rgba(255,255,255,0)看起来更好。

  2. 让视觉效果更接近OP的屏幕截图:36个条带,每个占据10度角。

  3. <html>代码有效,就像OP尝试一样。

  4. BTW:Chrome的渲染引擎很糟糕,在Firefox中浏览效果最佳。

    &#13;
    &#13;
    html {
      height: 100%;
      position: relative;
    }
    
    html:before, html:after {
      content: '';
      height: 100%;
      width: 50%;
      position: absolute;
      top: 0;
      background-size: 200% 100%;
      background-image: linear-gradient(85deg, rgba(255,255,255,0) 50%, #12e0db 50%, #12e0db),
                        linear-gradient(75deg, rgba(255,255,255,0) 50%, #000 50%, #000),
                        linear-gradient(65deg, rgba(255,255,255,0) 50%, #12e0db 50%, #12e0db),
                        linear-gradient(55deg, rgba(255,255,255,0) 50%, #000 50%, #000),
                        linear-gradient(45deg, rgba(255,255,255,0) 50%, #12e0db 50%, #12e0db),
                        linear-gradient(35deg, rgba(255,255,255,0) 50%, #000 50%, #000),
                        linear-gradient(25deg, rgba(255,255,255,0) 50%, #12e0db 50%, #12e0db),
                        linear-gradient(15deg, rgba(255,255,255,0) 50%, #000 50%, #000),
                        linear-gradient(5deg, rgba(255,255,255,0) 50%, #12e0db 50%, #12e0db),
                        linear-gradient(-5deg, rgba(255,255,255,0) 50%, #000 50%, #000),
                        linear-gradient(-15deg, rgba(255,255,255,0) 50%, #12e0db 50%, #12e0db),
                        linear-gradient(-25deg, rgba(255,255,255,0) 50%, #000 50%, #000),
                        linear-gradient(-35deg, rgba(255,255,255,0) 50%, #12e0db 50%, #12e0db),
                        linear-gradient(-45deg, rgba(255,255,255,0) 50%, #000 50%, #000),
                        linear-gradient(-55deg, rgba(255,255,255,0) 50%, #12e0db 50%, #12e0db),
                        linear-gradient(-65deg, rgba(255,255,255,0) 50%, #000 50%, #000),
                        linear-gradient(-75deg, rgba(255,255,255,0) 50%, #12e0db 50%, #12e0db),
                        linear-gradient(-85deg, rgba(255,255,255,0) 50%, #000 50%, #000),
                        linear-gradient(-95deg, rgba(255,255,255,0) 50%, #12e0db 50%, #12e0db);
    }
    
    html:before {
      left: 50%;
      transform: rotate(180deg);
    }
    &#13;
    &#13;
    &#13;

答案 3 :(得分:0)

在现代的基于chrome的浏览器中,可以使用圆锥形渐变来实现此目的。

       div {
       height:250px;
          background-image:
             repeating-conic-gradient(#fff 0 9deg, #000 9deg 18deg);
        }
<div></div>

答案 4 :(得分:0)

当前,有一个repeating-conic-gradient,它会创建一个包含重复渐变的图像。

div {
  height: 500px;
  background: repeating-conic-gradient(
    hsl(186deg 100% 50% / 31%) 0deg 15deg,
    hsla(0,0%,100%,0) 0deg 30deg
  ) #1c2c3c
}
<div></div>

您可以在W3 CSS Image Values上详细了解它。 此属性与所有浏览器都不兼容。检查caniuse 了解更多信息。