首页 / CSS / 有趣的css—简单的下雨效果
有趣的css—简单的下雨效果
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了有趣的css—简单的下雨效果,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3854字,纯文字阅读大概需要6分钟。
内容图文
简单的下雨效果
前言
最近在b站上看到一个下雨效果的视频,感觉思路很清奇,我也按照自己的思路做了一个简单的下雨效果。
由于我制作GIF图片的工具最多只支持制作33FPS的GIF图,所以看起来可能有一点点卡顿,实际的效果比图片还是要好一些的,点击这里可以在线查看效果。
思路
制作背景
首先给body中添加一个id为rain的div,并通过背景颜色线性渐变得到天空-地平线-海面的效果。
<meta name="charset" content="utf-8">
<title>简单的下雨效果</title>
<div ></div>
#rain {
position: relative;
height: 100%;
background: linear-gradient(#333,#999 ,#1f4794);
background-repeat: no-repeat;
background-size: 100% 100%;
}
制作雨滴
通过设置背景颜色径向渐变得到圆形的水滴,再将其沿Y轴进行旋转得到椭圆形的水滴,最后给其添加水滴下落的动画效果。
.raindrop {
display: inline-block;
position: absolute;
top: 0;
left: 150px;
width: 5px;
height: 5px;
background: radial-gradient(#8fd4fc, #52b1f2, #0599fc);
border-radius: 5000px;
transform: rotateY(45deg);
animation: raindrop .8s;
}
@keyframes raindrop {
0% {top:5%;}
10% {top:10%;}
20% {top:20%;}
30% {top:30%;}
40% {top:40%;}
50% {top:50%;}
60% {top:60%;}
70% {top:70%;}
80% {top:80%;}
90% {top:90%;}
100% {top:95%;}
}
动态添加大批量的雨滴
通过appendChild添加随机位置的雨滴节点,并随机在400ms~750ms之间通过removeChild将其移除。
let clientWidth;
let clientHeight;
window.onload = function onload(){
let rain = document.getElementById(‘rain‘);
clientWidth = document.body.clientWidth;
clientHeight = document.body.clientHeight;
function dorpRain(){
setTimeout(() => {
if(typeof clientWidth !== ‘undefined‘ && null !== clientWidth){
let el = document.createElement(‘div‘);
el.setAttribute(‘class‘, ‘raindrop‘);
el.style.left = parseInt(Math.random() * clientWidth, 10) + ‘px‘;
rain.appendChild(el);
setTimeout(() => {
rain.removeChild(el);
}, parseInt(400 + Math.random() * 350, 10))
}
dorpRain();
}, parseInt(10 + Math.random() * 10, 10))
}
dorpRain();
}
制作波纹效果
通过背景透明和圆形边框得到圆形的环,再将其沿X轴进行旋转得到椭圆形的环,最后给其添加环逐渐扩大的动画效果。
.ripple {
display: inline-block;
position: absolute;
top: 60vh;
left: 50vh;
border: 2px solid #8fd4fc;
border-radius: 5000px;
background: rgba(0, 0, 0, 0);
transform: rotateX(72deg);
animation: ripple .6s;
}
@keyframes ripple {
0% {
width: 2px;
height: 2px;
}
10% {
width: 4px;
height: 4px;
}
20% {
width: 6px;
height: 6px;
}
30% {
width: 8px;
height: 8px;
}
40% {
width: 10px;
height: 10px;
}
50% {
width: 12px;
height: 12px;
}
60% {
width: 14px;
height: 14px;
}
70% {
width: 16px;
height: 16px;
}
80% {
width: 18px;
height: 18px;
}
90% {
width: 20px;
height: 20px;
}
100% {
width: 22px;
height: 22px;
}
}
动态添加大批量的波纹
通过appendChild添加随机位置的雨滴节点,并在动画效果过后通过removeChild将其移除。
let clientWidth;
let clientHeight;
window.onload = function onload(){
let rain = document.getElementById(‘rain‘);
clientWidth = document.body.clientWidth;
clientHeight = document.body.clientHeight;
function ripple(){
setTimeout(() => {
if(typeof clientWidth !== ‘undefined‘ && null !== clientWidth && typeof clientHeight !== ‘undefined‘ && null !== clientHeight){
let el = document.createElement(‘div‘);
el.setAttribute(‘class‘, ‘ripple‘);
el.style.left = parseInt(Math.random() * clientWidth, 10) + ‘px‘;
el.style.top = parseInt(clientHeight / 100 * 50 + (Math.random() * (clientHeight / 100 * 50)), 10) + ‘px‘;
rain.appendChild(el);
setTimeout(() => {
rain.removeChild(el);
}, 600)
}
ripple();
}, parseInt(10 + Math.random() * 10, 10))
}
ripple();
}
细节
最后再完善一些细节,比如window.onresize监听窗口变化以及overflow: hidden隐藏超出屏幕外的内容等等。
#rain {
position: relative;
height: 100%;
overflow: hidden;
background: linear-gradient(#333,#999 ,#1f4794);
background-repeat: no-repeat;
background-size: 100% 100%;
}
let clientWidth;
let clientHeight;
window.onresize = function onresize(){
clientWidth = document.body.clientWidth;
clientHeight = document.body.clientHeight;
}
结尾
笔者才疏学浅,慌忙之下难免有遗漏或是疏忽,如有错误之处,还望各位看官不吝赐教,笔者在此感谢。
最终的代码我放在简单的下雨效果。
作者:Fatman
博客园地址:https://www.cnblogs.com/liujingjiu
CSDN地址:https://blog.csdn.net/qq_35508835
版权归Fatman所有,欢迎保留原文链接进行转载:)
原文:https://www.cnblogs.com/liujingjiu/p/14775653.html
内容总结
以上是互联网集市为您收集整理的有趣的css—简单的下雨效果全部内容,希望文章能够帮你解决有趣的css—简单的下雨效果所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。