<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" type="text/css" href="css/snow.css">
<style type="text/css">
.box{width: 100%; height: 900px; background: #000;}
</style>
</head>
<body>
<div class="box"></div>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/snow.js"></script>
</body>
</html>

snow.css

/*雪花飘落*/
.snow-container {
position: fixed;
top:;
left:;
width: 100%;
pointer-events: none;
z-index:;
} .snow-container .snow {
position: absolute;
background: #fff;
-webkit-border-radius: 50%;
border-radius: 50%;
-webkit-box-shadow: 0 0 15px rgba(255, 255, 255, 0.8);
box-shadow: 0 0 15px rgba(255, 255, 255, 0.8);
background: url(../images/snow.png) no-repeat\9;
} :root .snow-container .snow { }

snow.js

var isSupportCss3 = (function(){
var ret = /MSIE (\d+\.\d+)/.exec(navigator.userAgent);
if( !ret || ret[1] > 9 ){
return true;
}
return false;
})(); (function () { Function.prototype.bind = Function.prototype.bind || function(){
var self = this,
context = [].shift.call(arguments),
args = [].slice.call(arguments);
return function(){
return self.apply(context, [].concat.call(args, [].slice.call(arguments)));
}
}; function getRandom(min, max) {
return min + Math.random()*(max-min);
} function getWindowSize() {
return {
clientW: window.innerWidth || document.documentElement.clientWidth,
clientH: window.innerHeight || document.documentElement.clientHeight
}
} var clientSize = getWindowSize();
var body = document.body; function Snow(container, opts) {
this.container = container;
this.opts = opts;
this.create();
} Snow.prototype = {
create: function () {
this.el = document.createElement("div");
this.el.className = 'snow';
this.el.style["width"] = this.opts.snowWidth + "px";
this.el.style["height"] = this.opts.snowHeight + "px";
this.el.style["top"] = -this.opts.snowHeight + "px";
this.el.style["-webkit-transition"] = "all " + this.opts.speed + "ms linear";
this.el.style["transition"] = "all " + this.opts.speed + "ms linear"; this.container.appendChild(this.el);
this.fall();
},
fall: function () {
var self = this;
var left = getRandom(0, clientSize.clientW - this.opts.snowWidth);
var destLeft = getRandom(-300, 300);
var scale = getRandom(0.6, 1); this.el.style["left"] = left + "px";
this.el.style["-ms-transform"] = "scale("+ scale +")";
this.el.style["-webkit-transform"] = "scale("+ scale +")";
this.el.style["transform"] = "scale("+ scale +")"; body.offsetWidth;
var transformStyle = "scale("+ scale +") translate3d("+ destLeft +"px,"+ (clientSize.clientH + this.opts.snowHeight)*2 +"px,0px)";
this.el.style["-webkit-transform"] = transformStyle;
this.el.style["transform"] = transformStyle; //当前页面失去焦点时,通过transitionend的方式移除this.el会有问题,因此通过这种方式移除
$({y: -this.opts.snowHeight, left: left}).animate({
y: (clientSize.clientH + this.opts.snowHeight)*(1/scale),
left: left + destLeft
}, {
easing: 'linear',
duration: this.opts.speed,
step: function ( value, obj) {
if( !isSupportCss3 ){
if( obj.prop == 'y' ) {
self.el.style.top = obj.now + "px";
}
if( obj.prop == 'left' ){
self.el.style.left = obj.now + "px";
}
}
},
complete: function () {
self.reset();
}
});
},
reset: function () {
try {
this.container.removeChild(this.el);
} catch (e){
console.error(e.message);
}
}
}; function SnowFall(opts){
this.opts = $.extend({
interval: 200,
speed: 15000,
snowWidth: 8,
snowHeight: 8
}, opts||{}); this.timer = null;
this.body = document.body; this.init();
} SnowFall.prototype = {
init: function () {
this.createLayout();
this.start();
},
start: function () {
new Snow(this.container, this.opts);
this.timer = setTimeout(function () {
this.start();
}.bind(this), this.opts.interval);
},
createLayout:function () {
this.container = document.createElement("div");
this.container.className = 'snow-container';
this.body.appendChild(this.container);
},
destroy: function () {
if( this.timer ) clearTimeout(this.timer);
this.container.parentNode.removeChild(this.container);
}
}; $(function () {
$(window).on("resize", function () {
clientSize = getWindowSize();
});
}); new SnowFall(); })();

最新文章

  1. NOIP2012同余方程
  2. javascript方法链式调用和构造函数链式调用对比
  3. css之滚动条
  4. Android开发(二十六)——Application
  5. 实现虚拟模式的动态数据加载Windows窗体DataGridView控件 .net 4.5 (一)
  6. javascript笔记——点击按钮(或超链接)如何跳转到另外一个页面并执行目标页面的js函数
  7. 九度OJ 1511 从尾到头打印链表
  8. position与anchorPoint
  9. 函数响应式编程及ReactiveObjC学习笔记 (-)
  10. 初学者易上手的SSH-struts2 05拦截器与自定义拦截器
  11. 排序算法Java实现(堆排序)
  12. python课程安排
  13. 【XSY2032】简单粗暴的题目 组合数
  14. [pytorch修改]npyio.py 实现在标签中使用两种delimiter分割文件的行
  15. Apache Struts2高危漏洞(S2-057CVE-2018-11776)
  16. python爬虫之urllib
  17. 【APP测试(Android)】--安全测试
  18. loj#6491. zrq 学反演
  19. Comet OJ - Contest #2 简要题解
  20. Mybatis批量更新和插入

热门文章

  1. Centos7.3云服务器上安装Nginx、MySQL、JDK、Tomcat环境
  2. 乔布斯在位时,库克实质上已经在做CEO的工作了:3星|《蒂姆&#183;库克传》
  3. Spring Cloud 组件 —— hystrix
  4. test20190526 Noip 模拟赛 4
  5. 十七.protobuf在rpc中的使用
  6. Flask - flask-script | 多app应用 | wtforms
  7. PostgreSQL 抛出错误信息(错误行号)
  8. Linux 命令 ipcs/ipcrm
  9. jsp文件上传下载组件
  10. [分享]Passcape Software - Windows Password Recovery