Skip to content

window.innerWidth获取可视区域不可靠

在计算当前可视区域宽高的时候,我使用了window.innerWidth和dom.clientWidth,发现这俩的值有时候一样,有时候不一样,尤其是在浏览器审查元素的时候, window.innerWidth的值特别奇怪

Image text

js
    const dom = largeScreenConRef.value
    console.log(dom.clientWidth)
    console.log(window.innerWidth)

所以推荐要获取当前页面的可视区域:

1、可以设定最外层的div宽高为100%,然后通过dom.clientWidth获取

2、window.screen包含可视区域的宽高等信息

3、document.documentElement.clientWidth和document.documentElement.clientHeight

如有转载或 CV 的请标注本站原文地址