简体   繁体   English

如何获取浏览器视口的小数宽度和高度?

[英]How to get the fractional width and height of the browser viewport?

I'm not talking about getting a rounded size, I need to get the fractional one.我不是在谈论获得四舍五入的大小,我需要获得分数。

An only way I know is to create a new div element with the position: fixed; top: 0; right: 0, bottom: 0, left: 0我知道的唯一方法是使用position: fixed; top: 0; right: 0, bottom: 0, left: 0创建一个新的div元素:已修复; position: fixed; top: 0; right: 0, bottom: 0, left: 0 position: fixed; top: 0; right: 0, bottom: 0, left: 0 properties set and append it to the body element after it's done, I can get a fractional size with getBoundingClientRect . position: fixed; top: 0; right: 0, bottom: 0, left: 0属性设置和 append 完成后它到body元素,我可以使用getBoundingClientRect获得分数大小。

But it's slow, it's very slow.但是它很慢,非常慢。 This approach stops the rest JS code from being executed until the DOM manipulations and calculations are done.这种方法会停止执行 rest JS 代码,直到 DOM 操作和计算完成。

Is there a way of doing this without an expensive approach?有没有一种不用昂贵的方法就可以做到这一点的方法?

You could try using你可以尝试使用

let viewPortHeight = window.innerHeight;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM