Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

javascript - Get Android Chrome Browser Address bar height in JS

How do I get the height of the address bar in JavaScript in the Chrome browser for Android (marked by red rectangle in left picture)? I need to know that as it disappears while scrolling down and I need to react to that because the viewport height is different then.

enter image description here

One solution I already figured out:

  1. Get viewport height at initial state: var height = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);

  2. Get viewport height when the address bar has disappeared

  3. Compute difference between both values

Problem is that you have to be in the second state to know that.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Because 100vh will be larger than the visible height when the URL bar is shown. According to this.

You can calculate the height of the URL bar by creating a 0-width element with 100vh height.

<div id="control-height"></div>
#control-height {
    height: 100vh;
    width: 0;
    position: absolute;
}

Then using javascript compare window.innerHeight with the height of this element.

const actualHeight = window.innerHeight;
const elementHeight = document.getElementById('control-height').clientHeight;

const barHeight = elementHeight - actualHeight;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...