如何在JS中获取CSS vw和vh?
我正在尝试在JavaScript中使用CSS VW和VH值,因为我想使它们适应更改某些JS变量。但是,在研究了不同的方法之后,这些值似乎已经略有一点,因为它弄乱了我的显示。我已经尝试使用:
viewPortWidth = window.innerWidth;
viewPortHeight = window.innerHeight;
但是
viewPortWidth = document.documentElement.clientWidth;
viewPortHeight = document.documentElement.clientHeight;
它们似乎没有起作用。
有人有想法吗?任何帮助都非常感谢!
谢谢!
I am trying to use the CSS vw and vh values in Javascript because I would like to make them adaptive to changing certain JS variables. However, after researching different methods, the values seem to be off by a little bit because it is messing up my display. I've tried using:
viewPortWidth = window.innerWidth;
viewPortHeight = window.innerHeight;
and
viewPortWidth = document.documentElement.clientWidth;
viewPortHeight = document.documentElement.clientHeight;
But they do not seem to be working.
Does anyone have any ideas? Any help is much appreciated!
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一切都很好
使用“ px” ,
我认为这会很好,请尝试一下。
Everything is fine just concatenate the viewPortWidth and viewPortHeight
with "px", like this,
I think this will work just fine, try it.
看看这支笔,它得到了主体元素和内部窗口的 vh 和 vw 。移动侧边栏,表格中的 domrect 将在调整大小时发生变化。
https://codepen.io/jfrazz/pen/KKZeaGe
check out this pen it gets vh and vw both of the body element and the inner window. Move the side bar around and the domrects in the table will change on resize.
https://codepen.io/jfrazz/pen/KKZeaGe