forked from pixelcog/parallax.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparallax.min.js
6 lines (6 loc) · 6.04 KB
/
parallax.min.js
1
2
3
4
5
6
/*!
* parallax.js v1.1 (http://pixelcog.github.io/parallax.js/)
* Copyright (c) 2014 PixelCog, Inc.
* Licensed under MIT (https://github.com/pixelcog/parallax.js/blob/master/LICENSE)
*/
(function(e,t,n,r){function o(t,n){var i=this;if(typeof n=="object"){delete n.refresh;delete n.render;e.extend(this,n)}this.$element=e(t);if(!this.imageSrc&&this.$element.is("img")){this.imageSrc=this.$element.attr("src")}var s=(this.position+"").toLowerCase().match(/\S+/g)||[];if(s.length<1){s.push("center")}if(s.length==1){s.push(s[0])}if(s[0]=="top"||s[0]=="bottom"||s[1]=="left"||s[1]=="right"){i.positionX=s[1];i.positionY=s[0]}else{i.positionX=s[0];i.positionY=s[1]}if(this.positionX!=r)s[0]=this.positionX.toLowerCase();if(this.positionY!=r)s[1]=this.positionY.toLowerCase();if(this.positionX!="left"&&this.positionX!="right"){if(isNaN(parseInt(this.positionX))){this.positionX="center"}else{this.positionX=parseInt(this.positionX)}}if(this.positionY!="top"&&this.positionY!="bottom"){if(isNaN(parseInt(this.positionY))){this.positionY="center"}else{this.positionY=parseInt(this.positionY)}}this.position=this.positionX+(isNaN(this.positionX)?"":"px")+" "+this.positionY+(isNaN(this.positionY)?"":"px");if(navigator.userAgent.match(/(iPod|iPhone|iPad)/)){if(this.iosFix&&!this.$element.is("img")){this.$element.css({backgroundImage:"url("+this.imageSrc+")",backgroundSize:"cover",backgroundPosition:this.position})}return this}if(navigator.userAgent.match(/(Android)/)){if(this.androidFix&&!this.$element.is("img")){this.$element.css({backgroundImage:"url("+this.imageSrc+")",backgroundSize:"cover",backgroundPosition:this.position})}return this}this.$mirror=e("<div />").prependTo("body");this.$slider=e("<img />").prependTo(this.$mirror);this.$mirror.addClass("parallax-mirror").css({visibility:"hidden",zIndex:this.zIndex,position:"fixed",top:0,left:0,overflow:"hidden"});this.$slider.addClass("parallax-slider").one("load",function(){if(!i.naturalHeight||!i.naturalWidth){i.naturalHeight=this.naturalHeight||this.height||1;i.naturalWidth=this.naturalWidth||this.width||1}i.aspectRatio=i.naturalWidth/i.naturalHeight;o.isSetup||o.setup();o.sliders.push(i);o.isFresh=false;o.requestRender()});this.$slider[0].src=this.imageSrc;if(this.naturalHeight&&this.naturalWidth||this.$slider[0].complete){this.$slider.trigger("load")}}function u(r){return this.each(function(){var i=e(this);var s=typeof r=="object"&&r;if(this==t||this==n||i.is("body")){o.configure(s)}else if(!i.data("px.parallax")){s=e.extend({},i.data(),s);i.data("px.parallax",new o(this,s))}if(typeof r=="string"){o[r]()}})}(function(){var e=0;var n=["ms","moz","webkit","o"];for(var r=0;r<n.length&&!t.requestAnimationFrame;++r){t.requestAnimationFrame=t[n[r]+"RequestAnimationFrame"];t.cancelAnimationFrame=t[n[r]+"CancelAnimationFrame"]||t[n[r]+"CancelRequestAnimationFrame"]}if(!t.requestAnimationFrame)t.requestAnimationFrame=function(n,r){var i=(new Date).getTime();var s=Math.max(0,16-(i-e));var o=t.setTimeout(function(){n(i+s)},s);e=i+s;return o};if(!t.cancelAnimationFrame)t.cancelAnimationFrame=function(e){clearTimeout(e)}})();var i=e("body");var s=e(t);e.extend(o.prototype,{speed:.2,bleed:0,zIndex:-100,iosFix:true,androidFix:true,position:"center",refresh:function(){this.boxWidth=this.$element.width();this.boxHeight=this.$element.height()+this.bleed*2;this.boxOffsetTop=this.$element.offset().top-this.bleed;this.boxOffsetLeft=this.$element.offset().left;this.boxOffsetBottom=this.boxOffsetTop+this.boxHeight;var e=0;var t=o.winHeight;var n=t-(t-this.boxHeight)*this.speed|0;if(n*this.aspectRatio>=this.boxWidth){this.imageWidth=n*this.aspectRatio|0;this.imageHeight=n;this.offsetBaseTop=0;e=this.imageWidth-this.boxWidth;if(this.positionX=="left"){this.offsetLeft=0}else if(this.positionX=="right"){this.offsetLeft=-e}else if(!isNaN(this.positionX)){this.offsetLeft=Math.max(this.positionX,-e)}else{this.offsetLeft=-e/2|0}}else{this.imageWidth=this.boxWidth;this.imageHeight=this.boxWidth/this.aspectRatio|0;this.offsetLeft=0;e=this.imageHeight-n;if(this.positionY=="top"){this.offsetBaseTop=0}else if(this.positionY=="bottom"){this.offsetBaseTop=-e}else if(!isNaN(this.positionY)){this.offsetBaseTop=Math.max(this.positionY,-e)}else{this.offsetBaseTop=-e/2|0}}},render:function(){var e=o.scrollTop;var t=o.scrollLeft;var n=e+o.winHeight;if(this.boxOffsetBottom>e&&this.boxOffsetTop<n){this.visibility="visible"}else{this.visibility="hidden"}this.mirrorTop=this.boxOffsetTop-e;this.mirrorLeft=this.boxOffsetLeft-t;this.offsetTop=this.offsetBaseTop-this.mirrorTop*(1-this.speed);this.$mirror.css({transform:"translate3d(0px, 0px, 0px)",visibility:this.visibility,top:this.mirrorTop,left:this.mirrorLeft,height:this.boxHeight,width:this.boxWidth});this.$slider.css({transform:"translate3d(0px, 0px, 0px)",position:"absolute",top:this.offsetTop,left:this.offsetLeft,height:this.imageHeight,width:this.imageWidth})}});e.extend(o,{scrollTop:0,scrollLeft:0,winHeight:0,winWidth:0,docHeight:1<<30,docWidth:1<<30,sliders:[],isReady:false,isFresh:false,isBusy:false,setup:function(){if(this.isReady)return;s.on("scroll.px.parallax load.px.parallax",function(){var e=o.docHeight-o.winHeight;var t=o.docWidth-o.winWidth;o.scrollTop=Math.max(0,Math.min(e,s.scrollTop()));o.scrollLeft=Math.max(0,Math.min(t,s.scrollLeft()));o.requestRender()}).on("resize.px.parallax load.px.parallax",function(){o.winHeight=s.height();o.winWidth=s.width();o.docHeight=e(n).height();o.docWidth=e(n).width();o.isFresh=false;o.requestRender()});this.isReady=true},configure:function(t){if(typeof t=="object"){delete t.refresh;delete t.render;e.extend(this.prototype,t)}},refresh:function(){e.each(this.sliders,function(){this.refresh()});this.isFresh=true},render:function(){this.isFresh||this.refresh();e.each(this.sliders,function(){this.render()})},requestRender:function(){var e=this;if(!this.isBusy){this.isBusy=true;t.requestAnimationFrame(function(){e.render();e.isBusy=false})}}});var a=e.fn.parallax;e.fn.parallax=u;e.fn.parallax.Constructor=o;e.fn.parallax.noConflict=function(){e.fn.parallax=a;return this};e(n).on("ready.px.parallax.data-api",function(){e('[data-parallax="scroll"]').parallax()})})(jQuery,window,document)