jQuery canvas particles

create particles over HTML elements using canvas

Download .zip Download .tar.gz View on GitHub GitHub Project Page

Examples:
1
2
3
4
5
6

HTML
<!DOCTYPE html>
<html>
  <head>
    <link rel="stylesheet" type="text/css" href="css/stylesheet.css" media="screen" />
  </head>
  <body>
		<div class="rectangle" id="rec1">
			<div>1</div>
		</div>
		<div class="rectangle" id="rec2">
			<div>2</div>
		</div>
		<div class="rectangle" id="rec3">
			<div>3</div>
		</div>
		<div class="rectangle" id="rec4">
			<div>4</div>
		</div>
		<div class="rectangle" id="rec5">
			<div>5</div>
		</div>
		<div class="rectangle" id="rec6">
			<div>6</div>
		</div>
	<script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"></script>
	<script src="js/jquery.canvas.particles.js" type="text/javascript"></script>
	<script type="text/javascript">
		$(window).load(function(){
			$("#rec1").particles({
				amount:10,
				duration:{random:true},
				speed:{speed:1},
				layout:"after",
				color:{random:true}
			}).click(function(){
				$(this).particles("stop");
			});

			$("#rec2").particles({
				amount:2,
				end:"change",
				dir:{randomx:true,randomy:true},
				opacity:{decay:false,animation:false},
				duration:{duration:5000},
				speed:{speed:1,random:true},
				radius:{random:false,radius:10},
				color:{color:{r:200,g:0,b:200},random:true,min:{r:200,g:200,b:0}},
				bound:"bounce"
			}).click(function(e){
				var pos={x:e.pageX-$(this).offset().left,y:e.pageY-$(this).offset().top};
				$(this).particles("add",{
					position:pos,
					radius:{radius:3,random:true},
					duration:{duration:7000},
					speed:{speed:1}
				});
			});

			$("#rec3").particles({
				amount:10,
				radius:{radius:10,random:true},
				opacity:{decay:true,animation:true},
				duration:{duration:10000,random:true,firststep:-2000},
				speed:{speed:.5,random:true},
				end:"remove",
				position:{x:100,y:60},
				color:{color:{r:0,g:100,b:150},random:true,min:{r:0,g:50,b:100}}
			}).click(function(){
				var imgrandom=Math.floor(Math.random()*3);
				$(this).particles("add",{
					position:{random:true},
					color:{
						color:{r:123}
					},
					radius:{radius:10},
					duration:{duration:10000},
					speed:{speed:2},
					opacity:{animation:false,decay:false},
					image:imgrandom==0?"images/1.png":imgrandom==1?"images/2.png":"images/3.png",
					end:"change",bound:"bounce"
				});
			});	
			$("#rec4").particles({
				amount:20,
				radius:{radius:10,random:true},
				opacity:{decay:true,animation:true},
				duration:{duration:10000,random:true,firststep:-1000},
				speed:{speed:1.5,random:true},
				end:"remove",
				position:{x:0,y:0},
				color:{color:{r:0,g:0,b:150},random:true,min:{r:0,g:0,b:100}},
				dir:{
					x:1,
					y:1,
					xrand:false,
					yrand:true,
					rand:true
				}
			});

			$("#rec5").click(function(){
				var position={x:$(this).width(),y:$(this).height()};
				if(this.parts&&this.parts.part.length==0){
					$(this).particles("destroy");
				}
				if(!this.parts){
					$(this).particles({
						amount:10,
						radius:{radius:10,random:true},
						opacity:{decay:true,animation:true},
						duration:{duration:10000,random:false,firststep:-1000},
						speed:{speed:3.5,random:true,min:1},
						end:"remove",
						position:position,
						color:{color:{r:0,g:100,b:0}},
						dir:{
							x:-1,
							y:-1,
							xrand:false,
							yrand:false,
							rand:false,
							xfunction:function(dx,px,dy,py,s,w,h,step){
								return 175+50*Math.cos(2*Math.PI*(step*s)/600)
							},
							yfunction:function(dx,px,dy,py,s,w,h,step){
								return 100+50*Math.sin(2*Math.PI*(step*s)/600)
							}
						}
					});
				}
			});
			$("#rec6").click(function(){
				if(this.parts&&this.parts.part.length==0){
					$(this).particles("destroy");
				}
				if(!this.parts){
					$(this).particles({
						amount:0,
						radius:{radius:10,random:false},
						opacity:{decay:false,animation:false},
						duration:{duration:3000,random:false,firststep:0},
						speed:{speed:3,random:true,min:2},
						position:{x:0,y:0},
						image:"images/4.png",
						dir:{
							x:1,
							y:1,
							xrand:false,
							yrand:false,
							rand:false,
							xfunction:function(dx,px,dy,py,s,w,h,step){
								if(px<w){
									return px+=dx*s
								}
								else{
									return px==-200
								}
							},
							yfunction:function(dx,px,dy,py,s,w,h,step){
								if(py<h+100){
									return py+=dy*s
								}
								else{
									return py==-50
								}
							}
						},
						bound:false,
						layout:"after"
					});
					for(var i=0;i<50;i++){
						$(this).particles("add",{
							radius:{radius:Math.random()*10+2},
							speed:{speed:Math.random()*1+2},
							position:{x:Math.random()*$(this).width(),y:Math.random()-400}
						});
					}
				}
			});
		});
	</script>
  </body>
</html>

