Merge pull request #492 from jessetan/raf-scroll

Debounce scroll and resize through rAF
This commit is contained in:
Eric Holscher 2017-12-08 07:24:10 -08:00 committed by GitHub
commit 3c1a1bbc84
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -29,16 +29,21 @@ function ThemeNav () {
// Set scroll monitor
self.win.on('scroll', function () {
if (!self.linkScroll) {
self.winScroll = true;
if (!self.winScroll) {
self.winScroll = true;
requestAnimationFrame(function() { self.onScroll(); });
}
}
});
setInterval(function () { if (self.winScroll) self.onScroll(); }, 25);
// Set resize monitor
self.win.on('resize', function () {
self.winResize = true;
if (!self.winResize) {
self.winResize = true;
requestAnimationFrame(function() { self.onResize(); });
}
});
setInterval(function () { if (self.winResize) self.onResize(); }, 25);
self.onResize();
});
};
@ -166,3 +171,33 @@ module.exports.ThemeNav = ThemeNav();
if (typeof(window) != 'undefined') {
window.SphinxRtdTheme = { StickyNav: module.exports.ThemeNav };
}
// requestAnimationFrame polyfill by Erik Möller. fixes from Paul Irish and Tino Zijdel
// https://gist.github.com/paulirish/1579671
// MIT license
(function() {
var lastTime = 0;
var vendors = ['ms', 'moz', 'webkit', 'o'];
for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame']
|| window[vendors[x]+'CancelRequestAnimationFrame'];
}
if (!window.requestAnimationFrame)
window.requestAnimationFrame = function(callback, element) {
var currTime = new Date().getTime();
var timeToCall = Math.max(0, 16 - (currTime - lastTime));
var id = window.setTimeout(function() { callback(currTime + timeToCall); },
timeToCall);
lastTime = currTime + timeToCall;
return id;
};
if (!window.cancelAnimationFrame)
window.cancelAnimationFrame = function(id) {
clearTimeout(id);
};
}());