Get Android Chrome Browser Address bar height in JS

后端 未结 5 1954
忘掉有多难
忘掉有多难 2020-12-18 04:39

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 wh

5条回答
  •  失恋的感觉
    2020-12-18 05:17

    Had the same issue today, turns out there is no easy way to figure out the height of the url bar directly. As far as I know, none of the directly accessible variables in javascript can tell you how much the size of "100vh" really is.

    On mobile browsers, 100vh may or may not include the height of the url bar, which leaves us in a tricky situation, if we want to size a div to the exact height of the visible content area of the browser during load.

    I figured out a workaround though that worked pretty neat for me, here's what I did:

    1. add a dummy property on your html root element with a size of 100vh. In my case, i used the "perspective" attribute, which worked for me
    2. then you can get the address bar size with the following code:

      var addressBarSize = parseFloat(getComputedStyle(document.documentElement).perspective) - document.documentElement.clientHeight
      

提交回复
热议问题