jquery.canvas.particles.js
$.fn.particles=function(method){
	var pi=2*Math.PI;
	var frames=1000/60;
	var defaults={
		amount:10,
		stop:false,
		end:"change",
		dir:{
			x:1,
			y:1,
			xrand:true,
			yrand:true,
			rand:true,
			xfunction:function(dx,px,dy,py,s,w,h){return px+=dx*s;},
			yfunction:function(dx,px,dy,py,s,w,h){return py+=(Math.sin(2*Math.PI*(px/w))*(dy*s))}
		},
		image:false,
		radius:{
			radius:5,
			random:false,
			min:3
		},
		duration:{
			duration:10000,
			random:false,
			min:1000,
			firststep:-2000
		},
		speed:{
			speed:1,
			random:false,
			min:.2
		},	
		opacity:{
			opacity:1,
			random:false,
			min:0,
			animation:true,
			decay:true
		},
		position:{
			x:0,
			y:0,
			random:false
		},
		color:{
			color:{r:255,g:255,b:255},
			random:false,
			min:{r:0,g:0,b:0}
		},
		layout:"before",
		bound:"back"//,bounce,false
	};
	var requestAnimFrame=(function(){
		return window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||window.oRequestAnimationFrame||window.msRequestAnimationFrame||
			function(callback){
				var id=window.setTimeout(callback,frames);
				return id;
			};
	})();
	var cancelAnimationFrame=(function(){
		return window.cancelAnimationFrame||window.webkitCancelAnimationFrame||window.mozCancelAnimationFrame||window.oCancelAnimationFrame||window.msCancelAnimationFrame||window.webkitCancelRequestAnimationFrame||window.mozCancelRequestAnimationFrame||window.oCancelRequestAnimationFrame||window.msCancelRequestAnimationFrame||
			function(id){
				clearTimeout(id);
			};
	})();
	var methods={
		init:function(opt){
			opt=$.extend(true,{},defaults,opt||{});
			return this.each(function(){
				var el=$(this);
				var o=$.extend({},opt);
				if(o.image){
					var src=o.image;
					o.image=new Image();
					o.image.src=src;
				}
				o.part=[];
				o.canvas={};
				o.canvas.width=el.width();
				o.canvas.height=el.height();
				var canvas=$("");
				if(o.layout=="before"){
					el.append(canvas);
				}
				else if(o.layout=="after"){
					el.prepend(canvas);
				}
				var ctx = canvas.get(0).getContext("2d");
				o.canvas.ctx=ctx;
				o.canvas.object=canvas;
				this.parts=o;
				for(var i=0;i=o.duration.duration){
						o.step=o.duration.firststep;
						if(o.end=="remove"){
							o.remove=true;
						}
						else if(o.end=="change"){
							randomdir();
							var rand=Math.random();
							o.duration.duration=o.duration.random?parseInt(Math.random()*e.max)+e.min:o.duration.duration;
							o.radius.radius=o.radius.random?(Math.random()*e.radius)+defaults.radius.min:o.radius.radius;
							o.speed.speed=o.speed.random?(Math.random()*e.speed)+defaults.speed.min:o.speed.speed;
							o.opacity.opacity=o.opacity.random?(Math.random()*e.opacity)+defaults.opacity.min:o.opacity.opacity;
							o.color.color=o.color.random?{
								r:parseInt(rand*(defaults.color.color.r-defaults.color.min.r)+defaults.color.min.r),
								g:parseInt(rand*(defaults.color.color.g-defaults.color.min.g)+defaults.color.min.g),
								b:parseInt(rand*(defaults.color.color.b-defaults.color.min.b)+defaults.color.min.b)
							}:o.color.color;
							e.half=o.duration.duration/2;
						}
					}
					else if(o.dir.rand&&o.step>0&&(o.step%60)==0&&Math.floor(Math.random()*2)==1){
						randomdir();
					}
					o.alpha=o.opacity.animation?o.opacity.decay?o.stepdefaults.canvas.width+o.radius.radius+2?-o.radius.radius:(o.position.x<-(o.radius.radius+2))?defaults.canvas.width+o.radius.radius:o.position.x;
						if(!o.image){
							o.position.y=o.position.y>defaults.canvas.height+o.radius.radius+2?-o.radius.radius:(o.position.y<-(o.radius.radius+2))?defaults.canvas.height+o.radius.radius:o.position.y;
						}
						else{
							o.position.y=o.position.y>defaults.canvas.height+o.radius.radius*o.image.height/o.image.width+2?-o.radius.radius*o.image.height/o.image.width:(o.position.y<-(o.radius.radius*o.image.height/o.image.width+2))?defaults.canvas.height+o.radius.radius*o.image.height/o.image.width:o.position.y;	
						}
					}
					else if(o.bound=="bounce"){
						if(o.position.x+o.radius.radius>=defaults.canvas.width){
							o.position.x+=-o.speed.speed;
							o.dir.x=-1;
						}
						else if(o.position.x<=o.radius.radius){
							o.position.x+=o.speed.speed;
							o.dir.x=1;
						}
						if(!o.image){
							if(o.position.y+o.radius.radius>=defaults.canvas.height){
								o.position.y+=-o.speed.speed;
								o.dir.y=-1;
							}
							else if(o.position.y<=o.radius.radius){
								o.position.y+=o.speed.speed;
								o.dir.y=1;
							}
						}
						else{
							if(o.position.y+o.radius.radius*o.image.height/o.image.width>=defaults.canvas.height){
								o.position.y+=-o.speed.speed;
								o.dir.y=-1;
							}
							else if(o.position.y<=o.radius.radius*o.image.height/o.image.width){
								o.position.y+=o.speed.speed;
								o.dir.y=1;
							}
						}
					}
					if(o.image){
						defaults.canvas.ctx.save();
						defaults.canvas.ctx.translate(o.position.x, o.position.y);
						defaults.canvas.ctx.globalAlpha=o.alpha;
						defaults.canvas.ctx.drawImage(o.image,-o.radius.radius,-o.radius.radius*o.image.height/o.image.width,o.radius.radius*2,(o.radius.radius*2)*o.image.height/o.image.width);
						defaults.canvas.ctx.restore();
					}
					else{
						defaults.canvas.ctx.beginPath();
						defaults.canvas.ctx.arc(o.position.x,o.position.y,o.radius.radius,0,pi, false);
						defaults.canvas.ctx.fillStyle = "rgba("+o.color.color.r+","+o.color.color.g+","+o.color.color.b+","+o.alpha+")";
						defaults.canvas.ctx.fill();
					}
					o.step++;
				}
				defaults.part.push(o);
			});
		},
		stop:function(){
			return this.each(function(){
				var el=this;
				if(el.parts.stop==false){
					el.parts.stop=true;
					cancelAnimationFrame(el.parts.requestid);
				}else{
					el.parts.stop=false;
					methods["step"].apply($(el));
				}
			});
		},
		step:function(){
			return this.each(function(){
				var el=this;
				if(el.parts&&el.parts.stop==false){
					var rarray=[];
					el.parts.canvas.ctx.clearRect(0,0,el.parts.canvas.width,el.parts.canvas.height);
					$.each(el.parts.part,function(i,n){
						n.update();
						if(!n.remove){
							rarray.push(n);
						}
					});
					el.parts.part=rarray;
					el.parts.requestid=requestAnimFrame(function(){
					  methods["step"].apply($(el));
					});
				}
			});
		},
		destroy:function(){
			return this.each(function(){
				var el=this;
				methods["stop"].apply($(el));
				el.parts.canvas.object.remove();
				delete el.parts;
			});
		}
	};
    if(methods[method]){
		return methods[method].apply(this,Array.prototype.slice.call(arguments,1));
    }else if(typeof method==='object'||!method){
		return methods.init.apply(this,arguments);
    }else{
		$.error('Method '+method+' does not exist on jQuery.particles');
    }
};

