【css酷炫效果】纯CSS实现粒子旋转动画

【css酷炫效果】纯CSS实现粒子旋转动画

想直接拿走的老板,链接放在这里:https://download.csdn.net/download/u011561335/90492008

创作随缘,不定时更新。

创作背景

刚看到csdn出活动了,赶时间,直接上代码。

html结构

clike 复制代码
	<div id="particle-container" class='particle-container'></div>

css样式

clike 复制代码
/* 基本样式重置 */
body, html {
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100%;
    overflow: hidden; /* 防止滚动条出现 */
    background-color: #000; /* 设置背景颜色为黑色 */
}
 
/* 粒子容器 */
.particle-container {
    position: relative;
    width: 100%;
    height: 100%;
    pointer-events: none; /* 使容器内的粒子不影响鼠标事件 */
}
 
/* 粒子样式 */
.particle-container::before,
.particle-container::after,
.particle {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 2px;
    height: 2px;
    background-color: rgba(255, 255, 255, 0.8); /* 设置粒子颜色为白色,带有一定的透明度 */
    border-radius: 50%;
    box-shadow: 0 0 10px rgba(255, 255, 255, 0.5); /* 添加一些模糊效果 */
    animation: particleAnimation 5s infinite ease-in-out;
    pointer-events: none;
}
 
/* 使用伪元素生成一些粒子 */
.particle-container::before,
.particle-container::after {
    width: 4px;
    height: 4px;
}
 
/* 使用大量类生成更多粒子 */
.particle:nth-child(1) { top: 5%; left: 10%; animation-delay: 0s; }
.particle:nth-child(2) { top: 15%; left: 20%; animation-delay: 0.2s; }
.particle:nth-child(3) { top: 25%; left: 30%; animation-delay: 0.4s; }

/* 粒子动画关键帧 */
@keyframes particleAnimation {
    0% {
        transform: translate(-50%, -50%) rotate(0deg);
        opacity: 1;
    }
    100% {
        transform: translate(-50%, -50%) rotate(360deg) translate(200px, -200px);
        opacity: 0;
    }
}

完整代码

clike 复制代码
<!DOCTYPE html>
<html lang="en"> 
<head>

<title>页面特效</title>
<style type="text/css">
/* 基本样式重置 */
body, html {
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100%;
    overflow: hidden; /* 防止滚动条出现 */
    background-color: #000; /* 设置背景颜色为黑色 */
}
 
/* 粒子容器 */
.particle-container {
    position: relative;
    width: 100%;
    height: 100%;
    pointer-events: none; /* 使容器内的粒子不影响鼠标事件 */
}
 
/* 粒子样式 */
.particle-container::before,
.particle-container::after,
.particle {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    width: 2px;
    height: 2px;
    background-color: rgba(255, 255, 255, 0.8); /* 设置粒子颜色为白色,带有一定的透明度 */
    border-radius: 50%;
    box-shadow: 0 0 10px rgba(255, 255, 255, 0.5); /* 添加一些模糊效果 */
    animation: particleAnimation 5s infinite ease-in-out;
    pointer-events: none;
}
 

 
@keyframes particleAnimation {
    0% {
        transform: translate(-50%, -50%) rotate(0deg);
        opacity: 1;
    }
    100% {
        transform: translate(-50%, -50%) rotate(360deg) translate(200px, -200px);
        opacity: 0;
    }
}

</style>

</head>
<body>

	<h1 style='color:red;'>斗破苍穹!</h1>
	<div id="particle-container" class='particle-container'></div>
	

</body>

<script>
    const container = document.getElementById('particle-container');
    const numberOfParticles = 100;

	setInterval(function(){
		for (let i = 0; i < numberOfParticles; i++) {
			const particle = document.createElement('div');
			particle.classList.add('particle');
			particle.style.top = Math.random() * 95+ 'vh';
			particle.style.left = Math.random() * 95+ 'vw';
			particle.style.animationDelay = Math.random() * 5 * 0.4+`s`;
			particle.style.width = (Math.floor(Math.random() * 4) + 1)+`px`;
			particle.style.height = Math.floor(Math.random() * 4) + 1+`px`;
			container.appendChild(particle);
		}
	},3000);

</script>

</html>

效果图

相关推荐
烂蜻蜓1 小时前
深入理解 Vue 3 项目结构与运行机制
前端·javascript·vue.js
han_hanker2 小时前
一个普通的vue权限管理方案-菜单权限控制
前端·javascript·vue.js
老大白菜3 小时前
lunar是一款无第三方依赖的公历 python调用
前端·python
混血哲谈5 小时前
如何使用webpack预加载 CSS 中定义的资源和预加载 CSS 文件
前端·css·webpack
浪遏6 小时前
我的远程实习(二) | git 持续更新版
前端
智商不在服务器7 小时前
XSS 绕过分析:一次循环与两次循环的区别
前端·xss
MonkeyKing_sunyuhua7 小时前
npm WARN EBADENGINE required: { node: ‘>=14‘ }
前端·npm·node.js
Hi-Jimmy7 小时前
【VolView】纯前端实现CT三维重建-CBCT
前端·架构·volview·cbct
janthinasnail8 小时前
编写一个简单的chrome截图扩展
前端·chrome
拉不动的猪8 小时前
刷刷题40(vue中计算属性不能异步,如何实现异步)
前端·javascript·vue.js