I was unable to fix the issue, so as a workaround I wrote a function that returns a promise. This promise is resolved when elastic scroll is finished.
When user clicks “Next Article”, I load the article but I wait until promise is resolved before I perform DOM operations and call scrollTo
. This works well for me.
function waitForElasticScroll() {
// If user scrolls part viewport and clicks while elastic scroll
// has not fully "returned", scroll position will get messed up in Chrome.
// Our workaround is to wait for elastic scroll to finish before transitioning.
// rAF calls help avoid unnecessary layout.
return new Promise(function (resolve) {
window.requestAnimationFrame(function () {
var maxScrollY = document.body.scrollHeight - window.innerHeight;
function isReady() {
var scrollY = window.scrollY || window.pageYOffset;
return scrollY <= maxScrollY;
}
function resolveIfReady() {
if (isReady()) {
resolve();
} else {
window.requestAnimationFrame(resolveIfReady);
}
}
resolveIfReady();
});
});
}
I'm using Bluebird for Promises and animation-frame for rAF polyfill.