stylesheet.css
.rectangle{
	width:350px;
	height:200px;
	margin:10px;
	float:left;
	position:relative;
	display:block;
	background:#f0f0f0;
	border:1px solid #777777;
	overflow:hidden;
}
.rectangle div{
	font-size:50px;
	color:#555555;
	position:relative;
	top:75px;
	text-align:center;
}
.particles{
	position:absolute;
	top:0px;
	left:0px;
}
Ver más

jQuery img box

lightweight jQuery plugin to display images and html (lightbox alternative) includes:
  • custom animation effects
  • dynamic size
  • supports links to image and html files

Example:


Default:
image
error
image with title
Caption, fixed size and no close button:
image
error
imagen with title
Custom animation:
image
error
imagen with title
Custom animation 2:
image
error
imagen with title
div default

div animation

div html

HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script src="box.js" type="text/javascript"></script>
<link rel="stylesheet" href="style.css">
</head>
<body>
	Defaul:<br>
	<a href="1.jpg" class="default">
		image
	</a><br>
	<a href="error.jpg" class="default">
		error
	</a><br>
	<a href="1.jpg" title="some title" class="default">
		image with title
	</a><br>
	
	Caption, fixed size and no close button:<br>
	<a href="1.jpg" class="caption">
		image
	</a><br>
	<a href="error.jpg" class="caption">
		error
	</a><br>
	<a href="1.jpg" title="some title" class="caption">
		imagen with title
	</a><br>
	
	Custom animation:<br>
	<a href="1.jpg" class="customAnimation">
		image
	</a><br>
	<a href="error.jpg" class="customAnimation">
		error
	</a><br>
	<a href="1.jpg" title="some title" class="customAnimation">
		imagen with title
	</a><br>
	
	Custom animation 2:<br>
	<a href="1.jpg" class="customAnimation2">
		image
	</a><br>
	<a href="error.jpg" class="customAnimation2">
		error
	</a><br>
	<a href="1.jpg" title="some title" class="customAnimation2">
		imagen with title
	</a><br>
	
	<div class="div1">
		div default
	</div><br>
	<div class="div2">
		div animation
	</div><br>
	<div class="div3">
		div html
	</div>
