移动端常用适配方案一
通过媒体查询
媒体查询的方式可以说是我早期采用的布局方式, 它主要是通过查询设备的宽度来执行不同的 css 代码,最终达到界面的适配。
媒体查询优势
- 简单, 哪里不对改哪里
- 调整屏幕宽度的时候不用刷新页面, 即可响应式的进行展示
- 特别适合对移动端和 PC 维护同一套代码的时候
媒体查询劣势
- 由于移动端和 PC 端维护同一套代码, 所以代码量比较大,维护不方便
- 为了兼顾大屏幕或高清设备,会造成其他设备资源浪费,特别是加载的图片资源
- 为了兼顾移动端和 PC 端各自响应式的展示效果,难免会损失各自特有的交互方式
应用场景
- 对于比较简单(界面不复杂)的网页, 诸如: 企业官网、宣传单网页等
- 我们可以通过
媒体查询
、伸缩布局
、Bootstrap
来实现响应式站点 - 对于比较复杂(界面复杂)的网页, 诸如: 电商、团购等
- 更多的则是采用 PC 端一套代码, 移动端一套代码的方式
如下我将会给出一个示例布局代码,就是来演示一下移动端常用的布局方式当中的通过媒体查询实现的方式:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>移动端常用适配方案一</title>
<style>
* {
margin: 0;
padding: 0;
}
.top {
position: relative;
}
.top > img {
width: 100%;
height: auto;
}
.top > p {
font-size: 16px;
color: #fff;
position: absolute;
left: 50%;
transform: translateX(-50%);
top: 33px;
}
.middle, .bottom {
position: relative;
height: 124px;
}
.main {
border: 1px dashed #0d7efb;
border-radius: 5px;
padding: 4px;
display: inline-block;
position: absolute;
left: 50%;
transform: translateX(-50%);
}
.main > img:nth-of-type(1) {
width: 175px;
height: 116px;
vertical-align: bottom;
}
.main > img:nth-of-type(2) {
width: 35px;
height: 35px;
position: absolute;
left: 50%;
transform: translateX(-50%);
top: 25px;
}
.bottom {
margin-top: 14px;
}
/*
iPhone 6/7/8
*/
@media screen and (min-width: 375px) {
.top > p {
font-size: 18px;
top: 40px;
}
.middle, .bottom {
height: 143px;
}
.main > img:nth-of-type(1) {
width: 206px;
height: 135px;
}
.main > img:nth-of-type(2) {
width: 42px;
height: 42px;
top: 30px;
}
.bottom {
margin-top: 17px;
}
}
/*
iphone 6/7/8Plus
*/
@media screen and (min-width: 414px) {
.top > p {
font-size: 20px;
top: 43px;
}
.middle, .bottom {
height: 160px;
padding: 5px;
}
.main > img:nth-of-type(1) {
width: 227px;
height: 150px;
}
.main > img:nth-of-type(2) {
width: 45px;
height: 45px;
top: 35px;
}
.bottom {
margin-top: 19px;
}
}
</style>
</head>
<body>
<div class="top">
<img src="images/bg.png" alt="">
<p>实名认证</p>
</div>
<div class="middle">
<div class="main">
<img src="images/back.png" alt="">
<img src="images/add.png" alt="">
</div>
</div>
<div class="bottom">
<div class="main">
<img src="images/back.png" alt="">
<img src="images/add.png" alt="">
</div>
</div>
</body>
</html>
如上代码当中的素材以及测试效果博主这里就只是提供对应的素材文件,至于测试效果自行测试验证即可,其实通过媒体查询的方式进行实现在这里博主是不怎么推荐的,就因如上它所包含的那些劣势,就足以不推荐所以说,在下一篇文章当中我会在介绍另外一种,就是 PC 端一套代码,移动端一套代码的方式。
标签:
移动Web开发+项目实战
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具