Scroll element into view if it's been moved off the screen

This commit is contained in:
David Rowe 2018-05-05 12:03:18 +12:00
parent 03da2f0946
commit 1be948ee28

View file

@ -45,12 +45,15 @@
function scheduleBringToView(timeout) {
setTimeout(function () {
// If the element is not visible because the keyboard has been raised over the top of it, scroll it into view.
// If the element is not visible because the keyboard has been raised over the top of it, scroll it up into view.
// If the element is not visible because the keyboard raising has moved it off screen, scroll it down into view.
var elementRect = document.activeElement.getBoundingClientRect();
var VISUAL_MARGIN = 3
var delta = elementRect.y + elementRect.height + VISUAL_MARGIN - window.innerHeight;
if (delta > 0) {
window.scrollBy(0, delta);
} else if (elementRect.y < VISUAL_MARGIN) {
window.scrollBy(0, elementRect.y - VISUAL_MARGIN);
}
}, timeout);
}