Skip to content

Instantly share code, notes, and snippets.

@rjurado01
Last active March 9, 2020 18:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rjurado01/533362eb522709c9a514efc1b5bedd44 to your computer and use it in GitHub Desktop.
Save rjurado01/533362eb522709c9a514efc1b5bedd44 to your computer and use it in GitHub Desktop.
const fixScrollToInput = () => {
// resize is not finished (body height must be smaller because keyboard)
if (document.body.offsetHeight == window.bodyHeight) {
setTimeout(fixScrollToInput, 100);
}
// check if element is hidden by keyboard
else if (document.activeElement.getBoundingClientRect().top > document.body.offsetHeight) {
document.activeElement.scrollIntoView({behavior: 'smooth', lock: "center"});
}
}
const calculateBodyHeight = () => {
window.bodyHeight = document.body.offsetHeight;
if (!window.bodyHeight) setTimeout(calculateBodyHeight, 100);
};
document.addEventListener("deviceready", () => {
calculateBodyHeight();
window.addEventListener('keyboardDidShow', fixScrollToInput);
}, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment