1(function ($) { 2 $(function() { 3 ParallaxScroll.init(); 4}); 5 6var ParallaxScroll = { 7 /* PUBLIC VARIABLES */ 8 showLogs: false, 9 round: 100, 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'], 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 data = $el.data("parallax"); 49 var style = $el.data("style"); 50 if (style == undefined) { 51 style = $el.attr("style") || ""; 52 $el.data("style", style); 53 } 54 var scrollFrom = data["from-scroll"]; 55 if (scrollFrom == undefined) scrollFrom = Math.max(0, $(el).offset().top - windowHeight); 56 scrollFrom = scrollFrom | 0; 57 var scrollDistance = data["distance"]; 58 var scrollTo = data["to-scroll"]; 59 if (scrollDistance == undefined && scrollTo == undefined) scrollDistance = windowHeight; 60 scrollDistance = Math.max(scrollDistance|0,1); 61 if (scrollTo == undefined) scrollTo = scrollFrom + scrollDistance; 62 scrollTo = scrollTo | 0; 63 var smoothness = data["smoothness"]; 64 if (smoothness == undefined) smoothness = 30; 65 smoothness = smoothness | 0; 66 if (noSmooth || smoothness == 0) smoothness = 1; 67 var scrollCurrent = scroll; 68 scrollCurrent = Math.max(scrollCurrent, scrollFrom); 69 scrollCurrent = Math.min(scrollCurrent, scrollTo); 70 var applyProperties = false; 71 this._properties.map($.proxy(function(prop) { 72 var to = data[prop]; 73 if (to == undefined) return; 74 to = to | 0; 75 var prev = $el.data("_" + prop); 76 if (prev == undefined) prev = 0; 77 var next = to * ((scrollCurrent - scrollFrom) / (scrollTo - scrollFrom)); 78 next = Math.floor(next * this.round) / this.round; 79 var val = prev + (next - prev) / smoothness; 80 val = (next > 0 ? Math.ceil : Math.floor)(val * this.round) / this.round; 81 if (prev != val) { 82 properties[prop] = val; 83 $el.data("_" + prop, val); 84 applyProperties = true; 85 } 86 }, this)); 87 if (applyProperties) { 88 if (properties["z"] != undefined) { 89 var perspective = data["perspective"]; 90 if (perspective == undefined) perspective = 800; 91 var $parent = $el.parent(); 92 if(!$parent.data("style")) $parent.data("style", $parent.attr("style") || ""); 93 $parent.attr("style", "perspective:" + perspective + "px; -webkit-perspective:" + perspective + "px; "+ $parent.data("style")); 94 } 95 var translate3d = "translate3d(" + (properties["x"] ? properties["x"] : 0) + "px, " + (properties["y"] ? properties["y"] : 0) + "px, " + (properties["z"] ? properties["z"] : 0) + "px)"; 96 var rotate3d = "rotateX(" + (properties["rotateX"] ? properties["rotateX"] : 0) + "deg) rotateY(" + (properties["rotateY"] ? properties["rotateY"] : 0) + "deg) rotateZ(" + (properties["rotateZ"] ? properties["rotateZ"] : 0) + "deg)"; 97 var cssTransform = translate3d + " " + rotate3d + ";"; 98 this._log(cssTransform); 99 $el.attr("style", "transform:" + cssTransform + " -webkit-transform:" + cssTransform + " " + style); 100 } 101 }, this)); 102 if(window.requestAnimationFrame) { 103 window.requestAnimationFrame($.proxy(this._onScroll, this, false)); 104 } 105 else { 106 this._requestAnimationFrame($.proxy(this._onScroll, this, false)); 107 } 108 } 109} 110}(jQuery));
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.