/
jquery.scratchie.min.js
13 lines (13 loc) · 2.78 KB
/
jquery.scratchie.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* Scratchie Plugin
*
* @version 1.2 (02/06/2010)
* @requires jQuery v1.3.2
* @author Alex Weber <alexweber.com.br>
* @copyright Copyright (c) 2010, Alex Weber
* @license http://www.opensource.org/licenses/bsd-license.php
* @see http://www.alexweber.com.br/jquery/scratchie
*
* Distributed under the terms of the new BSD License
*/
(function(a){a.fn.scratchie=function(l){var d={cursor:"coin.gif",cursorHeight:20,cursorWidth:20,target:"target",img:"prize.jpg",imgHeight:200,imgWidth:400,title:"Are you feeling lucky today?",fillColor:"#cc0000",fillImg:"overlay.jpg",fillX:20,fillY:20,completion:70,uncoverOnComplete:true,requireMouseClick:true,callback:function(){alert("Congratulations!")}};if(l){if(typeof l=="object"){a.extend(d,l)}else{d.target=l}}var c=false;var g=false;var h=overlaysUncovered=0;var b=function(){a().one("mouseup",function(){c=false;a().unbind();a(".scratch_overlay").bind("mousedown",b)});return false};var k=function(m){a("#cursor").css({left:m.clientX-2,top:m.clientY+2})};var f=function(){a("#cursor").show();return false};var e=function(){a("#cursor").hide();return false};this.each(function(){var p,q,r,o,n,m;p=a(this);q=a("#"+d.target);p.css({cursor:"url(blank.cur), none",height:d.imgHeight+"px",width:d.imgWidth+"px"});q.attr({alt:d.title,title:d.title,zIndex:1}).css({height:d.imgHeight+"px",width:d.imgWidth+"px"}).bind("mousedown",function(){return false});r=q.position();tt=r.top;tl=r.left;n=m=0;p.after('<div id="cursor" style="cursor:none;width:'+d.cursorWidth+"px;height:"+d.cursorHeight+"px;position:fixed;display:none;top:0;left:0;z-index:10000;background:url("+d.cursor+') top left no-repeat;"></div>');o="";for(i=0;i<d.imgWidth;i+=d.fillX){for(j=0;j<d.imgHeight;j+=d.fillY){++h;o+='<div class="scratch_overlay" style="z-index:100;height:'+d.fillY+"px;width:"+d.fillX+"px;position:absolute;border:0;overflow:hidden;top:"+(tt+j)+"px;left:"+(tl+i)+"px;background:";if(d.fillImg){o+=" transparent url("+d.fillImg+") -"+n+"px -"+m+"px no-repeat;";m+=d.fillY}else{o+=d.fillColor}o+='"/>'}if(d.fillImg){m=0;n+=d.fillX}}p.after(o);q.attr("src",d.img);a(".scratch_overlay").css("cursor","url(blank.cur), none");q.bind("mouseout",function(){e()}).bind("mouseenter",function(){f()}).bind("mousemove",function(s){k(s)});a(".scratch_overlay").bind("mouseout",function(){e()}).bind("mouseenter",function(){f()}).bind("mousemove",function(s){k(s)});if(d.requireMouseClick===true){a(".scratch_overlay").bind("mousedown",function(){c=true;b();if(g===true){++overlaysUncovered;a(this).remove()}return false})}a(".scratch_overlay").bind("mouseover",function(){if(c===true||d.requireMouseClick===false){if(g===true){++overlaysUncovered;a(this).remove()}if((overlaysUncovered/h)*100>=d.completion){if(d.uncoverOnComplete===true){a(".scratch_overlay").remove()}d.callback()}}});g=true});return this}})(jQuery);