网站首页 > 技术文章 正文
如果文章对你有收获,还请不要吝啬【点赞??收藏?评论?】三连哦,你的鼓励将是我成长助力之一!谢谢!
(1)方式1:简单版本
【1】先看实现效果
【2】再看完整代码
<!DOCTYPE html>
<html lang="zh-CN">
<head>
	<!-- 
	 CSS部分:
	 body设置为黑色背景,并隐藏溢出内容。
	 .meteor类定义了流星的样式,使用linear-gradient实现流星的渐变效果。
	 @keyframes meteor定义了流星从屏幕顶部移动到底部的动画,并逐渐消失。
	 
	 JavaScript部分:
	 createMeteor函数用于创建流星元素,并设置其初始位置和动画持续时间。
	 setInterval(createMeteor, 500)每隔500毫秒创建一个新的流星元素。
	 当流星动画结束时,使用animationend事件监听器将其从DOM中移除,以避免内存泄漏。
	 -->
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>流星雨特效</title>
    <style>
        body {
            margin: 0;
            overflow: hidden;
            background-color: #000;
        }
        .meteor {
            position: absolute;
            width: 2px;
            height: 100px;
            background: linear-gradient(to bottom, rgba(255, 255, 255, 0), rgba(255, 255, 255, 1));
            animation: meteor 1s linear infinite;
        }
        @keyframes meteor {
            0% {
                transform: translateY(-100px) rotate(-45deg);
                opacity: 1;
            }
            100% {
                transform: translateY(100vh) rotate(-45deg);
                opacity: 0;
            }
        }
    </style>
</head>
<body>
    <script>
        function createMeteor() {
            const meteor = document.createElement('div');
            meteor.className = 'meteor';
            meteor.style.left = Math.random() * window.innerWidth + 'px';
            meteor.style.animationDuration = Math.random() * 2 + 5 + 's';
            document.body.appendChild(meteor);
            meteor.addEventListener('animationend', () => {
                document.body.removeChild(meteor);
            });
        }
        setInterval(createMeteor, 500);
    </script>
</body>
</html>(2)方式2:升级版本
【1】先看实现效果
【2】再看实现思路
- CSS部分:
 body设置为黑色背景,并隐藏溢出内容。.meteor类定义了流星的样式,使用linear-gradient实现流星的渐变效果。@keyframes meteor定义了流星从左上角移动到右下角的动画,并逐渐消失。
- JavaScript部分:
 createMeteor函数用于创建流星元素,并设置其初始位置和动画持续时间。setInterval(createMeteor, 500)每隔500毫秒创建一个新的流星元素。
- 当流星动画结束时,使用animationend事件监听器将其从DOM中移除,以避免内存泄漏。
【3】最后完整代码
<!DOCTYPE html>
<html lang="zh-CN">
	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<title>流星雨特效</title>
		<!-- 
	 CSS部分:
	 body设置为黑色背景,并隐藏溢出内容。
	 .meteor类定义了流星的样式,使用linear-gradient实现流星的渐变效果。
	 @keyframes meteor定义了流星从左上角移动到右下角的动画,并逐渐消失。
	 
	 JavaScript部分:
	 createMeteor函数用于创建流星元素,并设置其初始位置和动画持续时间。
	 setInterval(createMeteor, 500)每隔500毫秒创建一个新的流星元素。
	 当流星动画结束时,使用animationend事件监听器将其从DOM中移除,以避免内存泄漏。
	 -->
		<style>
			body {
				margin: 0;
				overflow: hidden;
				background-color: #000;
			}
			.meteor {
				position: absolute;
				width: 2px;
				height: 100px;
				background: linear-gradient(to bottom right, rgba(255, 255, 255, 0), rgba(255, 255, 255, 1));
				animation: meteor 1s linear infinite;
			}
			@keyframes meteor {
				0% {
					/* 左上角 */
					transform: translate(0, 0) rotate(-45deg);
					opacity: 1;
				}
				100% {
					/* 右下角 */
					transform: translate(100vw, 100vh) rotate(-45deg);
					opacity: 0;
				}
			}
		</style>
	</head>
	<body>
		<script>
			// 控制流星的数量,避免随着时间越来越密集的出现流星
			let count = 30;
			function createMeteor() {
				const meteor = document.createElement('div');
				meteor.className = 'meteor';
				meteor.style.left = Math.random() * window.innerWidth + 'px';
				meteor.style.top = Math.random() * window.innerHeight + 'px';
				meteor.style.animationDuration = Math.random() * 2 + 3 + 's';
				count = document.body.children.length;
				if (count <= 30) {
					document.body.appendChild(meteor);
				}
				meteor.addEventListener('animationend', () => {
					document.body.removeChild(meteor);
				});
			}
			setInterval(createMeteor, 500);
		</script>
	</body>
</html>(3)方式3:美化版本
【1】先看实现效果
【2】再看实现思路
- CSS部分:
 body设置为黑色背景,并隐藏溢出内容。.star类定义了星星的样式,使用border-radius实现圆形,并使用@keyframes twinkle实现星星闪烁的动画。.meteor类定义了流星的样式,使用linear-gradient实现流星的渐变效果。@keyframes meteor定义了流星从左上角移动到右下角的动画,并逐渐消失。
- JavaScript部分:
 createStar函数用于创建星星元素,并设置其初始位置和动画持续时间。createMeteor函数用于创建流星元素,并设置其初始位置和动画持续时间。setInterval(createMeteor, 500)每隔500毫秒创建一个新的流星元素。当流星动画结束时,使用animationend事件监听器将其从DOM中移除,以避免内存泄漏。
