1/** 2 * Parallax 3 * 4 * Translate3d 5 * 1.0 | Muffin Group 6 */ 7 8var mfnSetup = { 9 translate: null 10}; 11 12(function($) { 13 14 /* globals jQuery */ 15 16 "use strict"; 17 18 /** 19 * mfnSetup 20 */ 21 22 // has3d 23 24 var has3d = function() { 25 26 if (!window.getComputedStyle) { 27 return false; 28 } 29 30 var el = document.createElement('div'), 31 has3d; 32 33 document.body.insertBefore(el, null); 34 35 if (el.style.transform !== undefined) { 36 //el.style.transform = "translate3d(1px,1px,1px)"; 37 el.style.transform = "translateY(0px) translateX(-50%)"; 38 has3d = window.getComputedStyle(el).getPropertyValue('transform'); 39 } 40 41 document.body.removeChild(el); 42 43 return (has3d !== undefined && has3d !== null && has3d.length > 0 && has3d !== "none"); 44 }; 45 46 // __construct 47 48 var __construct = function() { 49 50 if (has3d()) { 51 52 mfnSetup.translate = function(el, x, y) { 53 //el.css('transform', 'translate3d(' + x + ', ' + y + ', 0)'); 54 el.css('transform', 'translateY(' + y + ') translateX(-50%)'); 55 }; 56 57 } else { 58 59 /*mfnSetup.translate = function(el, x, y) { 60 el.css({ 61 "left": x, 62 "top": y 63 }); 64 };*/ 65 66 } 67 }; 68 69 __construct(); 70 71})(jQuery); 72 73(function($) { 74 75 "use strict"; 76 77 /** 78 * $.fn.mfnParallax 79 */ 80 81 $.fn.mfnParallax = function() { 82 83 var el = $(this), 84 parent = el.parent(), 85 speed = 500, 86 element, parentPos, windowH; 87 88 // imageSize 89 90 var imageSize = function(img) { 91 92 var w, h, l, t; // width, height, left, top 93 94 var imageW = img.get(0).clientWidth; 95 var imageH = img.get(0).clientHeight; 96 97 var parentW = img.parent().outerWidth(); 98 var parentH = img.parent().outerHeight(); 99 100 var windowH = $(window).height(); 101 102 // fix for small sections 103 if (windowH > parentH) { 104 parentH = windowH; 105 } 106 107 var diff = imageW / parentW; 108 109 if ((imageH / diff) < parentH) { 110 111 w = imageW / (imageH / parentH); 112 h = parentH; 113 114 if (w > imageW) { 115 116 w = imageW; 117 h = imageH; 118 119 } 120 121 } else { 122 123 w = parentW; 124 h = imageH / diff; 125 126 } 127 128 l = (parentW - w) / 2; 129 t = (parentH - h) / 2; 130 131 return [w, h, l, t]; 132 }; 133 134 // parallax 135 136 var parallax = function() { 137 138 var scrollTop = $(window).scrollTop(), 139 scrollDiff, ratio, translateTop; 140 141 if (parentPos !== undefined) { 142 143 if (scrollTop >= parentPos.min && scrollTop <= parentPos.max) { 144 145 scrollDiff = scrollTop - parentPos.min; 146 ratio = scrollDiff / parentPos.height; 147 148 translateTop = windowH + (ratio * speed) - scrollDiff - (speed * (windowH / parentPos.height)); 149 150 mfnSetup.translate(el, element.left + "px", -Math.round(translateTop) + "px"); 151 } 152 153 } 154 }; 155 156 // init 157 158 var init = function() { 159 160 windowH = $(window).height(); 161 162 var initElement = function() { 163 164 var size = imageSize(el); 165 166 el.removeAttr('style').css({ 167 /*'width': size[0], 168 'height': size[1]*/ 169 }); 170 171 mfnSetup.translate(el, size[2] + "px", size[3] + "px"); 172 173 return { 174 'width': size[0], 175 'height': size[1], 176 'left': size[2], 177 'top': size[3] 178 }; 179 }; 180 181 element = initElement(); 182 183 var initParent = function() { 184 185 var min = parent.offset().top - $(window).height(); 186 var max = parent.offset().top + $(parent).outerHeight(); 187 188 return { 189 'min': min, 190 'max': max, 191 'height': max - min 192 }; 193 }; 194 195 parentPos = initParent(); 196 197 // hide section background image 198 parent.css('background-image','unset'); 199 }; 200 201 // reload 202 203 var reload = function() { 204 205 setTimeout(function() { 206 init(); 207 parallax(); 208 }, 50); 209 210 }; 211 212 reload(); 213 214 /** 215 * $(window).scroll 216 */ 217 218 $(window).on('scroll', parallax); 219 220 }; 221 222 /** 223 * $(window).load 224 */ 225 226 $(window).on('load resize', function(){ 227 228 if ($(".mfn-parallax").length) { 229 230 $(".mfn-parallax").each(function() { 231 $(this).mfnParallax(); 232 }); 233 234 } 235 236 }); 237 238})(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.