-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2021-mobile-adaptive.html
More file actions
297 lines (280 loc) · 7.76 KB
/
Copy path2021-mobile-adaptive.html
File metadata and controls
297 lines (280 loc) · 7.76 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<!--
============================================================
移动端自适应布局 - rem + flex 移动端适配,响应式设计
布局方式:rem 相对单位 + Flex 弹性布局 + Media Query
布局思想:
1. 使用 rem 作为尺寸单位,通过设置 html 的 font-size 实现等比缩放
2. 以 750px 设计稿为基准,JS 动态计算根元素 font-size
3. 布局使用 Flex 实现弹性自适应,关键区域响应式调整
适用场景:
- H5 活动页面
- 移动端官网
- 移动端 Web App
- 微信公众号页面
- 需要适配各种手机屏幕的页面
技术要点:
- rem 单位:相对于根元素 font-size 的单位
- JS 动态设置 html font-size,通常按屏幕宽度 / 设计稿宽度 * 基准字体
- 配合 viewport meta 标签设置视口
- Flex 布局实现灵活的页面结构
- Media Query 处理横竖屏和极端尺寸
- 设计稿通常是 750px(iPhone 6/7/8 物理像素)
可迁移性:
- 可迁移到任何移动端 H5 页面
- rem 方案是移动端适配的经典方案
- 可与 vw/vh、flexible.js 等方案结合
wjs created by 2021-07-15
============================================================
-->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>移动端自适应布局</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
html { font-size: 50px; }
body {
font-family: -apple-system, BlinkMacSystemFont, "Helvetica Neue", Helvetica, "Microsoft YaHei", sans-serif;
background: #f5f5f5;
color: #333;
font-size: 0.28rem;
max-width: 750px;
margin: 0 auto;
}
.header {
height: 0.9rem;
background: #ff6b6b;
color: #fff;
display: flex;
align-items: center;
justify-content: center;
font-size: 0.34rem;
position: sticky;
top: 0;
z-index: 100;
}
.banner {
width: 100%;
height: 3rem;
background: linear-gradient(135deg, #ff9a9e 0%, #fecfef 100%);
display: flex;
align-items: center;
justify-content: center;
color: #fff;
font-size: 0.36rem;
}
.nav-grid {
display: flex;
flex-wrap: wrap;
background: #fff;
padding: 0.3rem 0;
margin-bottom: 0.2rem;
}
.nav-item {
width: 25%;
display: flex;
flex-direction: column;
align-items: center;
padding: 0.2rem 0;
}
.nav-item .icon {
width: 0.8rem;
height: 0.8rem;
border-radius: 50%;
background: #4ecdc4;
margin-bottom: 0.15rem;
display: flex;
align-items: center;
justify-content: center;
color: #fff;
font-size: 0.36rem;
}
.nav-item .label {
font-size: 0.24rem;
color: #666;
}
.section {
background: #fff;
margin-bottom: 0.2rem;
padding: 0.3rem;
}
.section-title {
font-size: 0.32rem;
font-weight: bold;
margin-bottom: 0.25rem;
padding-left: 0.15rem;
border-left: 0.06rem solid #ff6b6b;
}
.card-list {
display: flex;
flex-direction: column;
gap: 0.2rem;
}
.card {
display: flex;
background: #fafafa;
border-radius: 0.16rem;
overflow: hidden;
}
.card .img {
width: 2rem;
height: 1.6rem;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
flex-shrink: 0;
}
.card .info {
flex: 1;
padding: 0.2rem;
display: flex;
flex-direction: column;
justify-content: space-between;
}
.card .title {
font-size: 0.28rem;
font-weight: bold;
color: #333;
}
.card .desc {
font-size: 0.24rem;
color: #999;
}
.card .price {
font-size: 0.3rem;
color: #ff6b6b;
font-weight: bold;
}
.footer {
height: 1rem;
background: #fff;
display: flex;
border-top: 1px solid #eee;
position: sticky;
bottom: 0;
}
.footer-item {
flex: 1;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: 0.22rem;
color: #999;
}
.footer-item.active {
color: #ff6b6b;
}
.footer-item .icon {
font-size: 0.4rem;
margin-bottom: 0.05rem;
}
@media screen and (max-width: 320px) {
html { font-size: 42.67px; }
}
@media screen and (min-width: 750px) {
html { font-size: 50px; }
}
</style>
</head>
<body>
<div class="header">移动端自适应布局</div>
<div class="banner">
Banner 轮播图区域
</div>
<div class="nav-grid">
<div class="nav-item">
<div class="icon">1</div>
<div class="label">分类一</div>
</div>
<div class="nav-item">
<div class="icon" style="background: #ff6b6b;">2</div>
<div class="label">分类二</div>
</div>
<div class="nav-item">
<div class="icon" style="background: #feca57;">3</div>
<div class="label">分类三</div>
</div>
<div class="nav-item">
<div class="icon" style="background: #5f27cd;">4</div>
<div class="label">分类四</div>
</div>
<div class="nav-item">
<div class="icon" style="background: #00d2d3;">5</div>
<div class="label">分类五</div>
</div>
<div class="nav-item">
<div class="icon" style="background: #ff9ff3;">6</div>
<div class="label">分类六</div>
</div>
<div class="nav-item">
<div class="icon" style="background: #54a0ff;">7</div>
<div class="label">分类七</div>
</div>
<div class="nav-item">
<div class="icon" style="background: #5f27cd;">8</div>
<div class="label">分类八</div>
</div>
</div>
<div class="section">
<div class="section-title">精选推荐</div>
<div class="card-list">
<div class="card">
<div class="img"></div>
<div class="info">
<div class="title">商品标题一</div>
<div class="desc">这是商品描述信息</div>
<div class="price">¥99.00</div>
</div>
</div>
<div class="card">
<div class="img" style="background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);"></div>
<div class="info">
<div class="title">商品标题二</div>
<div class="desc">这是商品描述信息</div>
<div class="price">¥199.00</div>
</div>
</div>
<div class="card">
<div class="img" style="background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);"></div>
<div class="info">
<div class="title">商品标题三</div>
<div class="desc">这是商品描述信息</div>
<div class="price">¥299.00</div>
</div>
</div>
</div>
</div>
<div class="footer">
<div class="footer-item active">
<div class="icon">🏠</div>
<div>首页</div>
</div>
<div class="footer-item">
<div class="icon">📋</div>
<div>分类</div>
</div>
<div class="footer-item">
<div class="icon">🛒</div>
<div>购物车</div>
</div>
<div class="footer-item">
<div class="icon">👤</div>
<div>我的</div>
</div>
</div>
<script>
function setRem() {
var designWidth = 750;
var baseFont = 50;
var screenWidth = document.documentElement.clientWidth;
if (screenWidth > 750) screenWidth = 750;
var fontSize = (screenWidth / designWidth) * baseFont;
document.documentElement.style.fontSize = fontSize + 'px';
}
setRem();
window.addEventListener('resize', setRem);
window.addEventListener('orientationchange', setRem);
</script>
</body>
</html>