UNPKG

press-ui

Version:

简单、易用的跨端组件库,兼容 Vue2 和 Vue3,同时支持 uni-app和普通 Vue 项目

60 lines (46 loc) 1.21 kB
import { isDef, IN_BROWSER, isNumeric } from '../utils/validator'; export function addUnit(value) { if (!isDef(value)) { return undefined; } value = String(value); return isNumeric(value) ? `${value}px` : value; } // cache let rootFontSize; 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) { value = value.replace(/rem/g, ''); return +value * getRootFontSize(); } function convertVw(value) { value = value.replace(/vw/g, ''); return +value * window.innerWidth / 100; } function convertVh(value) { value = value.replace(/vh/g, ''); return +value * window.innerHeight / 100; } export function unitToPx(value) { if (typeof value === 'number') { return value; } if (IN_BROWSER) { if (value.indexOf('rem') !== -1) { return convertRem(value); } if (value.indexOf('vw') !== -1) { return convertVw(value); } if (value.indexOf('vh') !== -1) { return convertVh(value); } } return parseFloat(value); }