在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML5  HTML/ js 獲取css中設(shè)置的rem值

js 獲取css中設(shè)置的rem值

用 getComputedStyle 只能取得轉(zhuǎn)化成px的值

回答
編輯回答
浪婳

先說思路,得到需要計(jì)算屬性的px值,然后再得到html標(biāo)簽里的font-size的px值,因?yàn)閞em是基于html的font-size來的。1rem就是一個(gè)font-size的px值。
獻(xiàn)上代碼:css

*{
    margin: 0;
    padding: 0;
}
html{
    font-size: 20px;
}
#box{
    width: 9rem;
    height: 9rem;
    background-color: red;
}

html

<div id="box"></div>

js

var box = document.getElementById("box");
var boxStyle = window.getComputedStyle(box).width.slice(0,-2);

var html = document.getElementsByTagName("html");
var htmlStyle = window.getComputedStyle(html[0]).fontSize.slice(0,-2);
console.log(boxStyle/htmlStyle + "rem");
2017年11月5日 03:43
編輯回答
嘟尛嘴

取到的是瀏覽器默認(rèn)單位,你進(jìn)行一下?lián)Q算唄

px/16 = rem

2017年8月4日 02:41