Skip to content

Commit b5a596f

Browse files
committed
feat: Add px to responsive min/max vw in inline styles
1 parent 88f2d33 commit b5a596f

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "vue3-vant-mobile",
33
"type": "module",
44
"version": "2.3.7",
5-
"packageManager": "pnpm@9.4.0",
5+
"packageManager": "pnpm@9.6.0",
66
"description": "Vue + Vite H5 Starter Template",
77
"license": "MIT",
88
"scripts": {

src/utils/inline-px-to-vw.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,28 @@
1+
/**
2+
* 支持行内样式 px 转 vw
3+
* 本文件代码来自于 scale-view 开源项目
4+
* 源代码链接:https://github.com/wswmsword/scale-view
5+
* 非常感谢作者 @wswmsword 的支持
6+
*/
7+
18
import { round } from 'lodash-es'
29

3-
// 设计稿的宽度
4-
const width = 375
10+
// 理想宽度,设计稿的宽度
11+
const idealWidth = 375
12+
13+
// 表示伸缩视图的最大宽度
14+
const maxWidth = 600
515

616
/**
7-
* vw 转换
17+
* 限制大小的 vw 转换
818
* @param {number} n
919
*/
1020
export default function vw(n: number) {
1121
if (n === 0)
1222
return n
13-
const vwN = round(n * 100 / width, 3)
14-
return `${vwN}vw`
23+
24+
const vwN = round(n * 100 / idealWidth, 3)
25+
const maxN = round(n * maxWidth / idealWidth, 3)
26+
const cssF = n > 0 ? 'min' : 'max'
27+
return `${cssF}(${vwN}vw, ${maxN}px)`
1528
}

0 commit comments

Comments
 (0)