Sunburst effect with css3 gradient

2019-02-13 16:19发布

问题:

I have been looking around and trying for a few days, but i just cant seem to get it 100% right... i am trying to achieve the following effect with css3 gradient:

the closest i have gotten is 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%;
}

I will continue attempting it.. any help is greatly Appreciated though.

Update:

There has to be a better/reusable way of doing this... looking into a scss solution, here is what i have thus far:

.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),
  }
}

Now its just the actual maths behind it i am trying to figure out... attempting to steal logic from pow.js, but kind of difficult if your as terrible at maths as i am...

回答1:

You could use :before and :after :pseudo-elements to get this effect.

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>



回答2:

your background-postition is set to center -100%;. Just use

background-position: center center;

Now with this change and your provided code half of your image is already done ;). Just add the second half with more linear-gradients



回答3:

@chipChocolate.py gave a brilliant solution! This is an improvement based on his.

  1. In Firefox transparent behaves like rgba(0,0,0,0) which leaves a thin gray line at the edge. Change to rgba(255,255,255,0) looks better.

  2. Make the visual effect closer to OP's screenshot: 36 strips, each occupies a 10 degree angle.

  3. Effective on <html> tag, like OP's try.

BTW: Chrome's render engine sucks, best viewed in Firefox.

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);
}