CSS 如何将 vh 中给定的元素的高度转换为像素?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18387490/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 00:03:37  来源:igfitidea点击:

How do I convert a height of an element given in vh to pixels?

javascriptcssheightviewport-units

提问by Sudharsanan

I have the max-height of an element as 65vh. I need to convert it to pixels in my JavaScript to see whether an image can fit there or if I need to shrink/crop it. (am doing win8 App development).

我有一个元素的最大高度为 65vh。我需要在我的 JavaScript 中将其转换为像素,以查看图像是否适合那里,或者我是否需要缩小/裁剪它。(正在做win8 App开发)。

Will this work?

这会起作用吗?

100 vh = screen.heighttherefore 65vh in pixels is screen.height *0.65

100 vh =screen.height因此以像素为单位的 65vh 是screen.height *0.65

回答by rink.attendant.6

Not necessarily screen.height * 0.65, but viewport.height * 0.65. Even though a Windows 8 app will always have the same height, regardless of the snapped state, this is an important difference in browser-based applications.

不一定screen.height * 0.65,但是viewport.height * 0.65。即使 Windows 8 应用程序将始终具有相同的高度,无论捕捉状态如何,这也是基于浏览器的应用程序的一个重要区别。

In JavaScript:

在 JavaScript 中:

document.documentElement.clientHeight * 0.65;

If you're using jQuery, you can do:

如果您使用 jQuery,则可以执行以下操作:

$(window).height() * 0.65;

回答by LinkinTED

When I use $("div").height();it actually returns the value in pixels.

当我使用$("div").height();它时,它实际上以像素为单位返回值。

Check this fiddle.

检查这个小提琴