<script>
$(document).ready(function(){
	$(".default").click(function(e){
		e.preventDefault();
		$(this).box();
	});
	$(".caption").click(function(e){
		e.preventDefault();
		$(this).box({
			width:500,
			height:300,
			closebutton:false,
			caption:true
		});
	});
	$(".customAnimation").click(function(e){
		e.preventDefault();
		$(this).box({
			width:500,
			height:300,
			closebutton:true,
			caption:true,
			onStart:function(e){
				var defered=new $.Deferred();
				$(e.container).css({"display":"none"});
				$(e.overlay).css("opacity","0").animate({"opacity":"1"},300,function(){defered.resolve();});
				return defered.promise();
			},
			onLoad:function(e){
				$(e.container).css({"display":""});
				$(e.image).css({"width":"0px"}).animate({"width":"100%"},2000);
			},
			onClose:function(e){
				var defered=new $.Deferred();
				if(e.image){
					$(e.image).animate({"width":"0px"},2000,function(){
						$(e.overlay).stop().animate({"opacity":"0"},1000,function(){defered.resolve();});
					});
				}
				else{
					$(e.overlay).stop().animate({"opacity":"0"},1000,function(){defered.resolve();});
				}
				return defered.promise();
			}
		});
	});
	$(".customAnimation2").click(function(e){
		e.preventDefault();
		var ele=$(this);
		var x=ele.offset().left-$(window).scrollLeft(),y=ele.offset().top-$(window).scrollTop(),w=ele.width(),h=ele.height();
		ele.box({
			closebutton:true,
			caption:true,
			onStart:function(e){
				var defered=new $.Deferred();
				$(e.container).css({"opacity":"0"});
				$(e.overlay).css("opacity","0").animate({"opacity":"1"},1000,function(){defered.resolve();});
				return defered.promise();
			},
			onLoad:function(e){
				$(e.closebutton).css({"opacity":"0"});
				var x1=$(e.container).position().left,y1=$(e.container).position().top,w1=$(e.container).width(),h1=$(e.container).height();
				$(e.container).css({"opacity":"1","position":"absolute","left":x,"top":y,"width":w,"height":h})
								.animate({"left":x1,"top":y1,"width":w1,"height":h1},2000,function(){
									$(this).css({"position":"relative","left":"0"});
									$(e.closebutton).animate({"opacity":"1"},1000);
								});
			},
			onClose:function(e){
				var defered=new $.Deferred();
				if($(e.container).find(".close").is("span")){
					$(e.closebutton).animate({"opacity":"0"},1000);
				}
				if(e.image){
					$(e.image).animate({"width":"0px"},2000,function(){
						$(e.overlay).stop().animate({"opacity":"0"},1000,function(){defered.resolve();});
					});
				}
				else{
					$(e.overlay).stop().animate({"opacity":"0"},1000,function(){defered.resolve();});
				}
				return defered.promise();
			},
			onError:function(e){
				$(e.container).html("custom error<a href='#'>link</a>").css({"color":"#aa0000","display":"","opacity":"1"});
			}
		});
	});
	$(".div1").click(function(){
		$(this).box({
			ref:"1.jpg",
			title:"some title",
			caption:true
		});
	});
	$(".div2").click(function(){
		var ele=$(this);
		ele.box({
			ref:"1.jpg",
			title:"some title",
			caption:false,
			onStart:function(e){
				var defered=new $.Deferred();
				$(e.container).css({"opacity":"0"});
				$(e.overlay).css({"top":-$(e.overlay).height()}).animate({"top":$(window).scrollTop()},1000,function(){defered.resolve();});
				return defered.promise();
			},
			onLoad:function(e){
				$(e.closebutton).css({"opacity":"0"});
				$(e.title).css({"opacity":"0"});
				var x1=$(e.container).position().left,y1=$(e.container).position().top,w1=$(e.container).width(),h1=$(e.container).height();
				$(e.container).css({"opacity":"1","position":"absolute","top":-h1,"left":x1})
								.animate({"top":y1},2000,function(){
									$(this).css({"position":"relative","left":"0"});
									$(e.closebutton).animate({"opacity":"1"},1000);
									$(e.title).animate({"opacity":"1"},1000);
								});
			},
			onError:function(e){
				$(e.container).html("Error").addClass("error");
				$(e.container).css({"display":""});
				$(e.container).css({"opacity":"1"});
				var msgh=$(e.container).height()/2;
				$(e.container).css({"top":function(){return ($(window).height()/2)-msgh}});
				if(e.closebutton){
					var close=$("<span class='close'></span>");
					$(e.container).append(close);
					e.closebutton=close;
					close.click(function(){
						$(e.overlay).remove();
					});
				}
				$(window).resize(function(){
					$(e.container).css({"top":function(){return ($(window).height()/2)-msgh}});
				});
			},
			onClose:function(e){
				var defered=new $.Deferred();
				if(e.closebutton){
					$(e.closebutton).animate({"opacity":"0"},1000);
				}
				if(e.title){
					$(e.title).animate({"opacity":"0"},1000);
				}
				if(e.image){
					$(e.container).animate({"top":$(e.overlay).height()},2000,function(){
						$(e.overlay).stop().animate({"top":$(e.overlay).height()+$(window).scrollTop()},1000,function(){defered.resolve();});
					});
				}
				else{
					$(e.overlay).stop().animate({"opacity":"0"},1000,function(){defered.resolve();});
				}
				return defered.promise();
			}
		});
	});
	$(".div3").click(function(){
		$(this).box({
			ref:"1.html",
			title:"some title",
			caption:true,
			type:"html",
			onStart:function(e){
				var defered=new $.Deferred();
				$(e.container).css({"width":"70%","height":"100%","background":"#ffffff","color":"#000000","opacity":"0","position":"relative","word-break": "break-all","text-align":"center"});
				$(e.overlay).css("opacity","0").animate({"opacity":"1"},3000,function(){defered.resolve();});
				return defered.promise();
			},
			onLoad:function(e){
				$(e.container).animate({"opacity":"1"},3000);
				$(e.title).removeClass("caption").css({"position":"absolute","bottom":"0px","left":"0px"});
			}
		});
	});
});
</script>
</body>
</html>

