21 Jan 2017

This article will take about 2 minutes to read.
6 Comments

If you ever used vh units and tested your work on mobile Chrome (iOS and Android), you probably were annoyed by page jumping when you scroll.

As you probably know vh units are based on the window height. When you scroll, Chrome’s address bar disappears and chrome actually changes window height. Also triggers window resize event. So when it does happen it changes the value of vh unit, making your elements resize, and page jump. On Android, keyboard toggle will do the same.

Well I made a plain JavaScript library to solve this problem.

Demo and documentation are available here.

How does it work?

On load, it will get the element and set them fixed height in pixels, using this formula:

window height / 100 * given vh value

Library listens for the window resize event, and only if both dimensions are changed (which on mobile means orientation has changed*) it will recalculate and apply fixed height based on new window height.

Library will only do this if it detects Chrome on Android or iOS.

* Android Chrome has a bug, when keyboard pops up, it triggers orientation change.

Usage

It accepts one parameter, which is an array of objects. Every object should have CSS selector and height in vh units. All elements that match given selector will be fixed.

var options = [
  {
    selector: '.Bears', // Mandatory, CSS selector
    vh: 150,  // Mandatory, height in vh units
  },
  {
    selector: '.Foxes',
    vh: 50
  },
  {
    selector: '.Horses',
    vh: 100
  }
];

var vhFix = new VHChromeFix(options);

React Component is WIP

I have a React component already done in a another project, but I need to extract it, and pack it up as a separate npm package for easier usage.

Grab the code on GitHub.

Category
CSS/SASS

Comments (6)

Adam
30 Jan 2017, 09:35 AM

Hi,

Can't see any mention of your vh-fix license in GitHub. Technically this means "all rights reserved". Is this accurate, or is the license just missing?

Many Thanks,

- Adam

Stanko
30 Jan 2017, 09:42 AM

Hi Adam,

It is missing, thanks for pointing it out. I just added MIT license to the repository.

Cheers!

brandon
16 Aug 2017, 05:10 AM

Very awesome! thanks for this, you saved my project!

David
30 Aug 2017, 12:10 AM

This is a very novel way of going about this rather annoying issue. Has there been any progress in turning this into a React component?

Stanko
30 Aug 2017, 08:38 AM

Thanks guys!

@David, I have working React component and I plan to release it on the npm. But it depends on some other decorators and I'm thinking of the best way to separate them before publishing.

Cheers!

David
30 Aug 2017, 11:45 PM

Makes sense. I'll keep an eye out for it.

Leave a comment

Sending failed, please try again.
Thank you! Your comment is sent. Please note that all of the comments go through moderation.