Skip to content

Commit 4f76cda

Browse files
author
Pablo Largo Mohedano
committed
Bypassing requestAnimationFrame removes any delay when fast scrolling.
requestAnimationFrame may be used with better results in a loop in the future
1 parent acc5b83 commit 4f76cda

File tree

1 file changed

+4
-35
lines changed

1 file changed

+4
-35
lines changed

parallax.js

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,6 @@
55
*/
66

77
;(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-
378
// Parallax Constructor
389

3910
function Parallax(element, options) {
@@ -267,7 +238,7 @@
267238
setup: function() {
268239
if (this.isReady) return;
269240

270-
this.lastRequestAnimationFrame = null;
241+
var self = this;
271242

272243
var $doc = $(document), $win = $(window);
273244

@@ -289,6 +260,7 @@
289260

290261
$win.on('resize.px.parallax load.px.parallax', function() {
291262
loadDimensions();
263+
self.refresh();
292264
Parallax.isFresh = false;
293265
Parallax.requestRender();
294266
})
@@ -323,12 +295,9 @@
323295

324296
requestRender: function() {
325297
var self = this;
326-
window.cancelAnimationFrame(self.lastRequestAnimationFrame);
327298

328-
self.lastRequestAnimationFrame = window.requestAnimationFrame(function() {
329-
self.render();
330-
self.isBusy = false;
331-
});
299+
self.render();
300+
self.isBusy = false;
332301
},
333302
destroy: function(el){
334303
var i,

0 commit comments

Comments
 (0)