雪花飘落动画

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>雪花</title>
<style>
html,body{ margin: 0; height: 100%; background-color: #151515; }
body{ background-image: url(img/bg.jpg); background-position: center center; background-repeat: no-repeat; background-color: #0D0B18; }
#snowBox{position: relative;width: 100%;height: 100%;margin: 0 auto;/*background: #33EAE8;*/overflow: hidden;}
.snowFlake{position: absolute;}
</style>
</head>
<body>
<div id="snowBox"></div>
<script>
//判断IE
function isIE(){
var b = document.createElement(‘b‘);
b.innerHTML = ‘<!--[if lte IE 8]><i></i><![endif]-->‘;
return b.getElementsByTagName(‘i‘).length === 1;
};
//定义雪花
function CreateSnow(snowBox,src,style){
this.snowBox = document.getElementById(snowBox);//找到容器
this.snowStyle = Math.ceil(Math.random()*style);//雪花类型[1,2]
this.maxLeft = this.snowBox.offsetWidth-Math.random()*5+3;//运动范围
this.maxTop = this.snowBox.offsetHeight-Math.random()*5+3;
this.left = this.snowBox.offsetWidth*Math.random();//起始位置
this.top = this.snowBox.offsetHeight*Math.random();
this.angle=1.1+0.8*Math.random();//飘落角度
this.minAngle=1.1;
this.maxAngle=1.9;
this.angleDelta=0.01*Math.random();
this.speed=1.4+Math.random();//下落速度
this.createEle(src);//制作雪花dom 凹=放在最后,使得原型里能取到值
};
//雪片生成+下落
CreateSnow.prototype = {
createEle : function(baseSrc){//生成雪花元素
var srcIndex = baseSrc.lastIndexOf(‘.‘),//获取最后一个‘.‘
src = baseSrc.substring(0,srcIndex)+this.snowStyle+baseSrc.substring(srcIndex,baseSrc.length);
var image = new Image();
image.src = src;
this.ele = document.createElement("img");
this.ele.setAttribute(‘src‘,src);
this.ele.style.position="absolute";
this.ele.style.zIndex="99";
this.snowBox.appendChild(this.ele);
//设置雪花尺寸
var img = this.ele;
image.onload = function(){
imgW = image.width;
img.setAttribute(‘width‘,Math.ceil(imgW*(0.1+Math.random())));
if(isIE()){//如果旧IE,设置高度
imgH = image.height;
img.setAttribute(‘height‘,Math.ceil(imgH*(0.1+Math.random())));
}
};
},
move : function(){//雪花运动
if(this.angle< this.minAngle||this.angle>this.maxAngle){
this.angleDelta=-this.angleDelta;
}
this.angle+=this.angleDelta;
this.left+=this.speed*Math.cos(this.angle*Math.PI);
this.top-=this.speed*Math.sin(this.angle*Math.PI);
if(this.left<0){
this.left=this.maxLeft;
}else if(this.left>this.maxLeft){
this.left=0
}
if(this.top>this.maxTop){//雪花掉出来后回到顶部
this.top=0;
}
this.ele.style.left=this.left+‘px‘;//凹=加‘px’
this.ele.style.top=this.top+‘px‘;
}
};
//下雪启动
function goSnow(snowBox,src,num,style){
var snowArr = [];
for(var j = 0 ; j<num ; j++){
snowArr.push(new CreateSnow(snowBox,src,style));
}
var makeSnowtime = setInterval(function(){
for(var i = snowArr.length-1;i>=0;i--){//找到数组中的最新的一个
if(snowArr[i]){
snowArr[i].move();
}
}
},40);
};
//初始化加载
window.onload = function(){
var snowBox = ‘snowBox‘,//雪花容器
src = ‘./img/snow.png‘,//雪花图基本命名<图片名就是snow+1/2/3/4...>
num = 16,//雪花数量
style = 2;//图片种类数
goSnow(snowBox,src,num,style);
};
</script>
</body>
</html>

时间: 2024-08-06 09:33:47

雪花飘落动画的相关文章

HTML5 canvas绘制雪花飘落动画(需求分析、知识点、程序编写分布详解)

看到网上很多展示html5雪花飞动的效果,确实非常引人入胜,我相信大家也跟我一样看着心动的同时,也很好奇,想研究下代码如何实现:虽然哦很多地方也能下载这些源码,不过也不知道别人制作此类动画时的思路及难点分析. 我这几天刚好学习了一下,也趁着此刻有时间从需求分析.知识点.程序编写一步步给大家解剖下,要是在各位关公面前耍大刀了,可别见笑哟. 最终效果图如下: 图1 一.需求分析 1.圆形雪花 本示例中雪花形状使用圆形 2.雪花数量固定 根据图1仔细观察白色雪花数量,飘落过程中,整张图的雪花数量应该是

【OpenGL】Shader实例分析(七)- 雪花飘落效果

转发请保持地址:http://blog.csdn.net/stalendp/article/details/40624603 研究了一个雪花飘落效果.感觉挺不错的.分享给大家,效果例如以下: 代码例如以下: Shader "shadertoy/Flakes" { // https://www.shadertoy.com/view/4d2Xzc Properties{ iMouse ("Mouse Pos", Vector) = (100,100,0,0) iChan

Qt实现桌面动态背景雪花飘落程序

        曾经收到过一份礼物,一个雪花飘落的程序,觉得效果很炫,通过前几篇的学习,我们已经掌握了贴图的一些技巧了,那么现在就可以自己实现了(当然你必须先拥有qt信号与槽的基础知识),这里先看效果,然后再分析如何实现. 效果图:          这个程序实现很久了,也是当初学习qt的时候写的,因为工作的原因,当初的部分设想,并没有全部实现,现在分享,供大家一起学习.         当初的设想:                 1.雪花随机飘落                 2.地面的花

今天大雪 看雪花飘落HTML5特效

今天大雪,弄一个下雪的特效.html5飘落的雪花堆积动画特效 查看效果:http://hovertree.com/texiao/js/snow.htm 以下是完整源代码,保存到HTML文件也可以看效果: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title&

手写简单的jq雪花飘落

闲来无事,准备写个雪花飘落的效果,没有写太牛逼的特效,极大的简化了代码量,这样容易读取代码,用起来也很简单,对于那些小白简直是福利啊,简单易读易学.先直接上代码吧,然后再一一讲解,直接复制粘贴就可以拿来用了,改起来更是容易. <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>雪花飘落</title> </head> <style

原生js实现雪花飘落效果

雪花飘落的效果实现步骤:1.使用setInterval定时器每800毫秒创建一个雪花:2.把每一个雪花作为参数传进动态下落的方法中即可. <style> *{padding: 0;margin: 0;} body{ background:#000; width: 100%; height: 100%; overflow:hidden; } </style> <div id="flame"></div> js实现代码: <script

HTML5树叶飘落动画

查看效果:http://keleyi.com/keleyi/phtml/css3/15.htm 请使用Chrome浏览器查看本效果. html源代码: <!DOCTYPE HTML> <html> <head> <title>HTML5树叶飘落动画-柯乐义</title><base target="_blank" href="http://keleyi.com/keleyi/phtml/css3/"

Android实战简易教程-第六十九枪(自定义控件实现雪花飘落效果)

现在APP要求越来越高了,不只是要求实现功能,颜值的要求也越来越高,下面我们通过自定义控件来实现雪花飘落的效果,可以作为界面背景哦. 1.自定义控件: package com.test.a; import java.util.Random; import android.content.Context; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.Can

电脑全屏雪花飘落的背景特效

1 <html> 2 <head> 3 <title>全屏雪花飘落的背景特效丨电表控制器</title> 4 <script language="JavaScript"> 5 <!-- 6 Amount=20; //Smoothness! depends on image file size, the smaller the size the more you can use! 7 //Pre-load your ima