This repository has been archived by the owner on Apr 20, 2022. It is now read-only.
forked from markdalgleish/stellar.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.stellar.min.js
3 lines (2 loc) · 12.4 KB
/
jquery.stellar.min.js
1
2
3
/*! Stellar.js v0.6.2 | Copyright 2017, Mark Dalgleish | http://markdalgleish.com/projects/stellar.js | http://markdalgleish.mit-license.org */
!function(t,e,i){function s(e,i){this.element=e,this.options=t.extend({},n,i),this._defaults=n,this._name=o,this.init()}var o="stellar",n={scrollProperty:"scroll",positionProperty:"position",horizontalScrolling:!0,verticalScrolling:!0,horizontalOffset:0,verticalOffset:0,responsive:!1,parallaxBackgrounds:!0,parallaxElements:!0,hideDistantElements:!0,hideElement:function(t){t.hide()},showElement:function(t){t.show()}},r=function(){var e,i=/^(Moz|Webkit|Khtml|O|ms|Icab)(?=[A-Z])/,s=t("script")[0].style,o="";for(e in s)if(i.test(e)){o=e.match(i)[0];break}return"WebkitOpacity"in s&&(o="Webkit"),"KhtmlOpacity"in s&&(o="Khtml"),function(t){return o+(o.length>0?t.charAt(0).toUpperCase()+t.slice(1):t)}}()("transform"),a={scroll:{getLeft:function(t){return t.scrollLeft()},setLeft:function(t,e){t.scrollLeft(e)},getTop:function(t){return t.scrollTop()},setTop:function(t,e){t.scrollTop(e)}},position:{getLeft:function(t){return-1*parseInt(t.css("left"),10)},getTop:function(t){return-1*parseInt(t.css("top"),10)}},margin:{getLeft:function(t){return-1*parseInt(t.css("margin-left"),10)},getTop:function(t){return-1*parseInt(t.css("margin-top"),10)}},transform:{getLeft:function(t){var e=getComputedStyle(t[0])[r];return"none"!==e?-1*parseInt(e.match(/(-?[0-9]+)/g)[4],10):0},getTop:function(t){var e=getComputedStyle(t[0])[r];return"none"!==e?-1*parseInt(e.match(/(-?[0-9]+)/g)[5],10):0}}},l={position:{setLeft:function(t,e){t.css("left",e)},setTop:function(t,e){t.css("top",e)}},transform:{setPosition:function(t,e,i,s,o){t[0].style[r]="translate3d("+(e-i)+"px, "+(s-o)+"px, 0)"}}},f=void 0!==t("<div />",{style:"background:#fff"}).css("background-position-x"),c=f?function(t,e,i){t.css({"background-position-x":e,"background-position-y":i})}:function(t,e,i){t.css("background-position",e+" "+i)},p=f?function(t){return[t.css("background-position-x"),t.css("background-position-y")]}:function(t){return t.css("background-position").split(" ")},h=window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||window.oRequestAnimationFrame||window.msRequestAnimationFrame||function(t){setTimeout(t,1e3/60)};s.prototype={init:function(){this.options.name=o+"_"+Math.floor(1e9*Math.random()),this._defineElements(),this._defineGetters(),this._defineSetters(),this._handleWindowLoadAndResize(),this._detectViewport(),this.refresh({firstLoad:!0}),"scroll"===this.options.scrollProperty?this._handleScrollEvent():this._startAnimationLoop()},_defineElements:function(){this.element===e.body&&(this.element=window),this.$scrollElement=t(this.element),this.$element=this.element===window?t("body"):this.$scrollElement,this.$viewportElement=void 0!==this.options.viewportElement?t(this.options.viewportElement):this.$scrollElement[0]===window||"scroll"===this.options.scrollProperty?this.$scrollElement:this.$scrollElement.parent()},_defineGetters:function(){var t=this,e=a[t.options.scrollProperty];this._getScrollLeft=function(){return e.getLeft(t.$scrollElement)},this._getScrollTop=function(){return e.getTop(t.$scrollElement)}},_defineSetters:function(){var e=this,i=a[e.options.scrollProperty],s=l[e.options.positionProperty],o=i.setLeft,n=i.setTop;this._setScrollLeft="function"==typeof o?function(t){o(e.$scrollElement,t)}:t.noop,this._setScrollTop="function"==typeof n?function(t){n(e.$scrollElement,t)}:t.noop,this._setPosition=s.setPosition||function(t,i,o,n,r){e.options.horizontalScrolling&&s.setLeft(t,i,o),e.options.verticalScrolling&&s.setTop(t,n,r)}},_handleWindowLoadAndResize:function(){var e=this,i=t(window);e.options.responsive&&i.bind("load."+this.name,function(){e.refresh()}),i.bind("resize."+this.name,function(){e._detectViewport(),e.options.responsive&&e.refresh()})},refresh:function(e){var i=this,s=i._getScrollLeft(),o=i._getScrollTop();e&&e.firstLoad||this._reset(),this._setScrollLeft(0),this._setScrollTop(0),this._setOffsets(),this._findParticles(),this._findBackgrounds(),e&&e.firstLoad&&/WebKit/.test(navigator.userAgent)&&t(window).load(function(){var t=i._getScrollLeft(),e=i._getScrollTop();i._setScrollLeft(t+1),i._setScrollTop(e+1),i._setScrollLeft(t),i._setScrollTop(e)}),this._setScrollLeft(s),this._setScrollTop(o)},_detectViewport:function(){var t=this.$viewportElement.offset(),e=null!==t&&void 0!==t;this.viewportWidth=this.$viewportElement.width(),this.viewportHeight=this.$viewportElement.height(),this.viewportOffsetTop=e?t.top:0,this.viewportOffsetLeft=e?t.left:0},_findParticles:function(){var e=this;this._getScrollLeft(),this._getScrollTop();if(void 0!==this.particles)for(var i=this.particles.length-1;i>=0;i--)this.particles[i].$element.data("stellar-elementIsActive",void 0);this.particles=[],this.options.parallaxElements&&this.$element.find("[data-stellar-ratio]").each(function(i){var s,o,n,r,a,l,f,c,p,h=t(this),d=0,u=0,g=0,m=0;if(h.data("stellar-elementIsActive")){if(h.data("stellar-elementIsActive")!==this)return}else h.data("stellar-elementIsActive",this);e.options.showElement(h),h.data("stellar-startingLeft")?(h.css("left",h.data("stellar-startingLeft")),h.css("top",h.data("stellar-startingTop"))):(h.data("stellar-startingLeft",h.css("left")),h.data("stellar-startingTop",h.css("top"))),n=h.position().left,r=h.position().top,a="auto"===h.css("margin-left")?0:parseInt(h.css("margin-left"),10),l="auto"===h.css("margin-top")?0:parseInt(h.css("margin-top"),10),c=h.offset().left-a,p=h.offset().top-l,h.parents().each(function(){var e=t(this);if(!0===e.data("stellar-offset-parent"))return d=g,u=m,f=e,!1;g+=e.position().left,m+=e.position().top}),s=void 0!==h.data("stellar-horizontal-offset")?h.data("stellar-horizontal-offset"):void 0!==f&&void 0!==f.data("stellar-horizontal-offset")?f.data("stellar-horizontal-offset"):e.horizontalOffset,o=void 0!==h.data("stellar-vertical-offset")?h.data("stellar-vertical-offset"):void 0!==f&&void 0!==f.data("stellar-vertical-offset")?f.data("stellar-vertical-offset"):e.verticalOffset,e.particles.push({$element:h,$offsetParent:f,isFixed:"fixed"===h.css("position"),horizontalOffset:s,verticalOffset:o,startingPositionLeft:n,startingPositionTop:r,startingOffsetLeft:c,startingOffsetTop:p,parentOffsetLeft:d,parentOffsetTop:u,stellarRatio:void 0!==h.data("stellar-ratio")?h.data("stellar-ratio"):1,width:h.outerWidth(!0),height:h.outerHeight(!0),isHidden:!1})})},_findBackgrounds:function(){var e,i=this,s=this._getScrollLeft(),o=this._getScrollTop();this.backgrounds=[],this.options.parallaxBackgrounds&&(e=this.$element.find("[data-stellar-background-ratio]"),this.$element.data("stellar-background-ratio")&&(e=e.add(this.$element)),e.each(function(){var e,n,r,a,l,f,h,d=t(this),u=p(d),g=0,m=0,v=0,L=0;if(d.data("stellar-backgroundIsActive")){if(d.data("stellar-backgroundIsActive")!==this)return}else d.data("stellar-backgroundIsActive",this);d.data("stellar-backgroundStartingLeft")?c(d,d.data("stellar-backgroundStartingLeft"),d.data("stellar-backgroundStartingTop")):(d.data("stellar-backgroundStartingLeft",u[0]),d.data("stellar-backgroundStartingTop",u[1])),r="auto"===d.css("margin-left")?0:parseInt(d.css("margin-left"),10),a="auto"===d.css("margin-top")?0:parseInt(d.css("margin-top"),10),l=d.offset().left-r-s,f=d.offset().top-a-o,d.parents().each(function(){var e=t(this);if(!0===e.data("stellar-offset-parent"))return g=v,m=L,h=e,!1;v+=e.position().left,L+=e.position().top}),e=void 0!==d.data("stellar-horizontal-offset")?d.data("stellar-horizontal-offset"):void 0!==h&&void 0!==h.data("stellar-horizontal-offset")?h.data("stellar-horizontal-offset"):i.horizontalOffset,n=void 0!==d.data("stellar-vertical-offset")?d.data("stellar-vertical-offset"):void 0!==h&&void 0!==h.data("stellar-vertical-offset")?h.data("stellar-vertical-offset"):i.verticalOffset,i.backgrounds.push({$element:d,$offsetParent:h,isFixed:"fixed"===d.css("background-attachment"),horizontalOffset:e,verticalOffset:n,startingValueLeft:u[0],startingValueTop:u[1],startingBackgroundPositionLeft:isNaN(parseInt(u[0],10))?0:parseInt(u[0],10),startingBackgroundPositionTop:isNaN(parseInt(u[1],10))?0:parseInt(u[1],10),startingPositionLeft:d.position().left,startingPositionTop:d.position().top,startingOffsetLeft:l,startingOffsetTop:f,parentOffsetLeft:g,parentOffsetTop:m,stellarRatio:void 0===d.data("stellar-background-ratio")?1:d.data("stellar-background-ratio")})}))},_reset:function(){var t,e,i,s,o;for(o=this.particles.length-1;o>=0;o--)e=(t=this.particles[o]).$element.data("stellar-startingLeft"),i=t.$element.data("stellar-startingTop"),this._setPosition(t.$element,e,e,i,i),this.options.showElement(t.$element),t.$element.data("stellar-startingLeft",null).data("stellar-elementIsActive",null).data("stellar-backgroundIsActive",null);for(o=this.backgrounds.length-1;o>=0;o--)(s=this.backgrounds[o]).$element.data("stellar-backgroundStartingLeft",null).data("stellar-backgroundStartingTop",null),c(s.$element,s.startingValueLeft,s.startingValueTop)},destroy:function(){this._reset(),this.$scrollElement.unbind("resize."+this.name).unbind("scroll."+this.name),this._animationLoop=t.noop,t(window).unbind("load."+this.name).unbind("resize."+this.name)},_setOffsets:function(){var e=this,i=t(window);i.unbind("resize.horizontal-"+this.name).unbind("resize.vertical-"+this.name),"function"==typeof this.options.horizontalOffset?(this.horizontalOffset=this.options.horizontalOffset(),i.bind("resize.horizontal-"+this.name,function(){e.horizontalOffset=e.options.horizontalOffset()})):this.horizontalOffset=this.options.horizontalOffset,"function"==typeof this.options.verticalOffset?(this.verticalOffset=this.options.verticalOffset(),i.bind("resize.vertical-"+this.name,function(){e.verticalOffset=e.options.verticalOffset()})):this.verticalOffset=this.options.verticalOffset},_repositionElements:function(){var t,e,i,s,o,n,r,a,l,f,p=this._getScrollLeft(),h=this._getScrollTop(),d=!0,u=!0;if(this.currentScrollLeft!==p||this.currentScrollTop!==h||this.currentWidth!==this.viewportWidth||this.currentHeight!==this.viewportHeight){for(this.currentScrollLeft=p,this.currentScrollTop=h,this.currentWidth=this.viewportWidth,this.currentHeight=this.viewportHeight,f=this.particles.length-1;f>=0;f--)e=(t=this.particles[f]).isFixed?1:0,this.options.horizontalScrolling?a=(n=(p+t.horizontalOffset+this.viewportOffsetLeft+t.startingPositionLeft-t.startingOffsetLeft+t.parentOffsetLeft)*-(t.stellarRatio+e-1)+t.startingPositionLeft)-t.startingPositionLeft+t.startingOffsetLeft:(n=t.startingPositionLeft,a=t.startingOffsetLeft),this.options.verticalScrolling?l=(r=(h+t.verticalOffset+this.viewportOffsetTop+t.startingPositionTop-t.startingOffsetTop+t.parentOffsetTop)*-(t.stellarRatio+e-1)+t.startingPositionTop)-t.startingPositionTop+t.startingOffsetTop:(r=t.startingPositionTop,l=t.startingOffsetTop),this.options.hideDistantElements&&(u=!this.options.horizontalScrolling||a+t.width>(t.isFixed?0:p)&&a<(t.isFixed?0:p)+this.viewportWidth+this.viewportOffsetLeft,d=!this.options.verticalScrolling||l+t.height>(t.isFixed?0:h)&&l<(t.isFixed?0:h)+this.viewportHeight+this.viewportOffsetTop),u&&d?(t.isHidden&&(this.options.showElement(t.$element),t.isHidden=!1),this._setPosition(t.$element,n,t.startingPositionLeft,r,t.startingPositionTop)):t.isHidden||(this.options.hideElement(t.$element),t.isHidden=!0);for(f=this.backgrounds.length-1;f>=0;f--)e=(i=this.backgrounds[f]).isFixed?0:1,s=this.options.horizontalScrolling?(p+i.horizontalOffset-this.viewportOffsetLeft-i.startingOffsetLeft+i.parentOffsetLeft-i.startingBackgroundPositionLeft)*(e-i.stellarRatio)+"px":i.startingValueLeft,o=this.options.verticalScrolling?(h+i.verticalOffset-this.viewportOffsetTop-i.startingOffsetTop+i.parentOffsetTop-i.startingBackgroundPositionTop)*(e-i.stellarRatio)+"px":i.startingValueTop,c(i.$element,s,o)}},_handleScrollEvent:function(){var t=this,e=!1,i=function(){t._repositionElements(),e=!1},s=function(){e||(h(i),e=!0)};this.$scrollElement.bind("scroll."+this.name,s),s()},_startAnimationLoop:function(){var t=this;this._animationLoop=function(){h(t._animationLoop),t._repositionElements()},this._animationLoop()}},t.fn[o]=function(e){var i=arguments;return void 0===e||"object"==typeof e?this.each(function(){t.data(this,"plugin_"+o)||t.data(this,"plugin_"+o,new s(this,e))}):"string"==typeof e&&"_"!==e[0]&&"init"!==e?this.each(function(){var n=t.data(this,"plugin_"+o);n instanceof s&&"function"==typeof n[e]&&n[e].apply(n,Array.prototype.slice.call(i,1)),"destroy"===e&&t.data(this,"plugin_"+o,null)}):void 0},t[o]=function(e){var i=t(window);return i.stellar.apply(i,Array.prototype.slice.call(arguments,0))},t[o].scrollProperty=a,t[o].positionProperty=l,window.Stellar=s}(jQuery,document);