1 line
2.3 KiB
JavaScript
1 line
2.3 KiB
JavaScript
window,document,nb_.listenTo("nb-app-ready",function(){!function(e,r){var o="parallaxBg",n={parallaxForce:40,oncustom:[],matchMedia:"only screen and (max-width: 800px)"};function t(i,t){this.element=e(i),this.options=e.extend({},n,t,this.parseElementDataOptions()),this._defaults=n,this._name=o,this.init()}t.prototype.parseElementDataOptions=function(){return this.element.data()},t.prototype.init=function(){var t=this;this.$_window=nb_.cachedElements.$window,this.doingAnimation=!1,this.isVisible=!1,this.isBefore=!1,this.isAfter=!0,("number"!=typeof t.options.parallaxForce||t.options.parallaxForce<0)&&(sekFrontLocalized.isDevMode&&console.log("parallaxBg => the provided parallaxForce is invalid => "+t.options.parallaxForce),t.options.parallaxForce=this._defaults.parallaxForce),100<t.options.parallaxForce&&(t.options.parallaxForce=100),this.$_window.on("scroll",function(i){t.maybeParallaxMe(i)}),this.$_window.on("resize",nb_.debounce(function(i){t.maybeParallaxMe(i)},100)),this.checkIfIsVisibleAndCacheProperties(),this.setTopPositionAndBackgroundSize()},t.prototype.setTopPositionAndBackgroundSize=function(){if(nb_.isFunction(r.matchMedia)&&matchMedia(this.options.matchMedia).matches)this.element.css({"background-position-y":"","background-attachment":""});else{var i=this.element,t=(i.outerHeight(),this.$_window.height()),e=i.offset().top,o=this.$_window.scrollTop(),n=100;this.isVisible?n=(e-o)/t:this.isBefore?n=1:this.isAfter&&(n=-1);var s=0<this.options.parallaxForce?t*(100-this.options.parallaxForce)/100:t,a=Math.round(n*s);this.element.css({"background-position-y":["calc(50% ",0<a?"+ ":"- ",Math.abs(a)+"px)"].join("")})}},t.prototype.checkIfIsVisibleAndCacheProperties=function(i){var t=this.element;if(!t.is(":visible"))return!1;var e=this.$_window.scrollTop(),o=e+this.$_window.height(),n=t.offset().top,s=n+t.outerHeight();return this.isVisible=e<=s&&n<=o,this.isBefore=o<n,this.isAfter=s<e,this.isVisible},t.prototype.maybeParallaxMe=function(i){var t=this;this.checkIfIsVisibleAndCacheProperties()&&(this.doingAnimation||(this.doingAnimation=!0,r.requestAnimationFrame(function(){t.setTopPositionAndBackgroundSize(),t.doingAnimation=!1})))},e.fn[o]=function(i){return this.each(function(){e.data(this,"plugin_"+o)||e.data(this,"plugin_"+o,new t(this,i))})}}(jQuery,window),nb_.emit("nb-parallax-parsed")}); |