box.js
$.fn.box=function(method){
	var defaults={
		width:0,
		height:0,
		ref:false,
		title:false,
		closebutton:true,
		type:"img",
		caption:false,
		onStart:false,
		onLoad:false,
		onClose:false,
		onError:false
	}
	var methods={
		init:function(opt){
			opt=$.extend({},defaults,opt||{});
			return this.each(function(){
				var el=this;
				opt.ref=$(el).attr("href")||opt.ref;
				opt.title=$(el).attr("title")||opt.title;
				var win=$(window),loading=$("<div class='loading'>"),overlay=$("<div class='box'>"),image=$(Image()),container=$("<div class='boxcontent'>"),close=$("<span class='close'>"),title=$("<p class='caption'>"+opt.title+"</p>");
				el.box=opt;
				if($("body").css("overflow")==""){
					if($("body").css("overflow-x")!=""){
						el.overflowx=$("body").css("overflow-x");
					}
					if($("body").css("overflow-y")!=""){
						el.overflowy=$("body").css("overflow-y");
					}
				}
				else{
					el.overflow=$("body").css("overflow");
				}
				$("body").append(overlay).css({"overflow":"hidden"});
				overlay.css({"top":win.scrollTop(),"left":win.scrollLeft()});
				overlay.click(function(){
					$(el).box("close");
				});
				el.box.overlay=overlay;
				overlay.append(loading);
				loading.css({"display":"block","position":"absolute","left":(win.width()/2)-(loading.width()/2),"top":(win.height()/2)-(loading.height()/2)});
				loading.click(function(e){
					e.stopPropagation();
				});
				win.resize(function(){
					loading.css({"left":(win.width()/2)-(loading.width()/2),"top":(win.height()/2)-(loading.height()/2)});
				});
				el.box.loading=loading;
				overlay.append(container);
				container.click(function(e){
					e.stopPropagation();
				});
				el.box.container=container;
				if($.isFunction(opt.onStart)){
					var defered=opt.onStart(el.box);
				}
				if(opt.type=="img"){
					image.load(function(){
						loading.fadeOut("fast",function(){$(this).remove();});
						container.append(image); 
						el.box.image=image;
						if(opt.closebutton){
							container.append(close);
							el.box.closebutton=close;
							close.click(function(){
								$(el).box("close");
							});
						}
						if(opt.caption&&opt.title){
							container.append(title);
							el.box.title=title;
						}
						var mheight=win.height()/2;
						if(el.box.width==0&&el.box.height==0){
							el.box.width=this.width;
							el.box.height=this.height;
							if(win.width()win.height()){
									container.css({"height":"90%","top":function(){return mheight-(container.height()/2)}});
									image.css({"height":"100%","width":"auto"});
									container.css({"width":image.width()});
								}
							}
							else if(win.height()win.width()){
									container.css({"width":"90%","height":"auto"});
									image.css({"width":"100%","height":"auto"});
									container.css({"top":function(){return mheight-(container.height()/2)}});
								}
							}
							else{
								container.css({"width":el.box.width,"height":el.box.height,"top":function(){return mheight-(el.box.height/2)}});
								image.css({"width":"100%","height":"100%"});

							}
							win.resize(function(){
								mheight=win.height()/2;
								if(win.width()win.height()){
										container.css({"height":"90%","top":function(){return mheight-(container.height()/2)}});
										image.css({"height":"100%","width":"auto"});
										container.css({"width":image.width()});
									}
								}
								else if(win.height()win.width()){
										container.css({"width":"90%","height":"auto"});
										image.css({"width":"100%","height":"auto"});
										container.css({"top":function(){return mheight-(container.height()/2)}});
									}
								}
								else{
									container.css({"width":el.box.width,"height":"auto","top":function(){return mheight-(el.box.height/2)}});
								}
							});
						}
						else{
							image.css({"width":function(){if(el.box.width!=0)return el.box.width; else return "auto"},"height":function(){if(el.box.height!=0)return el.box.height; else return "auto"}});
							container.css({"width":image.width(),"height":image.height()});
							overlay.css({"overflow":"auto"});
							if(container.height()>win.height()){
								container.css({"top":"0px"});
							}
							else{
								container.css({"top":function(){return mheight-(container.height()/2)}});
							}
						}
						if(defered&&defered.promise){
							defered.done(function(){
								if($.isFunction(opt.onLoad)){
									opt.onLoad(el.box);
								}
							});
						}
						else{
							if($.isFunction(opt.onLoad)){
								opt.onLoad(el.box);
							}
						}
						
					}).attr({
						src: opt.ref
					}).error(function(){
						loading.fadeOut("fast",function(){$(this).remove();});
						if($.isFunction(el.box.onError)){
							el.box.onError(el.box);
						}
						else{
							container.html("Error").addClass("error");
							container.css({"display":""});
							var msgh=$(this).height()/2;
							container.css({"top":function(){return (win.height()/2)-msgh}});
							if(opt.closebutton){
								container.append(close);
								el.box.closebutton=close;
								close.click(function(){
									$(el).box("close");
								});
							}
							win.resize(function(){
								container.css({"top":function(){return (win.height()/2)-msgh}});
							});
						}
					});
				}
				else if(opt.type="html"){
					if(opt.closebutton){
						container.append(close);
						el.box.closebutton=close;
						close.click(function(){
							$(el).box("close");
						});
					}
					var wrap=$("<span>");
					container.append(wrap);
					wrap.load(opt.ref, function(response, status, xhr) {
						loading.fadeOut("fast",function(){$(this).remove();});
						if (status == "error") {							
							var msg = "Sorry but there was an error: ";
							wrap.html(msg + xhr.status + " " + xhr.statusText);
						}
						if(defered&&defered.promise){
							defered.done(function(){
								if($.isFunction(opt.onLoad)){
									opt.onLoad(el.box);
								}
							});
						}
						else{
							if($.isFunction(opt.onLoad)){
								opt.onLoad(el.box);
							}
						}
					});
					if(opt.caption&&opt.title){
						container.append(title);
						el.box.title=title;
					}
				}
			});
		},
		close:function(){
			return this.each(function(){
				var el=this;
				if($.isFunction(el.box.onClose)){
					var defered=el.box.onClose(el.box);
					if(defered&&defered.promise){
						defered.done(function(){
							if(el.overflow){
								$("body").css({"overflow":el.overflow});
							}
							else{
								$("body").css({"overflow-x":el.overflowx,"overflow-y":el.overflowy});
							}
							el.box.overlay.remove();
						});
					}
					else{
						$("body").css({"overflow":"auto"});
						el.box.overlay.remove();
					}
				}
				else{
					if(el.overflow){
						$("body").css({"overflow":el.overflow});
					}
					else{
						$("body").css({"overflow-x":el.overflowx,"overflow-y":el.overflowy});
					}
					el.box.overlay.remove();
				}
			});
		}
	};
	if(methods[method]){
		return methods[method].apply(this,Array.prototype.slice.call(arguments,1));
	}else if(typeof method==='object'||!method){
		return methods.init.apply(this,arguments);
	}else{
		$.error('Method '+method+' does not exist on jQuery.box');
	}
};

