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> & ...
随机推荐
- 【jquery】基础知识
jquery简介 1 jquery是什么 jquery由美国人John Resig创建,至今已吸引了来自世界各地的众多 javascript高手加入其team. jQuery是继prototype之后 ...
- 解决springmvc报No converter found for return value of type: class java.util.ArrayList问题
一.背景 最近闲来无事,想自己搭建一套Spring+SpringMVC+Mybatis+Mysql的环境(搭建步骤会在以后博客中给出),结果运行程序时,适用@ResponseBody注解进行返回Lis ...
- 如何在Eclipse下查看JDK源代码
在Eclipse中查看JDK类库的源代码!!! 设置: 1.点 "window"-> "Preferences" -> "Java&quo ...
- Vue.js 快速入门
什么是Vue.js vue是法语中视图的意思,Vue.js是一个轻巧.高性能.可组件化的MVVM库,同时拥有非常容易上手的API.作者是尤雨溪,写下这篇文章时vue.js版本为1.0.7 准备 我推荐 ...
- yii 简单依赖注入
<?phpnamespace app\controllers;use Yii;use yii\di\Container;use yii\di\ServiceLocator;use yii\web ...
- Chrome Extension 检查视图(无效)处理方法
最近闲来无事,简单看了下Chrome扩展的开发,并且开发一个小小的翻译插件(TranslateBao)作为练手,开发细节不详述了,如果有新学习chrome extension开发的新人,可以参考源码, ...
- sql 语句
INSERT 基本语法:INSERT INTO table_name VALUES(value1,value2,value3,...); 指定列:INSERT INTO table_name(colu ...
- MySLQ 为数据库远程授权的方法与问题的解决解决方法
Mysql通过远程的连接工具连接,提示Can't connect to MySQL server (10060). 这个时候我们需要分析,看哪里设置不当而导致的该问题. 工具/原料 mysql数 ...
- react native 中webview内的点击事件传到外部原生调用
先说一下我使用webview的时候遇到的一个功能需求 是这样的,上图中的这个页面是用h5做的,但是由于点击"我的优惠劵"是需要跳转到我原生的页面,也就是说我需要获得这个h5提供的点 ...
- display:inline-block的间隙问题和解决办法
1.display:inline-block在水平方向的间隙: 代码如下: <style type="text/css"> *{margin:0; padding:0; ...