Lightweight JS package for easy overcoming the problem with 100vh on mobile devices 📱
It creates the CSS variable called --vh
at the <html>
tag and vh
variable in the window
, which is changed only on orientation change and stays still during scroll. This will prevent the page "jumping" on scroll, which leads to bad user experience.
This will stay still only on mobile devices, on desktop the value changes together with the viewport. That means that it can be used on any resolution and there is no need for additional css media queries.
Install package in your project
npm i @sparing-software/100vh
Import the package and run the init function at the page start.
(for Nuxt.js/Vue app it can be initialized in plugins, if you're using other technology – init the script above all the other code)
import vh from '@sparing-software/100vh'
vh.init()
CSS
.section {
height: var(--vh);
}
JS
window.querySelectorAll('section').style.height = window.vh
Want to help improve this plugin? Great!
Project is open-source so fork repo and join us!
MIT License © Sparing Interactive