//============================================================ // // the mit license // // copyright (c) 2013 matthew wagerfield - @mwagerfield // // permission is hereby granted, free of charge, to any // person obtaining a copy of this software and associated // documentation files (the "software"), to deal in the // software without restriction, including without limitation // the rights to use, copy, modify, merge, publish, distribute, // sublicense, and/or sell copies of the software, and to // permit persons to whom the software is furnished to do // so, subject to the following conditions: // // the above copyright notice and this permission notice // shall be included in all copies or substantial portions // of the software. // // the software is provided "as is", without warranty // of any kind, express or implied, including but not // limited to the warranties of merchantability, fitness // for a particular purpose and noninfringement. in no // event shall the authors or copyright holders be liable // for any claim, damages or other liability, whether in // an action of contract, tort or otherwise, arising from, // out of or in connection with the software or the use // or other dealings in the software. // //============================================================ (function(t,i,e,s){function o(i,e){this.element=i,this.$context=t(i).data("api",this),this.$layers=this.$context.find(".layer");var s={calibratex:this.$context.data("calibrate-x")||null,calibratey:this.$context.data("calibrate-y")||null,invertx:this.$context.data("invert-x")||null,inverty:this.$context.data("invert-y")||null,limitx:parsefloat(this.$context.data("limit-x"))||null,limity:parsefloat(this.$context.data("limit-y"))||null,scalarx:parsefloat(this.$context.data("scalar-x"))||null,scalary:parsefloat(this.$context.data("scalar-y"))||null,frictionx:parsefloat(this.$context.data("friction-x"))||null,frictiony:parsefloat(this.$context.data("friction-y"))||null};for(var o in s)null===s[o]&&delete s[o];t.extend(this,r,e,s),this.calibrationtimer=null,this.calibrationflag=!0,this.enabled=!1,this.depths=[],this.raf=null,this.ox=0,this.oy=0,this.ow=0,this.oh=0,this.cx=0,this.cy=0,this.ix=0,this.iy=0,this.mx=0,this.my=0,this.vx=0,this.vy=0,this.onmousemove=this.onmousemove.bind(this),this.ondeviceorientation=this.ondeviceorientation.bind(this),this.onorientationtimer=this.onorientationtimer.bind(this),this.oncalibrationtimer=this.oncalibrationtimer.bind(this),this.onanimationframe=this.onanimationframe.bind(this),this.onwindowresize=this.onwindowresize.bind(this),this.initialise()}var n="parallax",a=30,r={calibrationthreshold:100,calibrationdelay:500,supportdelay:500,calibratex:!1,calibratey:!0,invertx:!0,inverty:!0,limitx:!1,limity:!1,scalarx:10,scalary:10,frictionx:.1,frictiony:.1};o.prototype.transformsupport=function(t){for(var o=e.createelement("div"),n=!1,a=null,r=!1,h=null,l=null,p=0,c=this.vendors.length;c>p;p++)if(null!==this.vendors[p]?(h=this.vendors[p][0]+"transform",l=this.vendors[p][1]+"transform"):(h="transform",l="transform"),o.style[l]!==s){n=!0;break}switch(t){case"2d":r=n;break;case"3d":n&&(e.body.appendchild(o),o.style[l]="translate3d(1px,1px,1px)",a=i.getcomputedstyle(o).getpropertyvalue(h),r=a!==s&&a.length>0&&"none"!==a,e.body.removechild(o))}return r},o.prototype.ww=null,o.prototype.wh=null,o.prototype.hw=null,o.prototype.hh=null,o.prototype.portrait=null,o.prototype.desktop=!navigator.useragent.match(/(iphone|ipod|ipad|android|blackberry|bb10|mobi|tablet|opera mini|nexus 7)/i),o.prototype.vendors=[null,["-webkit-","webkit"],["-moz-","moz"],["-o-","o"],["-ms-","ms"]],o.prototype.motionsupport=!!i.devicemotionevent,o.prototype.orientationsupport=!!i.deviceorientationevent,o.prototype.orientationstatus=0,o.prototype.transform2dsupport=o.prototype.transformsupport("2d"),o.prototype.transform3dsupport=o.prototype.transformsupport("3d"),o.prototype.initialise=function(){"static"===this.$context.css("position")&&this.$context.css({position:"relative"}),this.$layers.css({position:"absolute",display:"block",height:"100%",width:"100%",left:0,top:0}),this.$layers.first().css({position:"relative"}),this.$layers.each(t.proxy(function(i,e){this.depths.push(t(e).data("depth")||0)},this)),this.accelerate(this.$context),this.accelerate(this.$layers),this.updatedimensions(),this.enable(),this.queuecalibration(this.calibrationdelay)},o.prototype.updatedimensions=function(){this.ox=this.$context.offset().left,this.oy=this.$context.offset().top,this.ow=this.$context.width(),this.oh=this.$context.height(),this.ww=i.innerwidth,this.wh=i.innerheight,this.hw=this.ww/2,this.hh=this.wh/2},o.prototype.queuecalibration=function(t){cleartimeout(this.calibrationtimer),this.calibrationtimer=settimeout(this.oncalibrationtimer,t)},o.prototype.enable=function(){this.enabled||(this.enabled=!0,this.orientationsupport?(this.portrait=null,i.addeventlistener("deviceorientation",this.ondeviceorientation),settimeout(this.onorientationtimer,this.supportdelay)):(this.cx=0,this.cy=0,this.portrait=!1,i.addeventlistener("mousemove",this.onmousemove)),i.addeventlistener("resize",this.onwindowresize),this.raf=requestanimationframe(this.onanimationframe))},o.prototype.disable=function(){this.enabled&&(this.enabled=!1,this.orientationsupport?i.removeeventlistener("deviceorientation",this.ondeviceorientation):i.removeeventlistener("mousemove",this.onmousemove),i.removeeventlistener("resize",this.onwindowresize),cancelanimationframe(this.raf))},o.prototype.calibrate=function(t,i){this.calibratex=t===s?this.calibratex:t,this.calibratey=i===s?this.calibratey:i},o.prototype.invert=function(t,i){this.invertx=t===s?this.invertx:t,this.inverty=i===s?this.inverty:i},o.prototype.friction=function(t,i){this.frictionx=t===s?this.frictionx:t,this.frictiony=i===s?this.frictiony:i},o.prototype.scalar=function(t,i){this.scalarx=t===s?this.scalarx:t,this.scalary=i===s?this.scalary:i},o.prototype.limit=function(t,i){this.limitx=t===s?this.limitx:t,this.limity=i===s?this.limity:i},o.prototype.clamp=function(t,i,e){return t=math.max(t,i),t=math.min(t,e)},o.prototype.css=function(i,e,o){for(var n=null,a=0,r=this.vendors.length;r>a;a++)if(n=null!==this.vendors[a]?t.camelcase(this.vendors[a][1]+"-"+e):e,i.style[n]!==s){i.style[n]=o;break}},o.prototype.accelerate=function(t){for(var i=0,e=t.length;e>i;i++){var s=t[i];this.css(s,"transform","translate3d(0,0,0)"),this.css(s,"transform-style","preserve-3d"),this.css(s,"backface-visibility","hidden")}},o.prototype.setposition=function(t,i,e){i+="%",e+="%",this.transform3dsupport?this.css(t,"transform","translate3d("+i+","+e+",0)"):this.transform2dsupport?this.css(t,"transform","translate("+i+","+e+")"):(t.style.left=i,t.style.top=e)},o.prototype.onorientationtimer=function(){this.orientationsupport&&0===this.orientationstatus&&(this.disable(),this.orientationsupport=!1,this.enable())},o.prototype.oncalibrationtimer=function(){this.calibrationflag=!0},o.prototype.onwindowresize=function(){this.updatedimensions()},o.prototype.onanimationframe=function(){var t=this.ix-this.cx,i=this.iy-this.cy;(math.abs(t)>this.calibrationthreshold||math.abs(i)>this.calibrationthreshold)&&this.queuecalibration(0),this.portrait?(this.mx=(this.calibratex?i:this.iy)*this.scalarx,this.my=(this.calibratey?t:this.ix)*this.scalary):(this.mx=(this.calibratex?t:this.ix)*this.scalarx,this.my=(this.calibratey?i:this.iy)*this.scalary),isnan(parsefloat(this.limitx))||(this.mx=this.clamp(this.mx,-this.limitx,this.limitx)),isnan(parsefloat(this.limity))||(this.my=this.clamp(this.my,-this.limity,this.limity)),this.vx+=(this.mx-this.vx)*this.frictionx,this.vy+=(this.my-this.vy)*this.frictiony;for(var e=0,s=this.$layers.length;s>e;e++){var o=this.depths[e],n=this.$layers[e],a=this.vx*o*(this.invertx?-1:1),r=this.vy*o*(this.inverty?-1:1);this.setposition(n,a,r)}this.raf=requestanimationframe(this.onanimationframe)},o.prototype.ondeviceorientation=function(t){if(!this.desktop&&null!==t.beta&&null!==t.gamma){this.orientationstatus=1;var e=(t.beta||0)/a,s=(t.gamma||0)/a,o=i.innerheight>i.innerwidth;this.portrait!==o&&(this.portrait=o,this.calibrationflag=!0),this.calibrationflag&&(this.calibrationflag=!1,this.cx=e,this.cy=s),this.ix=e,this.iy=s}},o.prototype.onmousemove=function(t){this.ix=(t.pagex-this.hw)/this.hw,this.iy=(t.pagey-this.hh)/this.hh};var h={enable:o.prototype.enable,disable:o.prototype.disable,calibrate:o.prototype.calibrate,friction:o.prototype.friction,invert:o.prototype.invert,scalar:o.prototype.scalar,limit:o.prototype.limit};t.fn[n]=function(i){var e=arguments;return this.each(function(){var s=t(this),a=s.data(n);a||(a=new o(this,i),s.data(n,a)),h[i]&&a[i].apply(a,array.prototype.slice.call(e,1))})}})(window.jquery||window.zepto,window,document),function(){for(var t=0,i=["ms","moz","webkit","o"],e=0;i.length>e&&!window.requestanimationframe;++e)window.requestanimationframe=window[i[e]+"requestanimationframe"],window.cancelanimationframe=window[i[e]+"cancelanimationframe"]||window[i[e]+"cancelrequestanimationframe"];window.requestanimationframe||(window.requestanimationframe=function(i){var e=(new date).gettime(),s=math.max(0,16-(e-t)),o=window.settimeout(function(){i(e+s)},s);return t=e+s,o}),window.cancelanimationframe||(window.cancelanimationframe=function(t){cleartimeout(t)})}();