1+ export type ScopeItem = {
2+ /**
3+ * 设置 CSS 变量的作用域元素。
4+ * 默认为 document.documentElement。
5+ */
6+ element : HTMLElement ;
7+ /**
8+ * 每个布局的比例因子数组,用于海报模式或自定义缩放。
9+ * 长度必须与 layouts 数组相同。
10+ * 默认为 [1, 1, ...] (无额外缩放)。
11+ */
12+ ratio ?: number [ ] ;
13+ /**
14+ * 用于 rem 基础值的 CSS 变量名。
15+ * 默认为 "--local-scope-rem"。
16+ */
17+ cssVarName ?: string ;
18+ } ;
19+
120/**
221 * 弹性布局函数的配置选项。
322 */
@@ -37,6 +56,25 @@ export interface FlexibleOptions {
3756 */
3857 orientationchange ?: boolean ;
3958
59+ onTransform ?: ( options : {
60+ /**
61+ * 作用域元素的信息(如果适用)。
62+ */
63+ scope ?: ScopeItem ;
64+ /**
65+ * 匹配的layout索引。
66+ */
67+ matchedIndex : number ;
68+ /**
69+ * 原始计算得到的 vw 值(未经过任何比例调整)。
70+ */
71+ originalVw : number ;
72+ /**
73+ * 计算得到的比例因子。
74+ */
75+ computedRatio : number ;
76+ } ) => number ;
77+
4078 /**
4179 * 是否在特定的作用域元素上设置 CSS 变量。
4280 * 默认为 false,表示在 document 元素上设置字体大小。
@@ -56,24 +94,7 @@ export interface FlexibleOptions {
5694 */
5795 cssVarName ?: string ;
5896 }
59- | {
60- /**
61- * 设置 CSS 变量的作用域元素。
62- * 默认为 document.documentElement。
63- */
64- element : HTMLElement ;
65- /**
66- * 每个布局的比例因子数组,用于海报模式或自定义缩放。
67- * 长度必须与 layouts 数组相同。
68- * 默认为 [1, 1, ...] (无额外缩放)。
69- */
70- ratio ?: number [ ] ;
71- /**
72- * 用于 rem 基础值的 CSS 变量名。
73- * 默认为 "--local-scope-rem"。
74- */
75- cssVarName ?: string ;
76- } [ ] ;
97+ | ScopeItem [ ] ;
7798
7899 /**
79100 * 每个布局的比例因子数组,用于海报模式或自定义缩放。
@@ -172,12 +193,29 @@ export const flexible = (options: FlexibleOptions = {}): (() => void) => {
172193 matchedIndex = layouts . length - 1 ;
173194 }
174195 }
196+ const originalVw = vw ;
197+ if ( options . onTransform ) {
198+ vw = options . onTransform ( {
199+ matchedIndex,
200+ originalVw,
201+ computedRatio : ratio ?. [ matchedIndex ] ?? 1 ,
202+ } ) ;
203+ }
175204 if ( scope ) {
176205 if ( Array . isArray ( scope ) ) {
177206 scope . forEach ( ( item ) => {
178207 const { element = document . documentElement , cssVarName = defaultScopeCssVarName , ratio : scopeRatio } = item ;
179208 const computedRatio = scopeRatio ?. [ matchedIndex ] ?? ratio ?. [ matchedIndex ] ?? 1 ;
180- element . style . setProperty ( cssVarName , vw * computedRatio + 'px' ) ;
209+ let scopeVw = originalVw * computedRatio ;
210+ if ( options . onTransform ) {
211+ scopeVw = options . onTransform ( {
212+ scope : item ,
213+ matchedIndex,
214+ originalVw,
215+ computedRatio,
216+ } ) ;
217+ }
218+ element . style . setProperty ( cssVarName , scopeVw + 'px' ) ;
181219 } ) ;
182220 } else {
183221 const { element = document . documentElement , cssVarName = defaultScopeCssVarName } = scope ;
0 commit comments