Skip to content

Commit 51e52e5

Browse files
committed
feat(px-to-response): 添加px2Response函数以支持像素值转换为响应式单位
1 parent 445b694 commit 51e52e5

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

packages/flexible/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@cherrywind/flexible",
3-
"version": "0.1.2",
3+
"version": "0.1.3",
44
"private": false,
55
"keywords": [
66
"flexible"

packages/flexible/src/index.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,3 +311,20 @@ function getScrollbarWidth(): number {
311311
document.body.removeChild(scrollDiv);
312312
return scrollbarWidth;
313313
}
314+
315+
/**
316+
* 将像素值转换为响应式单位
317+
* @param px 像素值
318+
* @param options 配置选项
319+
* @returns 响应式单位字符串
320+
*/
321+
export function px2Response(
322+
px: number,
323+
options: {
324+
baseWidth?: number;
325+
localVarName?: string;
326+
},
327+
) {
328+
const { baseWidth = 19.2, localVarName = '--local-scope-rem' } = options;
329+
return `calc(${(px / baseWidth).toFixed(4)} * var(${localVarName}, 1rem))`;
330+
}

0 commit comments

Comments
 (0)