Merge branch '2.x' into dev

This commit is contained in:
chenjiahan
2020-11-04 20:06:59 +08:00
18 changed files with 88 additions and 16 deletions
+8 -1
View File
@@ -44,6 +44,11 @@ function convertVw(value: string) {
return (+value * window.innerWidth) / 100;
}
function convertVh(value: string) {
value = value.replace(/vh/g, '');
return (+value * window.innerHeight) / 100;
}
export function unitToPx(value: string | number): number {
if (typeof value === 'number') {
return value;
@@ -53,10 +58,12 @@ export function unitToPx(value: string | number): number {
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);
+4
View File
@@ -132,6 +132,8 @@ test('addUnit', () => {
test('unitToPx', () => {
const originGetComputedStyle = window.getComputedStyle;
window.innerWidth = 100;
window.innerHeight = 200;
window.getComputedStyle = () => ({ fontSize: '16px' });
expect(unitToPx(0)).toEqual(0);
@@ -139,6 +141,8 @@ test('unitToPx', () => {
expect(unitToPx('10px')).toEqual(10);
expect(unitToPx('0rem')).toEqual(0);
expect(unitToPx('10rem')).toEqual(160);
expect(unitToPx('10vw')).toEqual(10);
expect(unitToPx('10vh')).toEqual(20);
window.getComputedStyle = originGetComputedStyle;
});