[英]How to convert or read CSS rem value to px?
I want to find the px value of height of html h1 element. 我想找到html h1元素的高度的px值。
var height = $("h1").height(); console.log(height); $(window).on("scroll", function() { if ($(window).scrollTop() >= height) { // ... } else { // ... } });
h1 { font-size: 3.5rem; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <h1>Foo</h1>
3.5rem
is 56px
3.5rem
是56px
When I wrote 56px
at h1
tag on CSS, then I can find the height 56px
. 当我在CSS的
h1
标签上编写56px
时,可以找到高度56px
。 However If i wrote 3.5rem
, it doesn't calculate as 56px
. 但是,如果我写了
3.5rem
,它不会计算为56px
。
I did test it with .scrollTop
function. 我确实使用
.scrollTop
函数对其进行了测试。
Why do I not get the 3.5rem
to 56px
value, when I used height()
? 当我使用
height()
时,为什么不能将3.5rem
为56px
? How can I convert the rem
value to px
? 如何将
rem
值转换为px
?
Think of h1
as a container element like div
, Note: these two elements has there own default styles, these default styles may sometimes differ in different browsers. 将
h1
视为类似于div
的容器元素, 请注意:这两个元素具有自己的默认样式,这些默认样式有时在不同的浏览器中可能会有所不同。 What matters in this case is what is inside the h1
html Element and the styles specified.. After understanding your question. 在这种情况下,重要的是
h1
html元素和指定的样式中的内容。理解您的问题之后。 You can achieve what you want by doing the following. 您可以通过执行以下操作来实现所需的目标。 If you want something different, let me know.
如果您想要其他东西,请告诉我。
var h1 = $('h1').css('font-size'); console.log(h1); $(window).on("scroll", function() { if ($(window).scrollTop() >= height) { // ... } else { // ... } });
h1 { font-size: 3.5rem; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <h1>Foo</h1>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.