JQuery 高度和单位
我知道 jquery 的 height()
方法返回一个无单位的数字... 我如何确定这个数字是我想要的单位?
我想用厘米 cm 有什么想法吗?
I know that height()
method of jquery returns a number unitless...
How can i be sure that this number is in the unit i want?
I want to work with centimeter cm any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
height() 返回的单位是像素。
将其转换为厘米可能很困难,因为这取决于用户的显示器、像素大小等。
问题,天哪,你为什么想要厘米?
the unit returned by height() is pixels.
converting that to cm might be difficult, as that depends on the user's monitor, size of pixels, etc.
question, why in gods name do you want cm?
我知道这是愚蠢的解决方案,但是这个怎么样? :D
对我来说通常返回 38px == 1cm
你觉得可以吗?
I know it is stupid solution, but how about this? :D
For me usually returns 38px == 1cm
Do you think it's OK?
height 返回以像素为单位的高度。不幸的是,厘米对你来说将很难使用。显示器都有不同的像素密度,而 JavaScript 没有办法知道这个像素密度。您必须估计平均一厘米有多少像素并从那里进行转换。
平均像素密度为每厘米 100-130 像素,您可以看出这是一个相当宽的范围。
height returns height in pixels. Unfortunately centimeters is going to be very difficult for you to work with. Monitors all have different pixel densities, and javascript does not have a way to know this pixel density. You'll have to estimate how many pixels average a centimeter and convert from there.
Average pixel density would have anywhere from 100-130px in a centimeter, as you can tell this is a pretty wide range.
您可以使用已知大小的不可见 div,然后使用。 width() 并除以已知大小即可得到比率。
you can use an invisible div with a known size and then use. width() and divide by the known size to get the ratio.