I have a chat app with sticky header and footer elements. When the mobile virtual keyboard is open, I set the document's height to window.visualViewport.height
. E.g. if the browser's height is 1000px and the virtual keyboard is 400px, then window.visualViewport.height
is 600px and both <html>
and <body>
are set to 600px. The header and footer would both appear correctly within the 600px viewport.
However, users can still scroll the whole page up by 400px, meaning they'll see 400px of empty space at the bottom. How can I prevent this empty space from showing up while still having the sticky header/footer?
I tried:
- Detecting scroll events, but they aren't fired
- Using an IntersectionObserver on an element below the 600px viewport, but it never triggers
- Using
position: fixed
on the footer, but it doesn't stick to the bottom when scrolling - The document's scroll Y position is always 0
- Setting
navigator.virtualKeyboard.overlaysContent
doesn't do anything
Mostly tested in Android + Chrome, but same issue occurs in iOS + Safari.
Video demo, the initial bottom white space is the keyboard: https://i.imgur.com/OMSXAAt.mp4
Edit for bounty:
I found a hacky solution using window.visualViewport.addEventListener('scroll', ...)
. On scroll, I'd add some padding to the top of the page equal to window.visualViewport.offsetTop
. However, there's some lag, so users can scroll a little, then the scroll handler runs. On iOS Safari, the lag can be more than 1 second. Is there a better solution?