当前位置: 首页 > 软件库 > 手机/移动开发 > >

vh-check

mobile vh unit utility
授权协议 MIT License
开发语言 Java
所属分类 手机/移动开发
软件类型 开源软件
地区 不详
投 递 者 从阎宝
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

vh-check

get reliable CSS vh sizes for 1kb gzipped

the problem

Browsers don't always compute the vh unit the same way.Some mobile browsers compute the vh CSS unit without taking care of the url bar.That means that a 100vh div will overflow the viewport by the size of the url bar.

This is the current behavior for:

As explained in the chrome post, that make sense but make it hard to have a full hero top block.

This script will measure the difference and put it in a CSS var.You can read more on this css-trick article by Louis Hoebregts

why not use viewport-units-buggyfill?

It's doing a very good job:

https://github.com/rodneyrehm/viewport-units-buggyfill

But it has some problems with media-queries:

https://github.com/rodneyrehm/viewport-units-buggyfill/issues/13

use

as a global variable

<script src="https://unpkg.com/vh-check/dist/vh-check.min.js"></script>
<script>
  (function () {
    // initialize the test
    var test = vhCheck();
  }());
</script>

as a commonJS module

npm install vh-check
var check = require('vh-check')
var test = vhCheck() // return an object (see below)

as a ES module module

npm install vh-check
import vhCheck from 'vh-check'
const test = vhCheck()

how it works

  • It will update the vh-check CSS custom property if needed
  • vh-check will be updated on orientationchange event
  • vh-check will not be updated on scroll event by default

returned object

vh-check will return a full object:

{
  isNeeded: false,
  // wether or not it's needed
  value: 0,
  // the CSS var value
  vh: 480,
  // a 100vh div size
  windowHeight: 480,
  // same value as window.innerHeight
  offset: 0,
  // difference between the above sizes
  recompute: function computeDifference(),
  // call this to programmatically get all the values and set the CSS var
  // - this can be useful if you want to add your own listeners
  //   that will trigger a computation
  unbind: function unbindVhCheckListeners(),
  // call this to remove any window listeners created by vh-check
},

example

in your javascript

vhCheck()

in your CSS

main {
  height: 100vh;
  /* If you need to support browser without CSS var support (<= IE11) */
  height: calc(100vh - var(--vh-offset, 0px));
  /* enable vh fix */
}

configuration

as a string

You can pass the CSS var name as a param to vhCheck() (default vh-offset)

vhCheck('browser-address-bar')

In your CSS you will have to reference:

main {
  min-height: 100vh;
  min-height: calc(100vh - var(--browser-address-bar, 0px));
}

as an object

vh-check allows you to have more control by passing a configuration object.

vhCheck({
  cssVarName: 'vh-offset',
  force: false,
  bind: true,
  redefineVh: false,
  updateOnTouch: false,
  onUpdate: function noop() {},
})

cssVarName

type: string
default: 'vh-offset'

Change the CSS var name

force

type: boolean
default: false

Set the CSS var even if 100vh computation is good

bind

type: boolean
default: true

Automatically bind to orientationchange event

redefineVh

type: boolean
default: false

Change the CSS var value.Instead of being the total size of the gap, it will be 1% of the real window size.
You can find more explanation in this CSS Trick article

⚠️ Important

If you don't set a cssVarName, the CSS custom property will be named vh instead of vh-offset.
So your CSS should be:

.my-element {
  height: 100vh;
  height: calc(var(--vh, 1vh) * 100);
}

updateOnTouch

type: boolean
default: false

Add an event listener on touchmove to recompute the sizes

⚠️ Important

  • This can impact your website performances as changing sizes will make your browser reflow
  • if options.bind is false, this will be ignored as well

onUpdate

type: function
default: function noop(){}

you can specify a callback which will be called with an updated vh-check object every time a computation occurre.

about browser support

This library require requestAnimationFrame which is IE10+You'll need a polyfill if you want to support older browsers

  • vh unit – supported since IE9+
  • calc – supported since IE9+
  • CSS custom properties – supported since IE Edge and iOS 9.3+IE11 & below will need a fallback without CSS var
  • concerned browsers – as for now:
    • Safari since iOS7+
    • Chrome Android >= v56