style.css
.div1{
	background: none repeat scroll 0 0 #0055F0;
	border: 3px solid #0022FF;
	border-radius: 5px 5px 5px 5px;
	color: #FFFFFF;
	display: block;
	font-weight: bolder;
	padding: 15px;
	text-align: center;
	width: 100px;
}
.div2{
	background: none repeat scroll 0 0 #0055F0;
	border: 3px solid #0022FF;
	border-radius: 5px 5px 5px 5px;
	color: #FFFFFF;
	display: block;
	font-weight: bolder;
	padding: 15px;
	text-align: center;
	width: 100px;
}
.div3{
        width:100px;
	padding:15px;
	background:#aa0000;
	display:block;
	border:3px solid #550000;
	border-radius:5px;
	text-align:center;
	color:#ffffff;
	font-weight:bolder;
}
.box{
	background: url("bg.png");
	display: block;
	height: 100%;
	left: 0;
	position: absolute;
	top: 0;
	width: 100%;
	padding:0;
	-moz-user-select: none;
	-khtml-user-select: none;
	-webkit-user-select: none;
	user-select: none;
}
.box .caption{
	position:relative;
	top:-15px;
}
.box .boxcontent{
	position:relative;
	margin:0 auto;
	color:#ffffff;
	font-weight:bolder;
	font-size:22px;
	text-align: center;
	text-shadow: 1px 1px 2px #000000;
	-moz-user-select: text;
	-khtml-user-select: text;
	-webkit-user-select: text;
	user-select: text;
}
.box .loading{
	position:relative;
	margin:0 auto;
	background:url("loader.gif");
	width:16px;
	height:16px;
	display:block;
}
.box .boxcontent img{
	-moz-box-shadow: 1px 7px 7px rgba(0, 0, 0, 0.7);
	-webkit-box-shadow: 1px 7px 7px rgba(0, 0, 0, 0.7);
	box-shadow: 1px 7px 7px rgba(0, 0, 0, 0.7);
}
.box .error{
	background:#ffffff;
	color:#000000;
	padding:10px;
	width:200px;
	text-shadow:0px 0px 0px;
	-moz-box-shadow: 1px 7px 7px rgba(0, 0, 0, 0.7);
	-webkit-box-shadow: 1px 7px 7px rgba(0, 0, 0, 0.7);
	box-shadow: 1px 7px 7px rgba(0, 0, 0, 0.7);
}
.box .boxcontent .close{
	position:absolute;
	right:-10px;
	top:-10px;
	width:20px;
	height:20px;
	background:url("close.png");
	cursor:pointer;
	-moz-box-shadow: 1px 2px 7px rgba(0, 0, 0, 0.7);
	-webkit-box-shadow: 1px 2px 7px rgba(0, 0, 0, 0.7);
	box-shadow: 1px 2px 7px rgba(0, 0, 0, 0.7);
}
Ver más

jQuery rotate element

Animate element to rotate clockwise and anticlockwise with jquery.animate
Random animation and callback
Example:




Rotate.js
$.fx.step['degree']=function(fx){
    if(! fx.deg){
        fx.deg=0;
        if($(fx.elem).data("degree")){
            fx.deg=$(fx.elem).data("degree")
        }   
    }
    $(fx.elem).css({"WebkitTransform":"rotate("+Math.floor(fx.deg+(fx.end*fx.pos))+"deg)","-moz-transform":"rotate("+Math.floor(fx.deg+(fx.end*fx.pos))+"deg)","-ms-transform":"rotate("+Math.floor(fx.deg+(fx.end*fx.pos))+"deg)"});
    $(fx.elem).data("degree",Math.floor(fx.deg+(fx.end*fx.pos)));
}

HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" type="text/css" href="style.css" />
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script src="rotate.js" type="text/javascript"></script>
<script>
$(document).ready(function(){
      rotate(Math.floor(Math.random()*720)-360);
      function rotate(r){
           var random=Math.floor(Math.random()*720)-360;
          $("#wheel").animate({"degree":r},1000,function(){rotate(random)});
      }
});
</script>
</head>
<body>

style.css
#wheel{
    width:300px;
    height:300px;
    display:block;
    background:url("gear.png");
}
Ver más

Background gradient animate IE support

Animate element background-image linear gradient like:
$(element).animate({"gradient":"linear-gradient(rgb(r,g,b) 0%, rgb(r,g,b) 100%)"});
Requirements:
jquery-cssHooks to use linear-gradient() without setting specific css rules for every browser
Add some lines to apply "progid:DXImageTransform.Microsoft.gradient" filter
Example:
click to change gradient

