Fix Safari over-scroll bug, fixes #218

Protect against negative scroll values or scroll values past the end of the
document. Stop scrolling if this is the case.
This commit is contained in:
Anthony Johnson 2015-07-17 15:48:27 -07:00
parent 6277c21110
commit 9f908fff9c

View File

@ -49,29 +49,30 @@ window.SphinxRtdTheme = (function (jquery) {
var navBar, var navBar,
win, win,
winScroll = false, winScroll = false,
winResize = false,
linkScroll = false, linkScroll = false,
winPosition = 0, winPosition = 0,
winHeight,
docHeight,
enable = function () { enable = function () {
init(); init();
reset(); reset();
win.on('hashchange', reset); win.on('hashchange', reset);
// Set scrolling // Set scroll monitor
win.on('scroll', function () { win.on('scroll', function () {
if (!linkScroll) { if (!linkScroll) {
winScroll = true; winScroll = true;
} }
}); });
setInterval(function () { setInterval(function () { if (winScroll) scroll(); }, 25);
if (winScroll) {
winScroll = false; // Set resize monitor
var newWinPosition = win.scrollTop(), win.on('resize', function () {
navPosition = navBar.scrollTop(), winResize = true;
newNavPosition = navPosition + (newWinPosition - winPosition); });
navBar.scrollTop(newNavPosition); setInterval(function () { if (winResize) resize(); }, 25);
winPosition = newWinPosition; resize();
}
}, 25);
}, },
init = function () { init = function () {
navBar = jquery('nav.wy-nav-side:first'); navBar = jquery('nav.wy-nav-side:first');
@ -95,6 +96,23 @@ window.SphinxRtdTheme = (function (jquery) {
} }
} }
}, },
scroll = function () {
winScroll = false;
var newWinPosition = win.scrollTop(),
winBottom = newWinPosition + winHeight,
navPosition = navBar.scrollTop(),
newNavPosition = navPosition + (newWinPosition - winPosition);
if (newWinPosition < 0 || winBottom > docHeight) {
return;
}
navBar.scrollTop(newNavPosition);
winPosition = newWinPosition;
},
resize = function () {
winResize = false;
winHeight = win.height();
docHeight = $(document).height();
},
hashChange = function () { hashChange = function () {
linkScroll = true; linkScroll = true;
win.one('hashchange', function () { win.one('hashchange', function () {