Advertisement
Guest User

paralaz.js

a guest
Apr 1st, 2015
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var ParallaxManager, ParallaxPart;
  2.  
  3. ParallaxPart = (function() {
  4.   function ParallaxPart(el) {
  5.     this.el = el;
  6.     this.speed = parseFloat(this.el.getAttribute('data-parallax-speed'));
  7.     this.maxScroll = parseInt(this.el.getAttribute('data-max-scroll'));
  8.   }
  9.  
  10.   ParallaxPart.prototype.update = function(scrollY) {
  11.    // if (scrollY > this.maxScroll) { return; }
  12.       var offset = 0;
  13.       if($(window).scrollTop() + $(window).height() == $(document).height()) {
  14.          
  15.        }else{
  16.            var offset = -(scrollY * this.speed);
  17.        }
  18.     var offset = (scrollY * this.speed);
  19.     this.setYTransform(offset);
  20.   };
  21.  
  22.   ParallaxPart.prototype.setYTransform = function(val) {
  23.     this.el.style.webkitTransform = "translate3d(0, " + val + "px, 0)";
  24.     this.el.style.MozTransform    = "translate3d(0, " + val + "px, 0)";
  25.     this.el.style.OTransform      = "translate3d(0, " + val + "px, 0)";
  26.     this.el.style.transform       = "translate3d(0, " + val + "px, 0)";
  27.     this.el.style.msTransform     = "translateY(" + val + "px)";
  28.   };
  29.  
  30.   return ParallaxPart;
  31.  
  32. })();
  33.  
  34. ParallaxManager = (function() {
  35.   ParallaxManager.prototype.parts = [];
  36.  
  37.   function ParallaxManager(elements) {
  38.     if (typeof elements === 'array' && elements.length) {
  39.       this.elements = elements;
  40.     }
  41.     if (typeof elements === 'object' && elements.item) {
  42.       this.elements = Array.prototype.slice.call(elements);
  43.     } else if (typeof elements === 'string') {
  44.       this.elements = document.querySelectorAll(elements);
  45.       if (this.elements.length === 0) {
  46.         throw new Error("Parallax: No elements found");
  47.       }
  48.       this.elements = Array.prototype.slice.call(this.elements);
  49.     } else {
  50.       throw new Error("Parallax: Element variable is not a querySelector string, Array, or NodeList");
  51.     }
  52.     for (var i in this.elements) {
  53.       this.parts.push(new ParallaxPart(this.elements[i]));
  54.     }
  55.     window.addEventListener("scroll", this.onScroll.bind(this));
  56.   }
  57.  
  58.   ParallaxManager.prototype.onScroll = function() {
  59.     window.requestAnimationFrame(this.scrollHandler.bind(this));
  60.   };
  61.  
  62.   ParallaxManager.prototype.scrollHandler = function() {
  63.      
  64.       //var scrollY =  Math.max(window.pageYOffset,0);
  65.      
  66.       //console.log("window height");
  67.       //console.log($(window).height());
  68.      
  69.      
  70.       var scrollY = $(document).height()-($(window).scrollTop() + $(window).height());
  71.       //console.log("scroll amount");
  72.       //console.log(scrollY);
  73.       //console.log($(window).scrollTop());
  74.     //var scrollY = Math.max(window.pageYOffset, 0);
  75.     for (var i in this.parts) { this.parts[i].update(scrollY); }
  76.   };
  77.  
  78.   return ParallaxManager;
  79.  
  80. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement