jquery/zepto 圣诞节雪花飞扬
下载地址:
http://www.html5tricks.com/jquery-html5-christ-snow.html
演示地址:
http://www.html5tricks.com/jquery-html5-christ-snow.html
个人的demo:
新建并且引用以下js文件:
(function($){
$.snowfall = function(element, options){
var defaults = {
flakeCount : 35,
flakeColor : '#ffffff',
flakeIndex: 999999,
minSize : 1,
maxSize : 2,
minSpeed : 1,
maxSpeed : 5,
round : false,
shadow : false,
collection : false,
collectionHeight : 40,
deviceorientation : false
},
options = $.extend(defaults, options),
random = function random(min, max){
return Math.round(min + Math.random()*(max-min));
};
$(element).data("snowfall", this);
// Snow flake object
function Flake(_x, _y, _size, _speed, _id)
{
// Flake properties
this.id = _id;
this.x = _x;
this.y = _y;
this.size = _size;
this.speed = _speed;
this.step = 0;
this.stepSize = random(1,10) / 100;
if(options.collection){
this.target = canvasCollection[random(0,canvasCollection.length-1)];
}
var flakeMarkup = $(document.createElement("div")).attr({'class': 'snowfall-flakes', 'id' : 'flake-' + this.id}).css({'width' : this.size, 'height' : this.size, 'background' : options.flakeColor, 'position' : 'absolute', 'top' : this.y, 'left' : this.x, 'fontSize' : 0, 'zIndex' : options.flakeIndex});
if($(element).get(0).tagName === $(document).get(0).tagName){
$('body').append(flakeMarkup);
element = $('body');
}else{
$(element).append(flakeMarkup);
}
this.element = document.getElementById('flake-' + this.id);
// Update function, used to update the snow flakes, and checks current snowflake against bounds
this.update = function(){
this.y += this.speed;
if(this.y > (elHeight) - (this.size + 6)){
this.reset();
}
this.element.style.top = this.y + 'px';
this.element.style.left = this.x + 'px';
this.step += this.stepSize;
if (doRatio === false) {
this.x += Math.cos(this.step);
} else {
this.x += (doRatio + Math.cos(this.step));
}
// Pileup check
if(options.collection){
if(this.x > this.target.x && this.x < this.target.width + this.target.x && this.y > this.target.y && this.y < this.target.height + this.target.y){
var ctx = this.target.element.getContext("2d"),
curX = this.x - this.target.x,
curY = this.y - this.target.y,
colData = this.target.colData;
if(colData[parseInt(curX)][parseInt(curY+this.speed+this.size)] !== undefined || curY+this.speed+this.size > this.target.height){
if(curY+this.speed+this.size > this.target.height){
while(curY+this.speed+this.size > this.target.height && this.speed > 0){
this.speed *= .5;
}
ctx.fillStyle = "#fff";
if(colData[parseInt(curX)][parseInt(curY+this.speed+this.size)] == undefined){
colData[parseInt(curX)][parseInt(curY+this.speed+this.size)] = 1;
ctx.fillRect(curX, (curY)+this.speed+this.size, this.size, this.size);
}else{
colData[parseInt(curX)][parseInt(curY+this.speed)] = 1;
ctx.fillRect(curX, curY+this.speed, this.size, this.size);
}
this.reset();
}else{
// flow to the sides
this.speed = 1;
this.stepSize = 0;
if(parseInt(curX)+1 < this.target.width && colData[parseInt(curX)+1][parseInt(curY)+1] == undefined ){
// go left
this.x++;
}else if(parseInt(curX)-1 > 0 && colData[parseInt(curX)-1][parseInt(curY)+1] == undefined ){
// go right
this.x--;
}else{
//stop
ctx.fillStyle = "#fff";
ctx.fillRect(curX, curY, this.size, this.size);
colData[parseInt(curX)][parseInt(curY)] = 1;
this.reset();
}
}
}
}
}
if(this.x > (elWidth) - widthOffset || this.x < widthOffset){
this.reset();
}
}
// Resets the snowflake once it reaches one of the bounds set
this.reset = function(){
this.y = 0;
this.x = random(widthOffset, elWidth - widthOffset);
this.stepSize = random(1,10) / 100;
this.size = random((options.minSize * 100), (options.maxSize * 100)) / 100;
this.speed = random(options.minSpeed, options.maxSpeed);
}
}
// Private vars
var flakes = [],
flakeId = 0,
i = 0,
elHeight = $(element).height(),
elWidth = $(element).width(),
widthOffset = 0,
snowTimeout = 0;
// Collection Piece ******************************
if(options.collection !== false){
var testElem = document.createElement('canvas');
if(!!(testElem.getContext && testElem.getContext('2d'))){
var canvasCollection = [],
elements = $(options.collection),
collectionHeight = options.collectionHeight;
for(var i =0; i < elements.length; i++){
var bounds = elements[i].getBoundingClientRect(),
canvas = document.createElement('canvas'),
collisionData = [];
if(bounds.top-collectionHeight > 0){
document.body.appendChild(canvas);
canvas.style.position = 'absolute';
canvas.height = collectionHeight;
canvas.width = bounds.width;
canvas.style.left = bounds.left + 'px';
canvas.style.top = bounds.top-collectionHeight + 'px';
for(var w = 0; w < bounds.width; w++){
collisionData[w] = [];
}
canvasCollection.push({element :canvas, x : bounds.left, y : bounds.top-collectionHeight, width : bounds.width, height: collectionHeight, colData : collisionData});
}
}
}else{
// Canvas element isnt supported
options.collection = false;
}
}
// ************************************************
// This will reduce the horizontal scroll bar from displaying, when the effect is applied to the whole page
if($(element).get(0).tagName === $(document).get(0).tagName){
widthOffset = 25;
}
// Bind the window resize event so we can get the innerHeight again
$(window).bind("resize", function(){
elHeight = $(element).height();
elWidth = $(element).width();
});
// initialize the flakes
for(i = 0; i < options.flakeCount; i+=1){
flakeId = flakes.length;
flakes.push(new Flake(random(widthOffset,elWidth - widthOffset), random(0, elHeight), random((options.minSize * 100), (options.maxSize * 100)) / 100, random(options.minSpeed, options.maxSpeed), flakeId));
}
// This adds the style to make the snowflakes round via border radius property
if(options.round){
$('.snowfall-flakes').css({'-moz-border-radius' : options.maxSize, '-webkit-border-radius' : options.maxSize, 'border-radius' : options.maxSize});
}
// This adds shadows just below the snowflake so they pop a bit on lighter colored web pages
if(options.shadow){
$('.snowfall-flakes').css({'-moz-box-shadow' : '1px 1px 1px #555', '-webkit-box-shadow' : '1px 1px 1px #555', 'box-shadow' : '1px 1px 1px #555'});
}
// On newer Macbooks Snowflakes will fall based on deviceorientation
var doRatio = false;
if (options.deviceorientation) {
$(window).bind('deviceorientation', function(event) {
doRatio = event.originalEvent.gamma * 0.1;
});
}
// this controls flow of the updating snow
function snow(){
for( i = 0; i < flakes.length; i += 1){
flakes[i].update();
}
snowTimeout = setTimeout(function(){snow()}, 30);
}
snow();
// Public Methods
// clears the snowflakes
this.clear = function(){
$(element).children('.snowfall-flakes').remove();
flakes = [];
clearTimeout(snowTimeout);
};
};
// Initialize the options and the plugin
$.fn.snowfall = function(options){
if(typeof(options) == "object" || options == undefined){
return this.each(function(i){
(new $.snowfall(this, options));
});
}else if (typeof(options) == "string") {
return this.each(function(i){
var snow = $(this).data('snowfall');
if(snow){
snow.clear();
}
});
}
};
})(zepto);
使用方式,请务必在页面完全加载之后使用以下代码,其他具体参数请查看演示地址的源码
$(document).snowfall({round : true, minSize: 2, maxSize:4}); // add rounded
jquery/zepto 圣诞节雪花飞扬的更多相关文章
- JS实现IOS风格对话框 jquery / zepto
Alert alert("这个是一个alert弹窗"); Alert 自定义参数 alert({ content: "自定义alert弹窗", btnText: ...
- 移动端下拉刷新、加载更多插件dropload.js(基于jQuery/Zepto)
移动端下拉刷新.加载更多插件dropload.js(基于jQuery/Zepto) 原文:http://www.grycheng.com/?p=1869 废话不多说,先让大家看一下案例效果: DEMO ...
- jQuery / zepto ajax 全局默认设置
jQuery / zepto 的 $.ajax 方法需要配置很多选项, 有些是很常用的每个 ajax 请求都要用到的, 可以全局设置, 避免每次都写. 注意: 此处用的 jQuery 版本是 1.8. ...
- 5分钟搞定jQuery+zepto.js+面向对象插件
今天分享一下快速使用jQuery+zepto.js的技巧,需要的记得收藏 1.jQuery的引入:本地下载jQuery(后面简称jq)的源文件,开发版本使用非min版,线上使用min版,zepto.j ...
- jquery,zepto插件编写相关
1. $.fn.pluginName = function(opt){}就是为jquery的prototype定义了函数, 这样, 任何一个jquery对象都可以使用这个成员函数, 这种写法直观明了, ...
- delaycall.js 修改表单延迟自动提交的 jQuery / Zepto 插件
delaycall.js delaycall 是一个 jQuery / Zepto 插件,用于在用户完成某项操作后,延迟指定秒数后自动调动指定函数.如用户输入完内容后,延迟1秒,自动提交表单. Git ...
- jQuery动画-圣诞节礼物
▓▓▓▓▓▓ 大致介绍 下午看到了一个送圣诞礼物的小动画,正好要快到圣诞节了,就动手模仿并改进了一些小问题 原地址:花式轮播----圣诞礼物传送 思路:动画中一共有五个礼物,他们平均分布在屏幕中,设置 ...
- 迷你版jQuery——zepto核心源码分析
前言 zepto号称迷你版jQuery,并且成为移动端dom操作库的首选 事实上zepto很多时候只是借用了jQuery的名气,保持了与其基本一致的API,其内部实现早已面目全非! 艾伦分析了jQue ...
- Jquery网站下雪花的效果
代码如下: <script type="text/javascript" src="jquery.min.js"></script> & ...
随机推荐
- Linux中杀不死的进程
前段时间,一哥们,去杀Linux服务器的进程,发现kill命令失灵了,怎么杀都杀不死. 然后上网查了下资料,原来是要被杀的进程,成为了僵尸进程. 僵尸进程的查看方法: 利用命令ps,可以看到有标记为Z ...
- ios 输入框bar设置
_textView = [[class alloc] init]; _textView.translatesAutoresizingMaskIntoConstraints = NO; ...
- iOS开发者联系 联系方式
苹果开发者客服电话地址:https://developer.apple.com/contact/phone.php 中国大陆地区客服电话: 中国香港地区客服电话:() 中国台湾地区客服电话: 链接地址 ...
- 微信公众平台oauth2.0网页授权参考资料
http://www.wangwenxiao.com/weixin/wxgzptoauth2_0wysq_12.html
- PHP Math 函数
abs() 绝对值. 3 acos() 反余弦. 3 acosh() 反双曲余弦. 4 asin() 反正弦. 3 asinh() 反双曲正弦. 4 atan() 反正切. 3 atan2() 两个参 ...
- (2)WebAPI的增删改查
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Ne ...
- android应用程序第一次启动时显示引导界面
市面上好多优秀的应用(举例新浪微博.UC浏览器)都采用了欢迎页面与使用向导的方式给用户带来了良好的用户体验. 一般来说用户第一次安装应用或者安装了新版本后第一次进入应用都会显示成 欢迎页面-使用向导- ...
- 解决java.beans.Introspector导致的内存泄漏
解决方案: 在WEB.XML 中配置监听器: <listener> <listener-class> org.springframework.web.util.Introspe ...
- linux菜鸟日记(2)
ntp服务的安装与配置: 安装ntp服务的过程比较简单首先你需要挂载光盘然后安装ntp服务如果配置了本地yum源可以直接使用光盘中的资源进行本地yum的安装,如果没有就使用rpm包进行安装. 由于我已 ...
- 【BZOJ】4144: [AMPPZ2014]Petrol
题意 给定一个\(n\)个点.\(m\)条边的带权无向图,其中有\(s\)个点是加油站.每辆车都有一个油量上限\(b\),即每次行走距离不能超过\(b\),但在加油站可以补满.\(q\)次询问,每次给 ...