- 在页面加载时,创建100颗星星。
【3】最后完整源码
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>流星雨与星光闪闪特效</title>
	<!-- 
	 CSS部分:
	 body设置为黑色背景,并隐藏溢出内容。
	 .star类定义了星星的样式,使用border-radius实现圆形,并使用@keyframes twinkle实现星星闪烁的动画。
	 .meteor类定义了流星的样式,使用linear-gradient实现流星的渐变效果。
	 @keyframes meteor定义了流星从左上角移动到右下角的动画,并逐渐消失。
	 
	 JavaScript部分:
	 createStar函数用于创建星星元素,并设置其初始位置和动画持续时间。
	 createMeteor函数用于创建流星元素,并设置其初始位置和动画持续时间。
	 setInterval(createMeteor, 500)每隔500毫秒创建一个新的流星元素。
	 当流星动画结束时,使用animationend事件监听器将其从DOM中移除,以避免内存泄漏。
	 在页面加载时,创建100颗星星。
	 -->
    <style>
        body {
            margin: 0;
            overflow: hidden;
            background-color: #000;
            position: relative;
        }
        .star {
            position: absolute;
            width: 2px;
            height: 2px;
            background-color: #fff;
            border-radius: 50%;
            animation: twinkle 1s infinite alternate;
        }
        @keyframes twinkle {
            0% {
                opacity: 0.5;
            }
            100% {
                opacity: 1;
            }
        }
        .meteor {
            position: absolute;
            width: 2px;
            height: 100px;
            background: linear-gradient(to bottom right, rgba(255, 255, 255, 0), rgba(255, 255, 255, 1));
            animation: meteor 1s linear infinite;
        }
        @keyframes meteor {
            0% {
                transform: translate(0, 0) rotate(-45deg);
                opacity: 1;
            }
            100% {
                transform: translate(100vw, 100vh) rotate(-45deg);
                opacity: 0;
            }
        }
    </style>
</head>
<body>
    <script>
        function createStar() {
            const star = document.createElement('div');
            star.className = 'star';
            star.style.left = Math.random() * window.innerWidth + 'px';
            star.style.top = Math.random() * window.innerHeight + 'px';
            star.style.animationDuration = Math.random() * 2 + 1 + 's';
            document.body.appendChild(star);
        }
        function createMeteor() {
            const meteor = document.createElement('div');
            meteor.className = 'meteor';
            meteor.style.left = Math.random() * window.innerWidth + 'px';
            meteor.style.top = Math.random() * window.innerHeight + 'px';
            meteor.style.animationDuration = Math.random() * 2 + 1 + 's';
            document.body.appendChild(meteor);
            meteor.addEventListener('animationend', () => {
                document.body.removeChild(meteor);
            });
        }
        // 创建星星
        for (let i = 0; i < 100; i++) {
            createStar();
        }
        // 创建流星
        setInterval(createMeteor, 500);
    </script>
</body>
</html>如果文章对你有收获,还请不要吝啬【点赞??收藏?评论?】三连哦,你的鼓励将是我成长助力之一!谢谢!
猜你喜欢
- 2024-10-24 初探animation中steps()属性(animation steps属性)
- 2024-10-24 HTML5(九)——超强的 SVG 动画(htmlsvg动画代码)
- 2024-10-24 自定义日历(二)(自定义日历控件)
- 2024-10-24 Flutter简单动画Animation运用(flutter 视频教程)
- 2024-10-24 css3中动画animation中的steps()函数
- 2024-10-24 移动端渲染原理浅析(移动端渲染原理浅析设计)
- 2024-10-24 iOS 事件处理机制与图像渲染过程(简述ios中的事件响应机制)
- 2024-10-24 Android 开机问题分析(android无法开机)
- 2024-10-24 GoogleCTF + zer0ptsCTF + ImaginaryCTF 2023 笔记
- 2024-10-24 决战“金三银四”,中高级Web前端大厂面试秘籍:CSS篇
- 最近发表
- 
- 聊一下 gRPC 的 C++ 异步编程_grpc 异步流模式
- [原创首发]安全日志管理中心实战(3)——开源NIDS之suricata部署
- 超详细手把手搭建在ubuntu系统的FFmpeg环境
- Nginx运维之路(Docker多段构建新版本并增加第三方模
- 92.1K小星星,一款开源免费的远程桌面,让你告别付费远程控制!
- Go 人脸识别教程_piwigo人脸识别
- 安卓手机安装Termux——搭建移动服务器
- ubuntu 安装开发环境(c/c++ 15)_ubuntu安装c++编译器
- Rust开发环境搭建指南:从安装到镜像配置的零坑实践
- Windows系统安装VirtualBox构造本地Linux开发环境
 
- 标签列表
- 
- cmd/c (90)
- c++中::是什么意思 (84)
- 标签用于 (71)
- 主键只能有一个吗 (77)
- c#console.writeline不显示 (95)
- pythoncase语句 (88)
- es6includes (74)
- sqlset (76)
- apt-getinstall-y (100)
- node_modules怎么生成 (87)
- chromepost (71)
- flexdirection (73)
- c++int转char (80)
- mysqlany_value (79)
- static函数和普通函数 (84)
- el-date-picker开始日期早于结束日期 (76)
- js判断是否是json字符串 (75)
- c语言min函数头文件 (77)
- asynccallback (87)
- localstorage.removeitem (77)
- vector线程安全吗 (73)
- java (73)
- js数组插入 (83)
- mac安装java (72)
- 无效的列索引 (74)
 
