Please wait...

小波Note

四川 · 成都市14 ℃
English

Use sass and box-shadow to achieve starry sky background

成都 (cheng du)9/23/2024, 1:20:31 AM1.26kEstimated reading time 4 minFavoriteCtrl + D / ⌘ + D
cover
IT FB(up 主)
Back-end development engineer
html
        <div>
    <div class="astral_1" />
    <div class="astral_2" />
    <div class="astral_3" />
    <div class="astral_4" />
    <div class="astral_5" />
</div>

    
scss
        <style scoped lang="scss">

@use "sass:math";
@function getShadows($n) {
    $result: '#{random(100)}vw #{random(100)}vh 0 #fff';
    @for $i from 2 through floor($n) {
        $result: '#{$result}, #{random(100)}vw #{random(100)}vh 0 #fff';
    }
    @return unquote($result);
}

$duration: 600s;
$num: 1000;
@for $i from 1 through 5 {
    $duration: math.div($duration, 2);
    $num: math.div($num, 2);
    .astral_#{$i} {
        z-index: -1;
        position: fixed;
        top: 0;
        left: 0;
        width: #{$i}px;
        height: #{$i}px;
        border-radius: 50%;
        box-shadow: getShadows($num);
        animation: moveUp $duration linear infinite;
        &::after {
            content: '';
            position: absolute;
            top: 100vh;
            left: 0;
            width: inherit;
            height: inherit;
            border-radius: inherit;
            box-shadow: inherit;
        }
    }
}

@keyframes moveUp {
    to {
        transform: translateY(-100vh);
    }
}

</style>