mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
perf: use inline style instead of getComputedStyle (#6550)
This commit is contained in:
parent
35a4b95ef2
commit
fd388d0475
@ -10,13 +10,24 @@ export function addUnit(value?: string | number): string | undefined {
|
|||||||
return isNumeric(value) ? `${value}px` : value;
|
return isNumeric(value) ? `${value}px` : value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// cache
|
||||||
|
let rootFontSize: number;
|
||||||
|
|
||||||
|
function getRootFontSize() {
|
||||||
|
if (!rootFontSize) {
|
||||||
|
const doc = document.documentElement;
|
||||||
|
const fontSize =
|
||||||
|
doc.style.fontSize || window.getComputedStyle(doc).fontSize;
|
||||||
|
|
||||||
|
rootFontSize = parseFloat(fontSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
return rootFontSize;
|
||||||
|
}
|
||||||
|
|
||||||
function convertRem(value: string) {
|
function convertRem(value: string) {
|
||||||
const rootStyle = window.getComputedStyle(document.documentElement);
|
|
||||||
const rootFontSize = parseFloat(rootStyle.fontSize);
|
|
||||||
|
|
||||||
value = value.replace(/rem/g, '');
|
value = value.replace(/rem/g, '');
|
||||||
|
return +value * getRootFontSize();
|
||||||
return +value * rootFontSize;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function unitToPx(value: string | number): number {
|
export function unitToPx(value: string | number): number {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user