直接上代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>下雪效果实现</title>
<link rel="stylesheet" type="text/css" href="reset.css">
<style type="text/css">
body,html{
overflow: hidden;
}
</style>
</head>
<body>
<script type="text/javascript">
var snowflakes = {
arr:[],// 数组盛放元素
snowflake : [//雪花类型
'"red",
"green",
"#ccc123",
"#345232",
"#231111",
"#ab2322"
],
random : function (num){
return Math.floor(Math.random()*num);// 获得一个num-1的整数
},
init : function (num){
// 最多个数
this.maxlength = num;
// 边界
this.maxWidth = (document.documentElement.clientWidth || document.body.clientWidth) + 20;
// 边界
this.maxHeight = (document.documentElement.clientHeight || document.body.clientHeight) + 20;
this.create();
this.move();
},
// 创建
create : function (){
var that = this;
setInterval(function (){
// 当数组中的数量,比最大数量要小的时候 开始创建
if( that.arr.length < that.maxlength){
var d = document.createElement("div");
// 内容和 颜色是随机的 颜色和文字库里面的
d.innerHTML = that.snowflake[that.random(that.snowflake.length)];
d.style.color = that.snowflakeColor[that.random(that.snowflakeColor.length)];
d.style.position = "absolute";
// 位置是随机的 top(0- -99) left (0 - that.maxWidth*2/3-1)
d.style.left = that.random(that.maxWidth*2/3) + "px";
d.style.top = -that.random(100) + "px";
// 速度
d.vx = 2+that.random(10);
d.vy = 3+that.random(10);
// 数组添加元素, body 添加元素
document.body.appendChild(d);
that.arr.push(d)
}
},20)
},
// 运动
move : function (){
var that = this;
var arr = that.arr;
setInterval(function (){
// 循环数组中的每一个元素
for(var i = 0 ; i < arr.length ; i ++ ){
// 替换位置
arr[i].style.left = arr[i].offsetLeft + arr[i].vx + "px";
arr[i].style.top = arr[i].offsetTop + arr[i].vy + 'px';
// 判断边界 删除元素
if (arr[i].offsetTop >= that.maxHeight || arr[i].offsetLeft >= that.maxWidth) {
document.body.removeChild(arr[i]);
arr.splice(i,1);
}
}
},30)
}
}
window.onload = function (){
snowflakes.init(100);
}
</script>
</body>
</html>
效果图
以上就是JavaScript 实现下雪特效的示例代码的详细内容,更多关于JavaScript 实现下雪特效的资料请关注其它相关文章!
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
暂无“JavaScript 实现下雪特效的示例代码”评论...
更新动态
2025年10月31日
2025年10月31日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]
