纯css星空动画

好久没有发css相关的demo了,今天整了个新活,
我们平常开发那种星星效果,如下gif,都是使用js开发,但是今天的文章既然是放在纯css专栏里,那自然是用js写的
请添加图片描述
原理其实也不复杂,每个元素都有阴影,阴影能设置多个,css能控制这些阴影移动,
那原理有了实践如下

使用的是v3, 和scss

<script setup>
</script>

<template>
  <div class="box">
    <div v-for="i in 5" :key="i" :class="'layer' + i"></div>
  </div>
</template>

<style scoped lang="scss">
@use "sass:math";
$size: 20px;
$duration: 800s;
$count: 1300;
.box {
  height: 100vh;
  width: 100vw;
  background: black;
}

.title {
  background-clip: text;
  color: transparent;
}
@function getShadows($n) {
  $shadows: "#{random(100)}vw #{random(100)}vh #fff";
  @for $i from 2 through $n {
    $shadows: "#{$shadows}, #{random(100)}vw #{random(100)}vh #fff";
  }
  @return unquote($shadows);
}
@for $i from 1 through 5 {
  $duration: math.div($duration, 2);
  $count: floor(math.div($count, 2));
  .layer#{$i} {
    $size: #{$i}px;
    position: fixed;
    width: $size;
    height: $size;
    border-radius: 50%;
    left: 0;
    top: 0;
    box-shadow: getShadows($count);
    animation: moveUp $duration linear infinite;
    &::after {
      content: "";
      position: fixed;
      left: 0;
      top: 100vh;
      border-radius: inherit;
      width: inherit;
      height: inherit;
      box-shadow: inherit;
    }
  }
}
@keyframes moveUp {
  to {
    transform: translateY(-100vh);
  }
}
</style> 

  • 7
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Qayrup

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值