mirror of
https://github.com/readthedocs/sphinx_rtd_theme.git
synced 2025-02-25 18:55:21 -06:00
Merge pull request #492 from jessetan/raf-scroll
Debounce scroll and resize through rAF
This commit is contained in:
commit
3c1a1bbc84
43
js/theme.js
43
js/theme.js
@ -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);
|
||||
};
|
||||
}());
|
||||
|
Loading…
Reference in New Issue
Block a user