gradients.js (ie7+ linear gradient)
/*! 
* Copyright (c) 2011 Tom Ellis (http://www.webmuse.co.uk)
* Linear and Radial Gradient cssHook for jQuery
* Limitations:
  - Works with jQuery 1.4.3 and higher
  - Works in Firefox 3.6+, Safari 5.1+, Chrome 13+, Opera 11.10+, IE9+ 
  - Radial Gradients DO NOT work in Opera yet (Doesn't make sense I Know!)
  - Only works for background and background image CSS properties
* Licensed under the MIT License (LICENSE.txt).
*/
function rgbToHex(r, g, b) {
    return "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
}
(function($) {

 var div = document.createElement( "div" ),
  divStyle = div.style,
  rLinear = /^(.*?)linear-gradient(.*?)$/i,
  rRadial = /^(.*?)radial-gradient(.*?)$/i,
  rLinearSettings = /^(.*?)(:?linear-gradient)(\()(.*)(\))(.*?)$/i,  
  rRadialSettings = /^(.*?)(:?radial-gradient)(\()(.*?)(\))(.*?)$/i,
  rSupportLinearW3C = /(^|\s)linear-gradient/, 
  rSupportLinearMoz = /(^|\s)-moz-linear-gradient/, 
  rSupportLinearWebkit = /(^|\s)-webkit-linear-gradient/, 
  rSupportLinearOpera = /(^|\s)-o-linear-gradient/, 
  rSupportRadialW3C = /(^|\s)radial-gradient/, 
  rSupportRadialMoz = /(^|\s)-moz-radial-gradient/, 
  rSupportRadialWebkit = /(^|\s)-webkit-radial-gradient/, 
  rSupportRadialOpera = /(^|\s)-o-radial-gradient/,      
  rWhitespace = /\s/,
  rWhiteGlobal = /\s/g,
  cssProps = "background backgroundImage", //listStyleImage not supported yet
  cssLinear = "filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#000000', endColorstr='#aaaaaa',GradientType=0 );background-image: -moz-linear-gradient(red, blue);background-image: -webkit-linear-gradient(red, blue);background-image: -o-linear-gradient(red, blue);background-image: linear-gradient(red, blue);",
  cssRadial = "background-image: -moz-radial-gradient(circle, orange, red);background-image: -webkit-radial-gradient(circle, orange, red);background-image: -o-radial-gradient(circle,red, blue);background-image: radial-gradient(circle, orange, red);",
  cssPropsArray = cssProps.split( rWhitespace );
  divStyle.cssText = cssLinear,
  linearSettings = function ( value ) {
   var parts = rLinearSettings.exec( value );
   value = value.replace( new RegExp(parts[2], 'g') , $.support.linearGradient );
   return value;
  },
  radialSettings = function ( value ) {
   var parts = rRadialSettings.exec( value );   
   value = value.replace( new RegExp(parts[2], 'g') , $.support.radialGradient );
   return value;
  };

  $.support.linearGradient =
   rSupportLinearW3C.test( divStyle.backgroundImage ) ? "linear-gradient" :
   (rSupportLinearMoz.test( divStyle.backgroundImage ) ? "-moz-linear-gradient" :
   (rSupportLinearWebkit.test( divStyle.backgroundImage ) ? "-webkit-linear-gradient" :
   (rSupportLinearOpera.test( divStyle.backgroundImage ) ? "-o-linear-gradient" :
   false)));
  if(/(^|\s)progid:DXImageTransform.Microsoft.gradient/.test(divStyle.filter)){
   $.support.ie=true;
  }
  
  divStyle.cssText = cssRadial;

  $.support.radialGradient =
   rSupportRadialW3C.test( divStyle.backgroundImage ) ? "radial-gradient" :
   (rSupportRadialMoz.test( divStyle.backgroundImage ) ? "-moz-radial-gradient" :
   (rSupportRadialWebkit.test( divStyle.backgroundImage ) ? "-webkit-radial-gradient" :
   (rSupportRadialOpera.test( divStyle.backgroundImage ) ? "-o-radial-gradient" :
   false)));

     if ( $.support.linearGradient && $.support.linearGradient !== "linear-gradient" ) {

   $.each( cssPropsArray, function( i, prop ) {

    $.cssHooks[ prop ] = {

     set: function( elem, value ) {

      if( rLinear.test( value ) ){
       elem.style[ prop ] = linearSettings( value );
      } else if ( rRadial.test( value ) ) {
       elem.style[ prop ] = radialSettings( value );
      } else {
       elem.style[ prop ] = value;
      }
     }
    };

   });

     }
  else if($.support.ie){
   $.cssHooks[ "backgroundImage" ] = {
     set: function( elem, value ) {
      var startcolor=$.grep(value.replace(/(progid:DXImageTransform.Microsoft.gradient|startColorstr|endColorstr|GradientType|-o|-webkit|-moz|linear|radial|from|\bto\b|gradient|top|left|bottom|right|\d*%)/g,'').match(/(rgb\([^\)]+\)|#[a-z\d]*|[a-z]*)/g),function (s) { return $.trim( s )});
      var c1=$.map(startcolor[0].replace(/(rgb\(|\))/g,'').split(","),function(n){return parseInt(n);});
      var c2=$.map(startcolor[1].replace(/(rgb\(|\))/g,'').split(","),function(n){return parseInt(n);});
      var c3=rgbToHex(c1[0],c1[1],c1[2]);
      var c4=rgbToHex(c2[0],c2[1],c2[2]);
      var filter="progid:DXImageTransform.Microsoft.gradient( startColorstr='"+c3+"', endColorstr='"+c4+"',GradientType=0 )";
      elem.style[ "filter" ] =filter;
     }
    };   
  }
  div = divStyle = null; 
})(jQuery);
HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" type="text/css" href="style.css" />
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script src="gradients.js" type="text/javascript"></script>
<script src="animate-gradientsie.js" type="text/javascript"></script>
<script>
$(document).ready(function(){
 $("#element").click(function(){
  $(this).animate({"gradient":"linear-gradient(rgb("+Math.floor(Math.random() * 255)+","+Math.floor(Math.random() * 255)+","+Math.floor(Math.random() * 255)+") 0%, rgb("+Math.floor(Math.random() * 255)+","+Math.floor(Math.random() * 255)+","+Math.floor(Math.random() * 255)+") 100%)"},3000);
 });
});
</script>
</head>
<body>
    <div id="element">
  gradient
 </div>
</body>
</html>

style.css
#element{
width:500px;
height:600px;
display:block;
position:absolute;
/* IE10 Consumer Preview */ 
background-image: -ms-linear-gradient(top, #000000 0%, #aaaaaa 100%);
/* Mozilla Firefox */ 
background-image: -moz-linear-gradient(top, #000000 0%, #aaaaaa 100%);
/* Opera */ 
background-image: -o-linear-gradient(top, #000000 0%, #aaaaaa 100%);
/* Webkit (Safari/Chrome 10) */ 
background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0, #000000), color-stop(1, #aaaaaa));
/* Webkit (Chrome 11+) */ 
background-image: -webkit-linear-gradient(top, #000000 0%, #aaaaaa 100%);
/* W3C Markup, IE10 Release Preview */ 
background-image: linear-gradient(to bottom, #000000 0%, #aaaaaa 100%);
/* ie */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#000000', endColorstr='#aaaaaa',GradientType=0 );
}

animate-gradientsie.js (if filter exists convert hex color to rgb)
function hexToRgb(hex) {
    var shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
    hex = hex.replace(shorthandRegex, function(m, r, g, b) {
        return r + r + g + g + b + b;
    });
    var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
 return result ? parseInt(result[1], 16)+","+parseInt(result[2], 16)+","+parseInt(result[3], 16) :null;
}
$.fx.step['gradient']=function(fx){
  if(!fx.start){
   var css=$(fx.elem).css("filter")!="none"?$(fx.elem).css("filter"):$(fx.elem).css("background-image");
   var startcolor=$.grep(css.replace(/(progid:DXImageTransform.Microsoft.gradient|startColorstr|endColorstr|GradientType|-o|-webkit|-moz|linear|radial|from|\bto\b|gradient|top|left|bottom|right|\d*%)/g,'').match(/(rgb\([^\)]+\)|#[a-z\d]*|[a-z]*)/g),function (s) { return $.trim( s )});
   var endcolor=$.grep(fx.end.replace(/(-o|-webkit|-moz|linear|radial|from|\bto\b|gradient|top|left|bottom|right|\d*%)/g,'').match(/(rgb\([^\)]+\)|#[a-z\d]*|[a-z]*)/g),function (s) { return $.trim( s )});
   if(/^(.*?)progid:DXImageTransform(.*?)$/i.test(css)){
    startcolor[0]=hexToRgb(startcolor[0]);
    startcolor[1]=hexToRgb(startcolor[1]);
   }
   fx.start={top:$.map(startcolor[0].replace(/(rgb\(|\))/g,'').split(","),function(n){return parseInt(n);}),bottom:$.map(startcolor[1].replace(/(rgb\(|\))/g,'').split(","),function(n){return parseInt(n);})};
   fx.endgradient={top:$.map(endcolor[0].replace(/(rgb\(|\))/g,'').split(","),function(n){return parseInt(n);}),bottom:$.map(endcolor[1].replace(/(rgb\(|\))/g,'').split(","),function(n){return parseInt(n);})};
   fx._gradient={top:$.map(fx.start.top,function(n,i){return -(n-fx.endgradient.top[i])}),bottom:$.map(fx.start.bottom,function(n,i){return -(n-fx.endgradient.bottom[i])})};
  }
  var rgb1=$.map(fx._gradient.top,function(n,i){return fx.start.top[i]+Math.floor(n*fx.pos)});
  var rgb2=$.map(fx._gradient.bottom,function(n,i){return fx.start.bottom[i]+Math.floor(n*fx.pos)});
  $(fx.elem).css({"background-image":"linear-gradient(rgb("+rgb1+") 0%, rgb("+rgb2+") 100%)"});
}
Ver más

Background gradient animate

Animate element background-image linear gradient like:
$(element).animate({"gradient":{top:[r,g,b],bottom:[r,g,b]}});
Requirements:
jquery-cssHooks to use linear-gradient() without setting specific css rules for every browser
Example:
click to change gradient

HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" type="text/css" href="style.css" />
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script src="gradients.js" type="text/javascript"></script>
<script src="animate-gradient.js" type="text/javascript"></script>
<script>
$(document).ready(function(){
 $("#element").click(function(){
  $(this).animate({"gradient":{top:[Math.floor(Math.random() * 255),Math.floor(Math.random() * 255),Math.floor(Math.random() * 255)],bottom:[Math.floor(Math.random() * 255),Math.floor(Math.random() * 255),Math.floor(Math.random() * 255)]}},3000);
 });
});
</script>
</head>
<body>
    <div id="element">
  gradient
 </div>
</body>
</html>

animate-gradients.js
$.fx.step['gradient']=function(fx){
  if(!fx.start){
   var g=fx.end,gradient=$(fx.elem).css("background-image").replace(/(-o|-webkit|-moz|linear|radial|from|\bto\b|gradient|top|left|bottom|right|\d*%)/g,'');
   var color=$.grep(gradient.match(/(rgb\([^\)]+\)|#[a-z\d]*|[a-z]*)/g),function (s) { return $.trim( s )});
   fx.start={top:$.map(color[0].replace(/(rgb\(|\))/g,'').split(","),function(n){return parseInt(n);}),bottom:$.map(color[1].replace(/(rgb\(|\))/g,'').split(","),function(n){return parseInt(n);})};
   fx._gradient={top:$.map(g.top,function(n,i){return n-fx.start.top[i]}),bottom:$.map(g.bottom,function(n,i){return n-fx.start.bottom[i]})};
  }
  var rgb1=$.map(fx._gradient.top,function(n,i){return Math.floor(n*fx.pos)+fx.start.top[i]});
  var rgb2=$.map(fx._gradient.bottom,function(n,i){return Math.floor(n*fx.pos)+fx.start.bottom[i]});
  $(fx.elem).css({"background-image":"linear-gradient(rgb("+rgb1+") 0%, rgb("+rgb2+") 100%)"});
}

style.css
#backgroundanim{
 /* IE10 Consumer Preview */ 
 background-image: -ms-linear-gradient(top, #000000 0%, #aaaaaa 100%);
 /* Mozilla Firefox */ 
 background-image: -moz-linear-gradient(top, #000000 0%, #aaaaaa 100%);
 /* Opera */ 
 background-image: -o-linear-gradient(top, #000000 0%, #aaaaaa 100%);
 /* Webkit (Safari/Chrome 10) */ 
 background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0, #000000), color-stop(1, #aaaaaa));
 /* Webkit (Chrome 11+) */ 
 background-image: -webkit-linear-gradient(top, #000000 0%, #aaaaaa 100%);
 /* W3C Markup, IE10 Release Preview */ 
 background-image: linear-gradient(to bottom, #000000 0%, #aaaaaa 100%);
 width:500px;
 height:600px;
 display:block;
 position:absolute;
}
Ver más