I am building an editor using editorJS. For PC, I created a sticky toolbar at the top which follows the viewport scroll. However, for mobile, the stikcy toolbar just won't get fixed on top of the viewport because of the damn virtual keyboard.
So I decided to remove the position: sticky; and apply position: fixed; bottom: 0; for mobile screen so that it sticks on top of the keyboard. I thought I would be able to calculate the right position for the toolbar in the resize and scroll event handlers. The procedures are as follows.
- if
resizehappens, that means either the virtual keyboard is up/down or the browser header/footer is shown/disappeared by the scroll. - debounce the
resizeevent so to store thevisualViewport.heightbefore the resize and after theresize. - when
resizeevent ends, compare the thevisualViewport.heightbefore and after and set the difference as thestyle.bottomof the toolbar. - if
scrollhappens,visibility: hidden;the toolbar and calculate the position when the event ends. (also debouncing thescrollevent) visiblity: visiblethe toolbar to show it on top of the keyboard.
But, I was not able to achieve it because the [2] did not work. The resize event did not catch the visualViewport.height before the resize even with the debounce.
handleIOSKeyboardAppear(event) {
if (!this.isMobileResizing) {
console.log('start!', window.visualViewport?.height) <-- the same value
this.isMobileResizing = true
}
console.log('resizing...')
if (this.mobileResizeDebounceTimerId) {
clearTimeout(this.mobileResizeDebounceTimerId)
}
this.mobileResizeDebounceTimerId = setTimeout(() => {
console.log('end!', window.visualViewport?.height) <-- the same value
this.isMobileResizing = false
}, 500)
I found my own solution to this. Basically re-calculating the style.bottom every time the resize, scroll event happens.
the value of the style.bottom:
window.innerHeight - window.visualViewport.offsetTop - window.visualViewport.height