killrawr

Parallax JS

Dec 12th, 2019
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*!
  2.  * parallax.js v1.5.0 (http://pixelcog.github.io/parallax.js/)
  3.  * @copyright 2016 PixelCog, Inc.
  4.  * @license MIT (https://github.com/pixelcog/parallax.js/blob/master/LICENSE)
  5.  * @bug-fix Michael <[email protected]> 16/08/2019 - Fixed bug when no HEIGHT is found, and parallax renders blank
  6.  */
  7. (function($, window, document, undefined) {
  8.  
  9.     // Polyfill for requestAnimationFrame
  10.     // via: https://gist.github.com/paulirish/1579671
  11.  
  12.     (function() {
  13.  
  14.         var lastTime = 0;
  15.         var vendors = ['ms', 'moz', 'webkit', 'o'];
  16.  
  17.         for (var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
  18.             window.requestAnimationFrame = window[vendors[x] + 'RequestAnimationFrame'];
  19.             window.cancelAnimationFrame = window[vendors[x] + 'CancelAnimationFrame'] || window[vendors[x] + 'CancelRequestAnimationFrame'];
  20.         };
  21.  
  22.         if (!window.requestAnimationFrame) {
  23.             window.requestAnimationFrame = function(callback) {
  24.  
  25.                 var currTime = new Date().getTime();
  26.                 var timeToCall = Math.max(0, 16 - (currTime - lastTime));
  27.  
  28.                 var timeToFn = function() {
  29.                     callback(currTime + timeToCall);
  30.                 };
  31.  
  32.                 var id = window.setTimeout(timeToFn, timeToCall);
  33.                 lastTime = currTime + timeToCall;
  34.                 return id;
  35.  
  36.             };
  37.         };
  38.  
  39.         if (!window.cancelAnimationFrame) {
  40.             window.cancelAnimationFrame = function(id) {
  41.                 clearTimeout(id);
  42.             };
  43.         };
  44.  
  45.     }());
  46.  
  47.  
  48.     // Parallax Constructor
  49.  
  50.     function Parallax(element, options) {
  51.  
  52.         var self = this;
  53.  
  54.         if (typeof options == 'object') {
  55.             delete options.refresh;
  56.             delete options.render;
  57.             $.extend(this, options);
  58.         };
  59.  
  60.         this.$parallaxArgs = new URLSearchParams(window.location.search);
  61.  
  62.         this.$element = $(element);
  63.         this.isParallaxElement = (String(this.$element.data('parallax')) == 'scroll');
  64.         this.elementMirror = Boolean(this.$element.data('mirrorContainer'));
  65.         this.isUnderReview = Boolean(this.$parallaxArgs.get('parallax_review')) && !this.isParallaxElement;
  66.  
  67.         if (!Boolean(this.elementMirror) && this.isParallaxElement) {
  68.             this.$mirrorContainer = this.$element;
  69.         } else {
  70.             this.$mirrorContainer = $(this.mirrorContainer);
  71.         };
  72.  
  73.         if (typeof(this.parallaxIsFullWidth) !== 'undefined') {
  74.             this.$parallaxIsFullWidth = this.parallaxIsFullWidth;
  75.         } else {
  76.             this.$parallaxIsFullWidth = true;
  77.         };
  78.  
  79.         if (Boolean(this.boxElement)) {
  80.             this.$boxElement = $(this.boxElement);
  81.         } else {
  82.             this.$boxElement = this.$element;
  83.         };
  84.  
  85.         if (Boolean(this.boxWidthElement)) {
  86.             this.$boxWidthElement = $(this.boxWidthElement);
  87.         } else {
  88.             this.$boxWidthElement = this.$boxElement;
  89.         };
  90.  
  91.         if (Boolean(this.boxHeightElement)) {
  92.             this.$boxHeightElement = $(this.boxHeightElement);
  93.         } else {
  94.             this.$boxHeightElement = this.$boxElement;
  95.         };;
  96.  
  97.         if (!this.imageSrc && this.$element.is('img')) {
  98.             this.imageSrc = this.$element.attr('src');
  99.         };
  100.  
  101.         var positions = (this.position + '').toLowerCase().match(/\S+/g) || [];
  102.  
  103.         if (positions.length < 1) {
  104.             positions.push('center');
  105.         };
  106.  
  107.         if (positions.length == 1) {
  108.             positions.push(positions[0]);
  109.         };
  110.  
  111.         if (positions[0] == 'top' || positions[0] == 'bottom' || positions[1] == 'left' || positions[1] == 'right') {
  112.             positions = [positions[1], positions[0]];
  113.         };
  114.  
  115.         if (this.positionX !== undefined) {
  116.             positions[0] = this.positionX.toLowerCase();
  117.         };
  118.  
  119.         if (this.positionY !== undefined) {
  120.             positions[1] = this.positionY.toLowerCase();
  121.         };
  122.  
  123.         self.positionX = positions[0];
  124.         self.positionY = positions[1];
  125.  
  126.         if (this.positionX != 'left' && this.positionX != 'right') {
  127.             if (isNaN(parseInt(this.positionX))) {
  128.                 this.positionX = 'center';
  129.             } else {
  130.                 this.positionX = parseInt(this.positionX);
  131.             };
  132.         };
  133.  
  134.         if (this.positionY != 'top' && this.positionY != 'bottom') {
  135.             if (isNaN(parseInt(this.positionY))) {
  136.                 this.positionY = 'center';
  137.             } else {
  138.                 this.positionY = parseInt(this.positionY);
  139.             };
  140.         };
  141.  
  142.         var position_s = [];
  143.         position_s.push(this.positionX + (isNaN(this.positionX) ? '' : 'px'));
  144.         position_s.push(this.positionY + (isNaN(this.positionY) ? '' : 'px'));
  145.  
  146.         this.position = position_s.join(' ');
  147.         this.imgIsBackground = Boolean(this.imageSrc) && (Boolean(this.$element.is('img')) == false);
  148.  
  149.         var mobileBackground = function(mobileFix) {
  150.  
  151.             var backgroundCss = {
  152.                 backgroundImage: 'url(' + self.imageSrc + ')',
  153.                 backgroundSize: 'cover',
  154.                 backgroundPosition: self.position
  155.             };
  156.  
  157.             if (self.imgIsBackground && Boolean(mobileFix)) {
  158.                 self.$element.css(backgroundCss);
  159.                 return true;
  160.             };
  161.             return false;
  162.         };
  163.  
  164.         var isApple = Boolean(navigator.userAgent.match(/(iPod|iPhone|iPad)/));
  165.  
  166.         if (isApple) {
  167.             mobileBackground(self.iosFix)
  168.             return this;
  169.         };
  170.  
  171.         var isAndroid = Boolean(navigator.userAgent.match(/(Android)/));
  172.  
  173.         if (isAndroid) {
  174.             mobileBackground(self.androidFix);
  175.             return this;
  176.         };
  177.  
  178.         var runPxScripts = self.$parallaxArgs.has('parallax_scripts') ? self.$parallaxArgs.get('parallax_scripts') : 1;
  179.         runPxScripts = Boolean(Number(runPxScripts) > 0);
  180.  
  181.         if (!runPxScripts) {
  182.             mobileBackground(true);
  183.             return this;
  184.         };
  185.  
  186.         var sliderImg = this.$mirrorContainer.find('.parallax-slider-image').first();
  187.         var sliderExisted = false;
  188.  
  189.         if (!sliderImg.length) {
  190.             sliderImg = $('<img />');
  191.         } else {
  192.             sliderExisted = true;
  193.         };
  194.  
  195.         this.$mirror = $('<div />').prependTo(this.$mirrorContainer);
  196.         var slider = this.$element.find('>.parallax-slider');
  197.  
  198.         if (slider.length == 0) {
  199.  
  200.             var sliderResult = false;
  201.  
  202.             if (!sliderExisted) {
  203.                 sliderResult = sliderImg.prependTo(this.$mirror);
  204.             } else {
  205.                 this.$mirror.append(sliderImg);
  206.                 sliderResult = sliderImg;
  207.             };
  208.  
  209.             this.$slider = sliderResult;
  210.  
  211.         } else {
  212.             this.$slider = slider.prependTo(this.$mirror)
  213.             sliderExisted = true;
  214.         };
  215.  
  216.         this.$mirror.addClass('parallax-mirror').css({
  217.             visibility: 'hidden',
  218.             zIndex: this.zIndex,
  219.             position: 'fixed',
  220.             top: 0,
  221.             left: 0,
  222.             overflow: 'hidden'
  223.         });
  224.  
  225.         this.$slider = $(this.$slider);
  226.         this.$slider.addClass(this.sliderParallaxClasses);
  227.         this.$slider.addClass('parallax-slider');
  228.  
  229.         var sliderImageLoader = function(this_image) {
  230.  
  231.             if (!Boolean(self.naturalHeight) || !Boolean(self.naturalWidth)) {
  232.                 self.naturalHeight = this_image.naturalHeight || this_image.height || 1;
  233.                 self.naturalWidth = this_image.naturalWidth || this_image.width || 1;
  234.                 self.aspectRatio = self.naturalWidth / self.naturalHeight;
  235.             };
  236.  
  237.             Parallax.isSetup || Parallax.setup();
  238.             Parallax.sliders.push(self);
  239.             Parallax.isFresh = false;
  240.             Parallax.requestRender();
  241.  
  242.             if (typeof(self.loadFinished) === 'function') {
  243.                 self.loadFinished(self);
  244.             };
  245.  
  246.         };
  247.  
  248.         var imageIsLoaded = false;
  249.  
  250.         if (typeof(this.$slider.get(0).complete) !== 'undefined') {
  251.             imageIsLoaded = Boolean(true);
  252.             imageIsLoaded = (imageIsLoaded && Boolean(this.$slider));
  253.             imageIsLoaded = (imageIsLoaded && Boolean(this.$slider.get(0).complete));
  254.             imageIsLoaded = (imageIsLoaded && Boolean(this.$slider.attr('src')));
  255.         };
  256.  
  257.         if (imageIsLoaded) {
  258.  
  259.             sliderImageLoader(this.$slider.get(0));
  260.  
  261.         } else {
  262.  
  263.             this.$slider.on('load', function() {
  264.                 var this_image = this;
  265.                 sliderImageLoader(this_image);
  266.             });
  267.  
  268.             var currentImageSrc = this.$slider.attr('src');
  269.  
  270.             if (typeof(currentImageSrc) === 'undefined') {
  271.                 currentImageSrc = false;
  272.             } else {
  273.                 currentImageSrc = String(currentImageSrc).trim();
  274.             };
  275.  
  276.             if (!sliderExisted || !Boolean(currentImageSrc)) {
  277.                 this.$slider.attr('src', this.imageSrc);
  278.             };
  279.  
  280.             var forceImageLoad = true;
  281.  
  282.             if (!imageIsLoaded) {
  283.                 forceImageLoad = Boolean(Boolean(this.naturalHeight && this.naturalWidth) || this.$slider[0].complete || slider.length > 0);
  284.             };
  285.  
  286.             if (forceImageLoad) {
  287.                 this.$slider.trigger('load');
  288.             };
  289.  
  290.         };
  291.  
  292.     };
  293.  
  294.     var parallaxRefresh = function() {
  295.  
  296.         this.boxWidthValue = this.$boxWidthElement.outerWidth();
  297.         this.boxHeightValue = this.$boxHeightElement.outerHeight();
  298.  
  299.         this.boxLeftOffset = jQuery(this.$boxWidthElement).position().left;
  300.  
  301.         if (this.$parallaxIsFullWidth) {
  302.             this.boxIsFullWidth = this.$parallaxIsFullWidth;
  303.         } else {
  304.             this.boxIsFullWidth = Boolean(this.boxWidthValue == Parallax.winWidth);
  305.         };
  306.  
  307.         this.boxWidth = this.boxWidthValue;
  308.         this.boxHeight = this.boxHeightValue + this.bleed * 2;
  309.         this.boxOffsetTop = this.$boxElement.offset().top - this.bleed;
  310.         this.boxOffsetLeft = this.$boxElement.offset().left;
  311.         this.boxOffsetBottom = this.boxOffsetTop + this.boxHeight;
  312.  
  313.         var winHeight = Parallax.winHeight;
  314.         var docHeight = Parallax.docHeight;
  315.         var maxOffset = Math.min(this.boxOffsetTop, docHeight - winHeight);
  316.         var minOffset = Math.max(this.boxOffsetTop + this.boxHeight - winHeight, 0);
  317.         var imageHeightMin = this.boxHeight + (maxOffset - minOffset) * (1 - this.speed) | 0;
  318.         var imageOffsetMin = (this.boxOffsetTop - maxOffset) * (1 - this.speed) | 0;
  319.         var margin;
  320.  
  321.         if (imageHeightMin * this.aspectRatio >= this.boxWidth) {
  322.  
  323.             this.imageWidth = imageHeightMin * this.aspectRatio | 0;
  324.             this.imageHeight = imageHeightMin;
  325.             this.offsetBaseTop = imageOffsetMin;
  326.  
  327.             margin = this.imageWidth - this.boxWidth;
  328.  
  329.             if (this.positionX == 'left') {
  330.                 this.offsetLeft = 0;
  331.             } else if (this.positionX == 'right') {
  332.                 this.offsetLeft = -margin;
  333.             } else if (!isNaN(this.positionX)) {
  334.                 this.offsetLeft = Math.max(this.positionX, -margin);
  335.             } else {
  336.                 this.offsetLeft = -margin / 2 | 0;
  337.             };
  338.  
  339.         } else {
  340.  
  341.             this.imageWidth = this.boxWidth;
  342.             this.imageHeight = this.boxWidth / this.aspectRatio | 0;
  343.             this.offsetLeft = 0;
  344.  
  345.             margin = this.imageHeight - imageHeightMin;
  346.  
  347.             if (this.positionY == 'top') {
  348.                 this.offsetBaseTop = imageOffsetMin;
  349.             } else if (this.positionY == 'bottom') {
  350.                 this.offsetBaseTop = imageOffsetMin - margin;
  351.             } else if (!isNaN(this.positionY)) {
  352.                 this.offsetBaseTop = imageOffsetMin + Math.max(this.positionY, -margin);
  353.             } else {
  354.                 this.offsetBaseTop = imageOffsetMin - margin / 2 | 0;
  355.             };
  356.  
  357.         };
  358.  
  359.     };
  360.  
  361.     var parallaxRender = function() {
  362.  
  363.         var self = this;
  364.         var scrollTop = Parallax.scrollTop;
  365.         var scrollLeft = Parallax.scrollLeft;
  366.         var overScroll = this.overScrollFix ? Parallax.overScroll : 0;
  367.         var scrollBottom = scrollTop + Parallax.winHeight;
  368.         var mirrorTranslate = [];
  369.         var sliderTranslate = [];
  370.  
  371.         var boxIsVisible = (this.boxOffsetBottom > scrollTop && this.boxOffsetTop <= scrollBottom);
  372.         boxIsVisible = Boolean(boxIsVisible);
  373.  
  374.         var reloadBoxAttrs = false;
  375.  
  376.         if (typeof(self.mirrorTop) === 'undefined') {
  377.             reloadBoxAttrs = true;
  378.         };
  379.  
  380.         if (typeof(self.mirrorLeft) === 'undefined') {
  381.             reloadBoxAttrs = true;
  382.         };
  383.  
  384.         if (typeof(self.offsetLeft) === 'undefined') {
  385.             reloadBoxAttrs = true;
  386.         };
  387.  
  388.         if (typeof(self.offsetTop) === 'undefined') {
  389.             reloadBoxAttrs = true;
  390.         };
  391.  
  392.         if (reloadBoxAttrs) {
  393.             this.refresh();
  394.         };
  395.  
  396.         if (boxIsVisible || reloadBoxAttrs) {
  397.             this.visibility = 'visible';
  398.             this.mirrorTop = this.boxOffsetTop - scrollTop;
  399.             this.mirrorLeft = this.boxOffsetLeft - scrollLeft;
  400.             this.offsetTop = 0;
  401.             this.offsetTop = (this.offsetBaseTop - this.mirrorTop);
  402.             this.offsetTop = this.offsetTop * (1 - this.speed);
  403.         } else {
  404.             this.visibility = 'hidden';
  405.         };
  406.  
  407.         if (typeof(this.mirrorLeft) !== 'undefined' && !isNaN(this.mirrorLeft)) {
  408.             mirrorTranslate.push(this.mirrorLeft + 'px');
  409.         };
  410.  
  411.         if (typeof(this.mirrorTop) !== 'undefined' && !isNaN(this.mirrorTop)) {
  412.             var mirrorTop = this.mirrorTop - overScroll;
  413.             if (!isNaN(mirrorTop)) {
  414.                 mirrorTranslate.push(mirrorTop + 'px');
  415.             };
  416.         };
  417.  
  418.         var mirrorTranslateStr = '';
  419.  
  420.         if (mirrorTranslate.length > 1) {
  421.             mirrorTranslateStr = mirrorTranslate.join(', ');
  422.         };
  423.  
  424.         if (typeof(this.offsetLeft) !== 'undefined' && !isNaN(this.offsetLeft)) {
  425.             sliderTranslate.push(this.offsetLeft + 'px');
  426.         };
  427.  
  428.         if (typeof(this.offsetTop) !== 'undefined' && !isNaN(this.offsetTop)) {
  429.             sliderTranslate.push(this.offsetTop + 'px');
  430.         };
  431.  
  432.         var sliderTranslateStr = '';
  433.  
  434.         if (sliderTranslate.length > 1) {
  435.             sliderTranslateStr = sliderTranslate.join(', ');
  436.         };
  437.  
  438.         var renderMirror = true;
  439.  
  440.         // Render the MIRROR for the Parallax
  441.         renderMirror = (Boolean(renderMirror) && Boolean(this.boxHeight > 0));
  442.         renderMirror = (Boolean(renderMirror) && Boolean(this.boxWidth > 0));
  443.         renderMirror = (Boolean(renderMirror) && Boolean(this.imageHeight > 0));
  444.         renderMirror = (Boolean(renderMirror) && Boolean(this.imageWidth > 0));
  445.         renderMirror = (Boolean(renderMirror) && mirrorTranslateStr.length);
  446.         renderMirror = (Boolean(renderMirror) && sliderTranslateStr.length);
  447.         renderMirror = Boolean(renderMirror);
  448.  
  449.         var mirrorCss = {
  450.             transform: mirrorTranslateStr.length && renderMirror ? 'translate3d(' + mirrorTranslateStr + ', 0px)' : '',
  451.             visibility: renderMirror ? this.visibility : 'hidden',
  452.             height: this.boxHeight + 'px',
  453.             width: this.boxWidth + 'px',
  454.             display: 'block',
  455.         };
  456.  
  457.         if (!this.boxIsFullWidth && !this.mirrorIsLeft) {
  458.             mirrorCss['left'] = this.boxLeftOffset + 'px';
  459.         };
  460.  
  461.         var sliderCss = {
  462.             transform: sliderTranslateStr.length && renderMirror ? 'translate3d(' + sliderTranslateStr + ', 0px)' : '',
  463.             visibility: renderMirror ? this.visibility : 'hidden',
  464.             position: 'absolute',
  465.             // display: 'block',
  466.             height: this.imageHeight + 'px',
  467.             width: this.imageWidth + 'px',
  468.             maxWidth: 'none'
  469.         };
  470.  
  471.         if (!sliderTranslateStr.length) {
  472.             console.error("Mirror Translate: mirrorLeft (" + this.mirrorLeft + ") mirrorTop (" + this.mirrorTop + ") overScroll (" + overScroll + ")");
  473.         };
  474.  
  475.         this.$mirror.css(mirrorCss);
  476.  
  477.         if (!sliderTranslateStr.length) {
  478.             console.error("Slider Translate: offsetTop (" + this.offsetTop + ") offsetLeft (" + this.offsetLeft + ")");
  479.         };
  480.  
  481.         this.$slider.css(sliderCss);
  482.  
  483.         // Check if the Image is Transformed, before we strip the background
  484.         var imageIsTransformed = Boolean(true);
  485.         imageIsTransformed = (imageIsTransformed && Boolean(renderMirror));
  486.         imageIsTransformed = (imageIsTransformed && Boolean(this.$mirror.css('transform')));
  487.         imageIsTransformed = (imageIsTransformed && Boolean(this.$slider.css('transform')));
  488.  
  489.         var requestBackgroundRemove = function() {
  490.  
  491.             // Remove the Image Background
  492.             var removeImageBackground = Boolean(true);
  493.             removeImageBackground = (removeImageBackground && Boolean(self.imgIsBackground))
  494.             removeImageBackground = (removeImageBackground && Boolean(self.$element.css('background-image')));
  495.             removeImageBackground = (removeImageBackground && Boolean(imageIsTransformed));
  496.             removeImageBackground = (removeImageBackground && !self.isUnderReview);
  497.  
  498.             // The Image was TransFormed, Clear the CSS Background
  499.             if (removeImageBackground) {
  500.                 self.$element.data('parallax_bg_image', self.$element.css('background-image'));
  501.                 self.$element.css('background-image', '');
  502.             } else if (self.isUnderReview) {
  503.                 self.$element.css('opacity', 0.5);
  504.                 self.$mirror.css('filter', 'grayscale(100%)');
  505.                 self.$mirror.css('-webkit-filter', 'grayscale(100%)');
  506.             };
  507.  
  508.         };
  509.  
  510.         // Is TransFormed
  511.         if (imageIsTransformed) {
  512.             this.$slider.fadeIn('slow', requestBackgroundRemove);
  513.         };
  514.  
  515.     };
  516.  
  517.     // Parallax Instance Methods
  518.  
  519.     $.extend(Parallax.prototype, {
  520.  
  521.         speed: 0.2,
  522.         bleed: 0,
  523.         zIndex: -100,
  524.         iosFix: true,
  525.         androidFix: true,
  526.         position: 'center',
  527.         overScrollFix: false,
  528.         sliderParallaxClasses: '',
  529.         boxElement: false,
  530.         boxWidthElement: false,
  531.         boxHeightElement: false,
  532.         parallaxIsFullWidth: false,
  533.         loadFinished: false,
  534.         mirrorContainer: 'body',
  535.  
  536.         refresh: parallaxRefresh,
  537.         render: parallaxRender,
  538.  
  539.     });
  540.  
  541.  
  542.     // Parallax Static Methods
  543.  
  544.     $.extend(Parallax, {
  545.         scrollTop: 0,
  546.         scrollLeft: 0,
  547.         winHeight: 0,
  548.         winWidth: 0,
  549.         docHeight: 1 << 30,
  550.         docWidth: 1 << 30,
  551.         sliders: [],
  552.         isReady: false,
  553.         isFresh: false,
  554.         isBusy: false,
  555.  
  556.         setup: function() {
  557.  
  558.             if (this.isReady) {
  559.                 return;
  560.             };
  561.  
  562.             var self = this;
  563.  
  564.             var $doc = $(document),
  565.                 $win = $(window);
  566.  
  567.             var loadDimensions = function() {
  568.                 Parallax.winHeight = $win.height();
  569.                 Parallax.winWidth = $win.width();
  570.                 Parallax.docHeight = $doc.height();
  571.                 Parallax.docWidth = $doc.width();
  572.             };
  573.  
  574.             var loadScrollPosition = function() {
  575.                 var winScrollTop = $win.scrollTop();
  576.                 var scrollTopMax = Parallax.docHeight - Parallax.winHeight;
  577.                 var scrollLeftMax = Parallax.docWidth - Parallax.winWidth;
  578.                 Parallax.scrollTop = Math.max(0, Math.min(scrollTopMax, winScrollTop));
  579.                 Parallax.scrollLeft = Math.max(0, Math.min(scrollLeftMax, $win.scrollLeft()));
  580.                 Parallax.overScroll = Math.max(winScrollTop - scrollTopMax, Math.min(winScrollTop, 0));
  581.             };
  582.  
  583.             $win.on('resize.px.parallax load.px.parallax', function() {
  584.                 loadDimensions();
  585.                 self.refresh();
  586.                 Parallax.isFresh = false;
  587.                 Parallax.requestRender();
  588.             });
  589.  
  590.             $win.on('scroll.px.parallax load.px.parallax', function() {
  591.                 loadScrollPosition();
  592.                 Parallax.requestRender();
  593.             });
  594.  
  595.             loadDimensions();
  596.             loadScrollPosition();
  597.  
  598.             this.isReady = true;
  599.  
  600.             var lastPosition = -1;
  601.  
  602.             function frameLoop() {
  603.  
  604.                 if (lastPosition == window.pageYOffset) { // Avoid overcalculations
  605.                     window.requestAnimationFrame(frameLoop);
  606.                     return false;
  607.                 } else {
  608.                     lastPosition = window.pageYOffset;
  609.                 };
  610.  
  611.                 self.render();
  612.                 window.requestAnimationFrame(frameLoop);
  613.             }
  614.  
  615.             frameLoop();
  616.         },
  617.  
  618.         configure: function(options) {
  619.             if (typeof options == 'object') {
  620.                 delete options.refresh;
  621.                 delete options.render;
  622.                 $.extend(this.prototype, options);
  623.             }
  624.         },
  625.  
  626.         refresh: function() {
  627.             $.each(this.sliders, function() {
  628.                 this.refresh();
  629.             });
  630.             this.isFresh = true;
  631.         },
  632.  
  633.         render: function() {
  634.             this.isFresh || this.refresh();
  635.             $.each(this.sliders, function() {
  636.                 this.render();
  637.             });
  638.         },
  639.  
  640.         requestRender: function() {
  641.             var self = this;
  642.             self.isBusy = true;
  643.             self.render();
  644.             self.isBusy = false;
  645.         },
  646.  
  647.         destroy: function(el) {
  648.  
  649.             var i = false;
  650.             var parallaxElement = $(el).data('px.parallax');
  651.             parallaxElement.$mirror.remove();
  652.  
  653.             for (i = 0; i < this.sliders.length; i += 1) {
  654.                 if (this.sliders[i] == parallaxElement) {
  655.                     this.sliders.splice(i, 1);
  656.                 };
  657.             };
  658.  
  659.             $(el).data('px.parallax', false);
  660.  
  661.             if (this.sliders.length === 0) {
  662.                 $(window).off('scroll.px.parallax resize.px.parallax load.px.parallax');
  663.                 this.isReady = false;
  664.                 Parallax.isSetup = false;
  665.             };
  666.  
  667.         }
  668.     });
  669.  
  670.  
  671.     // Parallax Plugin Definition
  672.  
  673.     function Plugin(option) {
  674.  
  675.         return this.each(function() {
  676.             var $this = $(this);
  677.             var options = typeof option == 'object' && option;
  678.  
  679.             if (this == window || this == document || $this.is('body')) {
  680.                 Parallax.configure(options);
  681.             } else if (!$this.data('px.parallax')) {
  682.                 options = $.extend({}, $this.data(), options);
  683.                 $this.data('px.parallax', new Parallax(this, options));
  684.             } else if (typeof option == 'object') {
  685.                 $.extend($this.data('px.parallax'), options);
  686.             };
  687.  
  688.             if (typeof option == 'string') {
  689.                 if (option == 'destroy') {
  690.                     Parallax.destroy(this);
  691.                 } else {
  692.                     Parallax[option]();
  693.                 };
  694.             };
  695.  
  696.         });
  697.     };
  698.  
  699.     var old = $.fn.parallax;
  700.  
  701.     $.fn.parallax = Plugin;
  702.     $.fn.parallax.Constructor = Parallax;
  703.  
  704.     // Parallax No Conflict
  705.  
  706.     $.fn.parallax.noConflict = function() {
  707.         $.fn.parallax = old;
  708.         return this;
  709.     };
  710.  
  711.     // Parallax Data-API
  712.  
  713.     $(function() {
  714.         var parallaxScroll = $('[data-parallax="scroll"]');
  715.         parallaxScroll.parallax();
  716.     });
  717.  
  718. }(jQuery, window, document));
Advertisement
Add Comment
Please, Sign In to add comment