Skip to content

Commit 1acfebf

Browse files
committed
Use requestAnimationFrame inside scroll event handler
1 parent f9b83e2 commit 1acfebf

File tree

4 files changed

+66
-47
lines changed

4 files changed

+66
-47
lines changed

jquery.stellar.js

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,18 @@
146146
parts[indexOfNthMatch] = replace;
147147

148148
return parts.join('');
149-
};
149+
},
150+
151+
requestAnimFrame = (function(){
152+
return window.requestAnimationFrame ||
153+
window.webkitRequestAnimationFrame ||
154+
window.mozRequestAnimationFrame ||
155+
window.oRequestAnimationFrame ||
156+
window.msRequestAnimationFrame ||
157+
function(callback, element){
158+
window.setTimeout(callback, 1000 / 60);
159+
};
160+
}());
150161

151162
function Plugin(element, options) {
152163
this.element = element;
@@ -600,21 +611,26 @@
600611
}
601612
},
602613
_handleScrollEvent: function() {
603-
this.$scrollElement.bind('scroll.' + this.name, $.proxy(this._repositionElements, this));
604-
this._repositionElements();
614+
var self = this,
615+
ticking = false;
616+
617+
var update = function() {
618+
self._repositionElements();
619+
ticking = false;
620+
};
621+
622+
var requestTick = function() {
623+
if (!ticking) {
624+
requestAnimFrame(update);
625+
ticking = true;
626+
}
627+
};
628+
629+
this.$scrollElement.bind('scroll.' + this.name, requestTick);
630+
update();
605631
},
606632
_startAnimationLoop: function() {
607-
var self = this,
608-
requestAnimFrame = (function(){
609-
return window.requestAnimationFrame ||
610-
window.webkitRequestAnimationFrame ||
611-
window.mozRequestAnimationFrame ||
612-
window.oRequestAnimationFrame ||
613-
window.msRequestAnimationFrame ||
614-
function(callback, element){
615-
window.setTimeout(callback, 1000 / 60);
616-
};
617-
}());
633+
var self = this;
618634

619635
this._animationLoop = function(){
620636
requestAnimFrame(self._animationLoop);

0 commit comments

Comments
 (0)