11/**
2- * Options for the flexible layout function.
2+ * 弹性布局函数的配置选项。
33 */
44export interface FlexibleOptions {
55 /**
6- * An array of breakpoints in pixels, from largest to smallest.
7- * Defaults to [768].
6+ * 一个以像素为单位的断点数组,从大到小排列。
7+ * 默认为 [768]。
88 */
99 breakpoints ?: number [ ] ;
1010 /**
11- * An array of layout widths that corresponds to breakpoints.
12- * Must have exactly one more item than breakpoints array.
13- * For example, if breakpoints is [768], layouts could be [375, 1920],
14- * where 375 is the width for viewport <= 768px and 1920 is for viewport > 768px.
11+ * 一个与断点相对应的布局宽度数组。
12+ * 其项目数量必须比断点数组多一个。
13+ * 例如,如果 breakpoints 是 [768],则 layouts 可以是 [375, 1920],
14+ * 其中 375 是视口宽度 <= 768px 时的布局宽度, 1920 是视口宽度 > 768px 时的布局宽度。
1515 */
1616 layouts ?: number [ ] ;
1717 /**
18- * The base layout width used as reference for calculations.
19- * Only effective when layouts is provided.
20- * Used as the baseline layout width for ratio calculations.
21- * Defaults to the last item in layouts array (layouts?.at(-1)),
22- * which typically represents the largest viewport width.
18+ * 用作计算参考的基础布局宽度。
19+ * 仅在提供了 layouts 时有效。
20+ * 用作比例计算的基准布局宽度。
21+ * 默认为 layouts 数组中的最后一项 (layouts?.at(-1)),
22+ * 这通常代表最大的视口宽度。
2323 */
2424 basicLayout ?: number ;
2525 /**
26- * Whether to apply the layout immediately on initialization.
27- * Defaults to false.
26+ * 是否在初始化时立即应用布局。
27+ * 默认为 false。
2828 */
2929 immediate ?: boolean ;
3030 /**
31- * Whether to listen for orientation change events.
32- * Defaults to true.
31+ * 是否监听屏幕方向变化事件。
32+ * 默认为 true。
3333 */
3434 orientationchange ?: boolean ;
3535
3636 /**
37- * Whether to set the CSS variable on a specific scope element.
38- * Defaults to false, which means setting the font size on the document element.
39- * If an object is provided, it can specify the element and CSS variable name.
37+ * 是否在特定的作用域元素上设置 CSS 变量。
38+ * 默认为 false,表示在 document 元素上设置字体大小。
39+ * 如果提供一个对象,可以指定元素和 CSS 变量名。
4040 */
4141 scope ?:
4242 | false
4343 | {
4444 /**
45- * The scope element to set the CSS variable on.
46- * Defaults to document.documentElement.
45+ * 设置 CSS 变量的作用域元素。
46+ * 默认为 document.documentElement。
4747 */
4848 element : HTMLElement ;
4949 /**
50- * The CSS variable name to use for the base rem value.
51- * Defaults to "--local-scope-rem".
50+ * 用于 rem 基础值的 CSS 变量名。
51+ * 默认为 "--local-scope-rem"。
5252 */
5353 cssVarName ?: string ;
5454 }
5555 | {
5656 /**
57- * The scope element to set the CSS variable on.
58- * Defaults to document.documentElement.
57+ * 设置 CSS 变量的作用域元素。
58+ * 默认为 document.documentElement。
5959 */
6060 element : HTMLElement ;
6161 /**
62- * An array of ratio factors for each layout, used for poster mode or custom scaling.
63- * Must have the same length as layouts.
64- * Defaults to [1, 1, ...] (no extra scaling).
62+ * 每个布局的比例因子数组,用于海报模式或自定义缩放。
63+ * 长度必须与 layouts 数组相同。
64+ * 默认为 [1, 1, ...] (无额外缩放)。
6565 */
6666 ratio ?: number [ ] ;
6767 /**
68- * The CSS variable name to use for the base rem value.
69- * Defaults to "--local-scope-rem".
68+ * 用于 rem 基础值的 CSS 变量名。
69+ * 默认为 "--local-scope-rem"。
7070 */
7171 cssVarName ?: string ;
7272 } [ ] ;
7373
7474 /**
75- * An array of ratio factors for each layout, used for poster mode or custom scaling.
76- * Must have the same length as layouts.
77- * Defaults to [1, 1, ...] (no extra scaling).
75+ * 每个布局的比例因子数组,用于海报模式或自定义缩放。
76+ * 长度必须与 layouts 数组相同。
77+ * 默认为 [1, 1, ...] (无额外缩放)。
7878 */
7979 ratio ?: number [ ] ;
8080 /**
81- * Whether to recalibrate the layout when the window is resized.
81+ * 是否在窗口大小调整时重新校准布局。
8282 * 因为宽度变化,但是css var没来得及变化导致出现滚动条,clientWidth和innerWidth不一致
83- * Defaults to true.
83+ * 默认为 true。
8484 */
8585 recalibrate ?: boolean ;
8686 /**
87- * An option to control the resize behavior.
87+ * 用于控制 resize 行为的选项。
8888 */
8989 resizeOption ?:
9090 | {
@@ -95,11 +95,11 @@ export interface FlexibleOptions {
9595}
9696
9797/**
98- * Initializes a flexible layout system that sets a CSS variable for rem units
99- * based on the viewport width and adaptively scales according to breakpoints.
98+ * 初始化一个弹性布局系统,该系统会根据视口宽度设置一个用于 rem 单位的 CSS 变量,
99+ * 并根据断点自适应缩放。
100100 *
101- * @param options - Configuration options for the flexible layout
102- * @returns A cleanup function to remove event listeners
101+ * @param options - 弹性布局的配置选项
102+ * @returns 一个用于移除事件监听器的清理函数
103103 */
104104export const flexible = ( options : FlexibleOptions = { } ) : ( ( ) => void ) => {
105105 const {
@@ -120,7 +120,7 @@ export const flexible = (options: FlexibleOptions = {}): (() => void) => {
120120 // 对于相同设备来说,滚动条要么永远占据宽度,要么永远不占据宽度
121121 const isScrollbarPresent = getScrollbarWidth ( ) > 0 ;
122122
123- // Ensure ratio array matches layouts length, default to 1
123+ // 确保 ratio 数组的长度与 layouts 匹配,默认为 1
124124 let ratio = propRatio ;
125125 if ( ratio ) {
126126 if ( layouts ?. length !== ratio . length && layouts ) {
@@ -129,20 +129,20 @@ export const flexible = (options: FlexibleOptions = {}): (() => void) => {
129129 }
130130
131131 /**
132- * Calculate the ratio factor for a specific breakpoint
133- * @param index - Breakpoint index
134- * @returns The ratio factor, defaults to 1
132+ * 计算特定断点的比例因子
133+ * @param index - 断点索引
134+ * @returns 比例因子,默认为 1
135135 */
136136 const getBreakpointRatio = ( index : number ) : number => {
137137 if ( ! layouts || ! basicLayout ) return 1 ;
138138 if ( layouts . length - 1 === breakpoints . length ) {
139139 return basicLayout / layouts [ index ] ;
140140 }
141- return 1 ; // Default to ratio factor of 1
141+ return 1 ; // 默认为 1 的比例因子
142142 } ;
143143
144144 /**
145- * Respond to window size changes and update CSS variable
145+ * 响应窗口大小变化并更新 CSS 变量
146146 */
147147 const responsive = ( ) : void => {
148148 // 内部核心计算逻辑,可以被重复调用
@@ -211,7 +211,7 @@ export const flexible = (options: FlexibleOptions = {}): (() => void) => {
211211 if ( orientationchange ) {
212212 screen . orientation . addEventListener ( 'change' , resizeHandler ) ;
213213 }
214- // Return cleanup function
214+ // 返回清理函数
215215 return ( ) => {
216216 if ( ! immediate ) {
217217 window . removeEventListener ( 'load' , responsive ) ;
@@ -223,6 +223,12 @@ export const flexible = (options: FlexibleOptions = {}): (() => void) => {
223223 } ;
224224} ;
225225
226+ /**
227+ * 防抖函数:在事件被触发n秒后再执行回调,如果在这n秒内又被触发,则重新计时。
228+ * @param func 要执行的函数
229+ * @param wait 延迟时间(毫秒)
230+ * @returns 防抖后的函数
231+ */
226232function debounce < F extends ( ...args : any [ ] ) => any > ( func : F , wait : number ) : F {
227233 let timeout : ReturnType < typeof setTimeout > | null = null ;
228234
@@ -277,6 +283,10 @@ function throttle<T extends (...args: any[]) => void>(func: T, delay: number): (
277283 } ;
278284}
279285
286+ /**
287+ * 计算浏览器滚动条的宽度。
288+ * @returns {number } 滚动条的宽度(像素)
289+ */
280290function getScrollbarWidth ( ) : number {
281291 const scrollDiv = document . createElement ( 'div' ) ;
282292 scrollDiv . style . width = '100px' ;
0 commit comments