Skip to content

Instantly share code, notes, and snippets.

@shuding
Created January 6, 2022 18:01
Show Gist options
  • Save shuding/5cc2f70e2f70727bcca6391c7b406eb4 to your computer and use it in GitHub Desktop.
Save shuding/5cc2f70e2f70727bcca6391c7b406eb4 to your computer and use it in GitHub Desktop.
Modern iOS 100vh Polyfill
if (typeof window !== 'undefined') {
let onResize
if (window.visualViewport) {
onResize = () => {
const vh = window.visualViewport.height
document.documentElement.style.setProperty('--vh', `${vh}px`)
}
window.visualViewport.addEventListener('resize', onResize)
onResize()
} else {
onResize = () => {
const vh = window.innerHeight
document.documentElement.style.setProperty('--vh', `${vh}px`)
}
window.addEventListener('resize', onResize)
onResize()
}
}
// 100vh -> var(--vh)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment