screen-adapt.uts
4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// utils/screen-adapt.uts
// 屏幕自适应布局工具函数
// 用于替代 CSS @media 查询,实现响应式布局
/**
* 屏幕信息接口
*/
export interface ScreenInfo {
width: number
height: number
pixelRatio: number
screenWidth: number
screenHeight: number
}
/**
* 获取屏幕信息
*/
export function getScreenInfo(): ScreenInfo {
const windowInfo = uni.getWindowInfo()
return {
width: windowInfo.windowWidth,
height: windowInfo.windowHeight,
pixelRatio: windowInfo.pixelRatio,
screenWidth: windowInfo.screenWidth,
screenHeight: windowInfo.screenHeight
}
}
/**
* 屏幕尺寸分类
*/
export type ScreenSize = 'small' | 'medium' | 'large' | 'xlarge'
/**
* 获取屏幕尺寸分类
* - small: < 480px(小屏手机)
* - medium: 480px - 768px(常规手机)
* - large: 768px - 1024px(大屏手机/小平板)
* - xlarge: >= 1024px(平板、大屏设备)
*/
export function getScreenSize(): ScreenSize {
const width = getScreenInfo().width
if (width < 480) {
return 'small'
} else if (width < 768) {
return 'medium'
} else if (width < 1024) {
return 'large'
} else {
return 'xlarge'
}
}
/**
* 响应式字体大小
* @param baseSize 基础字体大小(设计稿 375px 下的值)
* @param minSize 最小字体大小(px),默认 12
* @param maxSize 最大字体大小(px),默认 48
* @returns 计算后的字体大小(px)
*
* @example
* // 设计稿 375px,字体 14px
* // 小屏(320px):12px(最小值)
* // 常规(375px):14px(基准值)
* // 大屏(768px):18px(按比例缩放)
* // 超大屏(1024px):20px(最大值)
* const fontSize = responsiveFontSize(14, 12, 20)
*/
export function responsiveFontSize(baseSize: number, minSize: number = 12, maxSize: number = 48): number {
const screenInfo = getScreenInfo()
const screenWidth = screenInfo.width
// 设计稿基准宽度(通常为 375px 或 750px)
// 如果设计稿是 750px,这里改为 750,baseSize 也需要相应调整
const designWidth = 375
// 计算缩放比例(限制在合理范围内)
const scale = screenWidth / designWidth
const scaledSize = baseSize * scale
// 限制在最小值和最大值之间
if (scaledSize < minSize) {
return minSize
} else if (scaledSize > maxSize) {
return maxSize
}
return Math.round(scaledSize)
}
/**
* 响应式间距
* @param baseSpacing 基础间距(设计稿 375px 下的值)
* @param minSpacing 最小间距(px),默认 4
* @param maxSpacing 最大间距(px),默认 64
* @returns 计算后的间距(px)
*
* @example
* // 设计稿 375px,间距 16px
* // 小屏(320px):12px(最小值)
* // 常规(375px):16px(基准值)
* // 大屏(768px):24px(按比例缩放)
* const padding = responsiveSpacing(16, 12, 24)
*/
export function responsiveSpacing(baseSpacing: number, minSpacing: number = 4, maxSpacing: number = 64): number {
const screenInfo = getScreenInfo()
const screenWidth = screenInfo.width
const designWidth = 375
const scale = screenWidth / designWidth
const scaledSpacing = baseSpacing * scale
if (scaledSpacing < minSpacing) {
return minSpacing
} else if (scaledSpacing > maxSpacing) {
return maxSpacing
}
return Math.round(scaledSpacing)
}
/**
* 响应式宽度(用于固定宽度元素)
* @param baseWidth 基础宽度(设计稿 375px 下的值)
* @returns 计算后的宽度(px)
*
* @example
* // 设计稿 375px,按钮宽度 200px
* // 小屏(320px):171px
* // 常规(375px):200px
* // 大屏(768px):410px
* const buttonWidth = responsiveWidth(200)
*/
export function responsiveWidth(baseWidth: number): number {
const screenInfo = getScreenInfo()
const screenWidth = screenInfo.width
const designWidth = 375
return Math.round(baseWidth * (screenWidth / designWidth))
}
/**
* 判断是否为横屏
*/
export function isLandscape(): boolean {
const screenInfo = getScreenInfo()
return screenInfo.width > screenInfo.height
}
/**
* 判断是否为平板尺寸
*/
export function isTablet(): boolean {
const screenSize = getScreenSize()
return screenSize === 'large' || screenSize === 'xlarge'
}
/**
* 判断是否为手机尺寸
*/
export function isMobile(): boolean {
const screenSize = getScreenSize()
return screenSize === 'small' || screenSize === 'medium'
}
/**
* 获取列数(用于列表布局)
* @param mobileColumns 手机端列数,默认 1
* @param tabletColumns 平板端列数,默认 2
* @returns 当前屏幕对应的列数
*
* @example
* // 手机单列,平板两列
* const columns = getColumns(1, 2)
*/
export function getColumns(mobileColumns: number = 1, tabletColumns: number = 2): number {
return isTablet() ? tabletColumns : mobileColumns
}