当前位置: 首页 > news >正文

前端实现一个星空特效的效果(实战+讲解)

目录

第一章 效果

第二章 实现步骤

第三章 源代码


第一章 效果

第二章 实现步骤

  • 首先创建好页面;利用box-shodow的特性我们创建多个星星点
<script setup></script><template><div class="main"><div class="layer_1"></div><div class="layer_2"></div><div class="layer_3"></div><div class="layer_4"></div><div class="layer_5"></div><div class="title">星空特效</div></div>
</template><style scoped lang="scss">
.main {width: 100vw;height: 100vh;display: flex;justify-content: center;align-items: center;background: #000;.title {font-size: 40px;font-family: "楷体";text-shadow: 0px 3px 0px rgba(97, 106, 203, 0.1);background: linear-gradient(0deg, #c9daf7 0%, #ffffff 100%);color: transparent;-webkit-background-clip: text;}.layer_1 {position: fixed;top: 0;left: 0;width: 20px;height: 20px;border-radius: 50%;background-color: #fff;// 我们暂时先用盒子阴影表示星星。注意:盒子阴影是可以有多个的box-shadow: 10vw 0 #fff, 20vw 10vh #fff;}
}
</style>

  • 由于我们需要很多很多星星,所以我们需要利用sass的特性,写函数实现,而不是一个一个输入
@function star($n) {// 创建第一个阴影$result: "#{random(100)}vw #{random(100)}vh #fff";// for循环,渲染n个阴影@for $i from 2 through $n {$result: "#{$result}, #{random(100)}vw #{random(100)}vh #fff";}@return unquote($result);
}

  • 添加动画
@keyframes moveUp {to {transform: translateY(-100vh);}
}

  • 解决动画移动到最后留白的问题
// 一个很实用的技巧,在后面再加一个一模一样页面
.layer_1::after {content: "";// inherit: 当前元素继承父元素position: inherit;width: inherit;height: inherit;border-radius: inherit;background-color: inherit;box-shadow: inherit;left: 0;top: 100vh;
}
  • 动态渲染5个模板,由近即远的效果,越后面速度越慢,数量越多,用函数写
  $n: 5;$duration: 600s;$count: 1000;@for $i from 1 through $n {$duration: floor($duration / 2);$count: floor($count / 2);.layer_#{$i} {position: fixed;top: 0;left: 0;width: #{$i}px;height: #{$i}px;border-radius: 50%;background-color: #fff;// 我们暂时先用盒子阴影表示星星。注意:盒子阴影是可以有多个的box-shadow: star($count);animation: moveUp $duration linear infinite;}// 一个很实用的技巧,在后面再加一个一摸一样页面.layer_#{$i}::after {content: "";// inherit: 当前元素继承父元素position: inherit;width: inherit;height: inherit;border-radius: inherit;background-color: inherit;box-shadow: inherit;left: 0;top: 100vh;}}

第三章 源代码

  • sass中文文档

https://sass.bootcss.com/documentation.html

  • 完整源代码:(复制即可使用)
<script setup></script><template><div class="main"><div class="layer_1"></div><div class="layer_2"></div><div class="layer_3"></div><div class="layer_4"></div><div class="layer_5"></div><div class="title">星空特效</div></div>
</template><style scoped lang="scss">
@function star($n) {// 创建第一个阴影$result: "#{random(100)}vw #{random(100)}vh #fff";// for循环,渲染n个阴影@for $i from 2 through $n {$result: "#{$result}, #{random(100)}vw #{random(100)}vh #fff";}@return unquote($result);
}
.main {width: 100vw;height: 100vh;display: flex;justify-content: center;align-items: center;background: #000;.title {font-size: 40px;font-family: "楷体";text-shadow: 0px 3px 0px rgba(97, 106, 203, 0.1);background: linear-gradient(0deg, #c9daf7 0%, #ffffff 100%);color: transparent;-webkit-background-clip: text;z-index: 1000;}$n: 5;$duration: 600s;$count: 1000;@for $i from 1 through $n {$duration: floor($duration / 2);$count: floor($count / 2);.layer_#{$i} {position: fixed;top: 0;left: 0;width: #{$i}px;height: #{$i}px;border-radius: 50%;background-color: #fff;// 我们暂时先用盒子阴影表示星星。注意:盒子阴影是可以有多个的box-shadow: star($count);animation: moveUp $duration linear infinite;}// 一个很实用的技巧,在后面再加一个一摸一样页面.layer_#{$i}::after {content: "";// inherit: 当前元素继承父元素position: inherit;width: inherit;height: inherit;border-radius: inherit;background-color: inherit;box-shadow: inherit;left: 0;top: 100vh;}}
}@keyframes moveUp {to {transform: translateY(-100vh);}
}
</style>

http://www.dtcms.com/a/392931.html

相关文章:

  • 【嵌入式】【科普】软件模块设计简介
  • 【ROS2】ROS2通讯机制Topic常用命令行
  • 欧姆龙NJ系列PLC编程标准化案例
  • 【OpenGL】LearnOpenGL学习笔记25 - 法线贴图 NormalMap
  • UE5 基础应用 —— 09 - 行为树 简单使用
  • 客户端实现信道管理
  • 异常解决记录 | Yarn NodeManager 注册异常
  • 【C#】C# 调用 Python 脚本正确姿势:解决 WaitForExit 死锁与退出检测问题
  • Java25新特性
  • 卷积神经网络CNN-part9-DenseNet
  • 深入浅出密码学第一章课后题(持续更新)
  • Mysql 入门概览
  • 大模型中权重共享的作用?
  • 【精品资料鉴赏】55页可编辑PPT详解 数字化高校智慧后勤解决方案
  • LLM大模型 - 实战篇 - AI Agents的开发应用
  • 【分布式技术】RedisShake相关功能详细介绍
  • qsv:一款高性能的CSV数据处理工具
  • `html` 将视频作为背景
  • 口播提词器怎么选?手机提词器实测指南与参数推荐
  • 解剖线性表
  • 计算数学研究方向有哪些细分领域?
  • [xboard]08-Makefile逐行分析2
  • Clash 中 REJECT 的技术原理与解决方案 —— 以哔哩哔哩延迟问题为例
  • 庖丁解牛与专家思维:道家的“心手合一”训练法
  • matlab通过GUI实现点云的读取、自定义显示和保存
  • 工业现场实战:如何利用智能网关实现西门子PLC与库卡机器人的无缝连接
  • 【开题答辩全过程】以 Java程序设计课程作业数据分析为例,包含答辩的问题和答案
  • ubuntu配置cuda与torch
  • C语言:输出水仙花数
  • 进程的创建