To sum it up:

Browser Library will work CSS Custom property
<= IE 9
IE 10 & IE 11
IE Edge
< iOS 9.3

demo

github pages

https://hiswe.github.io/vh-check/

local

you'll need node

caveats

On iOS only, Chrome & Firefox will change dynamically the size of 1vh depending on the display of the address bar.
Thus the library will return a not needed value.

If you want to prevent your vh's components to resize, you could fix the size of the unit like this:

vhCheck({
  bind: false,
  redefineVh: true,
})
.my-div {
  height: calc(var(--vh, 1vh) * 100);
}

other stuff

changelog

See CHANGELOG.md

migrating

See MIGRATING.md

run the tests

  • clone the project
  • npm install
  • npm test

thanks

related

  • 开发移动端 当设置100vh时,在各个浏览器计算高度不同,Chrome和Safari浏览器会计算底部tabbar和顶部url地址栏,所以产生比预期高的值, 至此 可借助vh-check来帮助适配各个浏览器的兼容 使用方法 npm install vh-check vue main.js 引入 import vhCheck from "vh-check"; //移动端浏览器100vh高度不一致

  • vminpoly A polyfill for CSS units vw, vh & vmin. Simple online demo right here. A more sophisticated responsive demo demonstrating vw/vh/vmin units along with media queries, working right down to IE5.

  • 起因: 中间内容需要减去顶部内容与底部导航原先使用 100 vh - 245px, 发现pc端浏览器显示正常,移动端除了Chrome与Safari 都显示正常。 定位: 排查得知,这Safari iOS 与Chrome >= 56 中的vh包括了搜索栏的高度。 解决: 采用 vh-check npm i vh-check -s 在vue中main.js引入 import vhCheck from

 相关资料
  • 我想知道如何计算和的值。我正在设计一个网站,使用和它的工作为我的文本完美。然而,我只是猜测。请告诉我这些单元是如何工作的,这样我以后就可以使用它们了。我还想知道他们是否有跨浏览器支持。

  • 问题内容: 我刚刚了解了一个新的不常见的CSS单元。并分别测量视口的高度和宽度百分比。 vw和vh分别是窗口宽度和高度的百分比:100vw是宽度的100%,80vw是80%,依此类推。 这似乎与单位完全相同,这很常见。 在开发人员工具中,我尝试将值从vw / vh更改为%,反之亦然,并得到相同的结果。 两者之间有区别吗?如果没有,为什么要引入这些新单位? 问题答案: 可以达到任何高度。举例来说,如

  • 我刚刚了解了一个新的不常见的CSS单元。和分别测量视口的高度和宽度的百分比。 我从Stack Overflow看这个问题,但它使单元看起来更加相似。 vw和vh单元是如何工作的 答案特别说 vw和vh分别是窗口宽度和高度的一个百分比:100vw是宽度的100%,80vw是80%等。 这似乎与单元完全相同,后者更为常见。 在Developer Tools中,我尝试将vw/vh的值更改为%,反之亦然,

  • 我已经使用SOF很长时间了,但从来没有问过一个问题。今天我在Firefox的网站上,注意到它谈到了px和%就好像它已经过时了。该网站建议使用ViewHeight和view width或view像素(vh和vi或vpx)。用... 而且 ..????他们为什么要做出改变?

  • 我将div设置为父容器 我在css文件中设置了100vh 我通过css中的背景色属性对其进行着色 我的问题是,这是一种有效的方法吗?有什么优点和缺点吗?还有你建议的其他方法。我只想给网页的背景上色。例如,Home组件,而不是其他组件,仅此一个。 如果不是,请用正确的方式建议我吗?

  • 问题内容: 我正在使用100vh将div以行高垂直居中。该网站对vh和vw的支持率约为70%,这是一个公平的评估吗?建立网站时,您是否建议使用视口单位?我知道这有点主观,我只是在寻求比我更有经验的Web开发人员的意见。 编辑:感谢大家的投入,我希望它在移动设备上看起来不错,所以我想我不得不放弃vh。 问题答案: 在我看来,该统计数据显然是很公平的评估。 我认为必须由您来决定。如果您想使用最新的,最