Advertisement
2sandaljepit

jquery.parallax-scroll.js

Sep 19th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $(function() {
  2.     console.log('initiated');
  3.     ParallaxScroll.init();
  4. });
  5.  
  6. var ParallaxScroll = {
  7.     /* PUBLIC VARIABLES */
  8.     showLogs: false,
  9.     round: 1000,
  10.  
  11.     /* PUBLIC FUNCTIONS */
  12.     init: function() {
  13.         this._log("init");
  14.         if (this._inited) {
  15.             this._log("Already Inited");
  16.             this._inited = true;
  17.             return;
  18.         }
  19.         this._requestAnimationFrame = (function(){
  20.           return  window.requestAnimationFrame       ||
  21.                   window.webkitRequestAnimationFrame ||
  22.                   window.mozRequestAnimationFrame    ||
  23.                   window.oRequestAnimationFrame      ||
  24.                   window.msRequestAnimationFrame     ||
  25.                   function(/* function */ callback, /* DOMElement */ element){
  26.                       window.setTimeout(callback, 1000 / 60);
  27.                   };
  28.         })();
  29.         this._onScroll(true);
  30.     },
  31.  
  32.     /* PRIVATE VARIABLES */
  33.     _inited: false,
  34.     _properties: ['x', 'y', 'z', 'rotateX', 'rotateY', 'rotateZ', 'scaleX', 'scaleY', 'scaleZ', 'scale'],
  35.     _requestAnimationFrame:null,
  36.  
  37.     /* PRIVATE FUNCTIONS */
  38.     _log: function(message) {
  39.         if (this.showLogs) console.log("Parallax Scroll / " + message);
  40.     },
  41.     _onScroll: function(noSmooth) {
  42.         var scroll = $(document).scrollTop();
  43.         var windowHeight = $(window).height();
  44.         this._log("onScroll " + scroll);
  45.         $("[data-parallax]").each($.proxy(function(index, el) {
  46.             var $el = $(el);
  47.             var properties = [];
  48.             var applyProperties = false;
  49.             var style = $el.data("style");
  50.             if (style == undefined) {
  51.                 style = $el.attr("style") || "";
  52.                 $el.data("style", style);
  53.             }
  54.             var datas = [$el.data("parallax")];
  55.             var iData;
  56.             for(iData = 2; ; iData++) {
  57.                 if($el.data("parallax"+iData)) {
  58.                     datas.push($el.data("parallax"+iData));
  59.                 }
  60.                 else {
  61.                     break;
  62.                 }
  63.             }
  64.             var datasLength = datas.length;
  65.             for(iData = 0; iData < datasLength; iData ++) {
  66.                 var data = datas[iData];
  67.                 var scrollFrom = data["from-scroll"];
  68.                 if (scrollFrom == undefined) scrollFrom = Math.max(0, $(el).offset().top - windowHeight);
  69.                 scrollFrom = scrollFrom | 0;
  70.                 var scrollDistance = data["distance"];
  71.                 var scrollTo = data["to-scroll"];
  72.                 if (scrollDistance == undefined && scrollTo == undefined) scrollDistance = windowHeight;
  73.                 scrollDistance = Math.max(scrollDistance | 0, 1);
  74.                 var easing = data["easing"];
  75.                 var easingReturn = data["easing-return"];
  76.                 if (easing == undefined || !$.easing|| !$.easing[easing]) easing = null;
  77.                 if (easingReturn == undefined || !$.easing|| !$.easing[easingReturn]) easingReturn = easing;
  78.                 if (easing) {
  79.                     var totalTime = data["duration"];
  80.                     if (totalTime == undefined) totalTime = scrollDistance;
  81.                     totalTime = Math.max(totalTime | 0, 1);
  82.                     var totalTimeReturn = data["duration-return"];
  83.                     if (totalTimeReturn == undefined) totalTimeReturn = totalTime;
  84.                     scrollDistance = 1;
  85.                     var currentTime = $el.data("current-time");
  86.                     if(currentTime == undefined) currentTime = 0;
  87.                 }
  88.                 if (scrollTo == undefined) scrollTo = scrollFrom + scrollDistance;
  89.                 scrollTo = scrollTo | 0;
  90.                 var smoothness = data["smoothness"];
  91.                 if (smoothness == undefined) smoothness = 30;
  92.                 smoothness = smoothness | 0;
  93.                 if (noSmooth || smoothness == 0) smoothness = 1;
  94.                 smoothness = smoothness | 0;
  95.                 var scrollCurrent = scroll;
  96.                 scrollCurrent = Math.max(scrollCurrent, scrollFrom);
  97.                 scrollCurrent = Math.min(scrollCurrent, scrollTo);
  98.                 if(easing) {
  99.                     if($el.data("sens") == undefined) $el.data("sens", "back");
  100.                     if(scrollCurrent>scrollFrom) {
  101.                         if($el.data("sens") == "back") {
  102.                             currentTime = 1;
  103.                             $el.data("sens", "go");
  104.                         }
  105.                         else {
  106.                             currentTime++;
  107.                         }
  108.                     }
  109.                     if(scrollCurrent<scrollTo) {
  110.                         if($el.data("sens") == "go") {
  111.                             currentTime = 1;
  112.                             $el.data("sens", "back");
  113.                         }
  114.                         else {
  115.                             currentTime++;
  116.                         }
  117.                     }
  118.                     if(noSmooth) currentTime = totalTime;
  119.                     $el.data("current-time", currentTime);
  120.                 }
  121.                 this._properties.map($.proxy(function(prop) {
  122.                     var defaultProp = 0;
  123.                     var to = data[prop];
  124.                     if (to == undefined) return;
  125.                     if(prop=="scale" || prop=="scaleX" || prop=="scaleY" || prop=="scaleZ" ) {
  126.                         defaultProp = 1;
  127.                     }
  128.                     else {
  129.                         to = to | 0;
  130.                     }
  131.                     var prev = $el.data("_" + prop);
  132.                     if (prev == undefined) prev = defaultProp;
  133.                     var next = ((to-defaultProp) * ((scrollCurrent - scrollFrom) / (scrollTo - scrollFrom))) + defaultProp;
  134.                     var val = prev + (next - prev) / smoothness;
  135.                     if(easing && currentTime>0 && currentTime<=totalTime) {
  136.                         var from = defaultProp;
  137.                         if($el.data("sens") == "back") {
  138.                             from = to;
  139.                             to = -to;
  140.                             easing = easingReturn;
  141.                             totalTime = totalTimeReturn;
  142.                         }
  143.                         val = $.easing[easing](null, currentTime, from, to, totalTime);
  144.                     }
  145.                     val = Math.ceil(val * this.round) / this.round;
  146.                     if(val==prev&&next==to) val = to;
  147.                     if(!properties[prop]) properties[prop] = 0;
  148.                     properties[prop] += val;
  149.                     if (prev != properties[prop]) {
  150.                         $el.data("_" + prop, properties[prop]);
  151.                         applyProperties = true;
  152.                     }
  153.                 }, this));
  154.             }
  155.             if (applyProperties) {
  156.                 if (properties["z"] != undefined) {
  157.                     var perspective = data["perspective"];
  158.                     if (perspective == undefined) perspective = 800;
  159.                     var $parent = $el.parent();
  160.                     if(!$parent.data("style")) $parent.data("style", $parent.attr("style") || "");
  161.                     $parent.attr("style", "perspective:" + perspective + "px; -webkit-perspective:" + perspective + "px; "+ $parent.data("style"));
  162.                 }
  163.                 if(properties["scaleX"] == undefined) properties["scaleX"] = 1;
  164.                 if(properties["scaleY"] == undefined) properties["scaleY"] = 1;
  165.                 if(properties["scaleZ"] == undefined) properties["scaleZ"] = 1;
  166.                 if (properties["scale"] != undefined) {
  167.                     properties["scaleX"] *= properties["scale"];
  168.                     properties["scaleY"] *= properties["scale"];
  169.                     properties["scaleZ"] *= properties["scale"];
  170.                 }
  171.                 var translate3d = "translate3d(" + (properties["x"] ? properties["x"] : 0) + "px, " + (properties["y"] ? properties["y"] : 0) + "px, " + (properties["z"] ? properties["z"] : 0) + "px)";
  172.                 var rotate3d = "rotateX(" + (properties["rotateX"] ? properties["rotateX"] : 0) + "deg) rotateY(" + (properties["rotateY"] ? properties["rotateY"] : 0) + "deg) rotateZ(" + (properties["rotateZ"] ? properties["rotateZ"] : 0) + "deg)";
  173.                 var scale3d = "scaleX(" + properties["scaleX"] + ") scaleY(" + properties["scaleY"] + ") scaleZ(" + properties["scaleZ"] + ")";
  174.                 var cssTransform = translate3d + " " + rotate3d + " " + scale3d + ";";
  175.                 this._log(cssTransform);
  176.                 $el.attr("style", "transform:" + cssTransform + " -webkit-transform:" + cssTransform + " " + style);
  177.             }
  178.         }, this));
  179.         if(window.requestAnimationFrame) {
  180.             window.requestAnimationFrame($.proxy(this._onScroll, this, false));
  181.         }
  182.         else {
  183.             this._requestAnimationFrame($.proxy(this._onScroll, this, false));
  184.         }
  185.     }
  186. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement