|
5 | 5 | */
|
6 | 6 |
|
7 | 7 | ;(function ( $, window, document, undefined ) {
|
| 8 | + |
| 9 | + // Polyfill for requestAnimationFrame |
| 10 | + // via: https://gist.github.com/paulirish/1579671 |
| 11 | + |
| 12 | + (function() { |
| 13 | + var lastTime = 0; |
| 14 | + var vendors = ['ms', 'moz', 'webkit', 'o']; |
| 15 | + for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) { |
| 16 | + window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame']; |
| 17 | + window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame'] || window[vendors[x]+'CancelRequestAnimationFrame']; |
| 18 | + } |
| 19 | + |
| 20 | + if (!window.requestAnimationFrame) |
| 21 | + window.requestAnimationFrame = function(callback) { |
| 22 | + var currTime = new Date().getTime(); |
| 23 | + var timeToCall = Math.max(0, 16 - (currTime - lastTime)); |
| 24 | + var id = window.setTimeout(function() { callback(currTime + timeToCall); }, |
| 25 | + timeToCall); |
| 26 | + lastTime = currTime + timeToCall; |
| 27 | + return id; |
| 28 | + }; |
| 29 | + |
| 30 | + if (!window.cancelAnimationFrame) |
| 31 | + window.cancelAnimationFrame = function(id) { |
| 32 | + clearTimeout(id); |
| 33 | + }; |
| 34 | + }()); |
| 35 | + |
| 36 | + |
8 | 37 | // Parallax Constructor
|
9 | 38 |
|
10 | 39 | function Parallax(element, options) {
|
|
273 | 302 | loadScrollPosition();
|
274 | 303 |
|
275 | 304 | this.isReady = true;
|
| 305 | + |
| 306 | + var lastPosition = -1; |
| 307 | + |
| 308 | + function frameLoop() { |
| 309 | + if (lastPosition == window.pageYOffset) { // Avoid overcalculations |
| 310 | + window.requestAnimationFrame(frameLoop); |
| 311 | + return false; |
| 312 | + } else lastPosition = window.pageYOffset; |
| 313 | + |
| 314 | + self.render(); |
| 315 | + window.requestAnimationFrame(frameLoop); |
| 316 | + } |
| 317 | + |
| 318 | + frameLoop(); |
276 | 319 | },
|
277 | 320 |
|
278 | 321 | configure: function(options) {
|
|
295 | 338 |
|
296 | 339 | requestRender: function() {
|
297 | 340 | var self = this;
|
298 |
| - |
299 | 341 | self.render();
|
300 | 342 | self.isBusy = false;
|
301 | 343 | },
|
|
0 commit comments