My chat app has sticky header and footer elements. To handle the mobile virtual keyboard opening, I adjust the document's height using window.visualViewport.height
. For example, if the browser's height is 1000px and the virtual keyboard takes up 400px, then window.visualViewport.height
will be 600px, setting both <html>
and <body>
to that height. This allows the header and footer to display correctly within the 600px viewport.
However, there is an issue where users can still scroll the entire page up by 400px, revealing empty space at the bottom. How can I prevent this empty space from showing while maintaining the sticky header/footer functionality?
I've attempted various solutions:
- Listening for scroll events, but they are not triggered
- Using an IntersectionObserver on an element below the viewport, which never triggers
- Applying
position: fixed
to the footer, but it does not remain fixed when scrolling - The document's scroll Y position always remains at 0
- Setting
has no effectnavigator.virtualKeyboard.overlaysContent
This issue persists in Android + Chrome, as well as iOS + Safari.
View a video demonstration showcasing the initial white space caused by the keyboard:
Edit
I discovered a workaround involving
window.visualViewport.addEventListener('scroll', ...)
. On scroll, I dynamically add padding to the top of the page matching window.visualViewport.offsetTop
. However, there is some delay where users can scroll before the scroll handler kicks in. In iOS Safari, this delay can exceed 1 second. Is there a more effective solution to address this issue?