网站首页 > 知识剖析 正文
移动端适配一直是前端开发中的重点难题,分享下常见的移动端兼容处理方案。
1. 使用viewport配置,确保完美视口
移动端开发首先要设置正确的viewport,这是适配的基础。
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
关键属性解析:
- width=device-width:将视口宽度设置为设备宽度
- initial-scale=1.0:初始缩放比例为1
- user-scalable=no:禁用用户缩放
- viewport-fit=cover:适配刘海屏
2. 使用rem实现弹性布局
rem是相对于根元素(html)的字体大小的单位,可以实现整体布局的弹性缩放。
// 设置 rem 基准值
(function flexible() {
const docEl = document.documentElement;
function setRemUnit() {
const rem = docEl.clientWidth / 10;
docEl.style.fontSize = rem + 'px';
}
setRemUnit();
window.addEventListener('resize', setRemUnit);
window.addEventListener('orientationchange', setRemUnit);
})();
配套的CSS使用:
.container {
width: 7.5rem; /* 750px / 100 */
height: 1rem; /* 100px / 100 */
font-size: 0.28rem; /* 28px / 100 */
}
3. CSS媒体查询处理不同尺寸
使用媒体查询针对不同屏幕尺寸定制样式。
/* iPhone SE */
@media screen and (max-width: 374px) {
.container {
font-size: 14px;
}
}
/* iPhone 6/7/8/X */
@media screen and (min-width: 375px) and (max-width: 413px) {
.container {
font-size: 16px;
}
}
/* iPhone 6/7/8 Plus */
@media screen and (min-width: 414px) {
.container {
font-size: 18px;
}
}
4. 1px边框问题解决方案
在高清屏幕下1px边框显示过粗的解决方案。
.border-1px {
position: relative;
&::after {
content: '';
position: absolute;
left: 0;
bottom: 0;
width: 100%;
height: 1px;
background-color: #000;
transform: scaleY(0.5);
transform-origin: bottom;
}
}
// 2x屏
@media (-webkit-min-device-pixel-ratio: 2) {
.border-1px::after {
transform: scaleY(0.5);
}
}
// 3x屏
@media (-webkit-min-device-pixel-ratio: 3) {
.border-1px::after {
transform: scaleY(0.33);
}
}
5. 安全区域适配
适配iPhone X等带有刘海的机型。
/* 适配刘海屏 */
.safe-area-inset {
padding-top: constant(safe-area-inset-top);
padding-top: env(safe-area-inset-top);
padding-bottom: constant(safe-area-inset-bottom);
padding-bottom: env(safe-area-inset-bottom);
}
/* 底部固定导航适配 */
.fixed-bottom {
position: fixed;
bottom: 0;
bottom: constant(safe-area-inset-bottom);
bottom: env(safe-area-inset-bottom);
}
6. 图片适配方案
针对不同分辨率设备的图片适配策略。
<!-- 使用srcset适配不同分辨率 -->
<img srcset="image-320w.jpg 320w,
image-480w.jpg 480w,
image-800w.jpg 800w"
sizes="(max-width: 320px) 280px,
(max-width: 480px) 440px,
800px"
src="image-800w.jpg" alt="Responsive image">
配合CSS的处理:
.responsive-image {
max-width: 100%;
height: auto;
display: block;
}
7. 横屏适配处理
处理横屏模式下的布局适配。
/* 检测横屏 */
@media screen and (orientation: landscape) {
.landscape-container {
display: flex;
flex-direction: row;
}
}
/* 检测竖屏 */
@media screen and (orientation: portrait) {
.portrait-container {
display: flex;
flex-direction: column;
}
}
JavaScript监听屏幕旋转:
window.addEventListener('orientationchange', function() {
if (window.orientation === 180 || window.orientation === 0) {
// 竖屏
console.log('竖屏');
}
if (window.orientation === 90 || window.orientation === -90) {
// 横屏
console.log('横屏');
}
});
8. 软键盘弹出处理
处理软键盘弹出时的页面适配问题。
// 监听软键盘
const originalHeight = document.documentElement.clientHeight;
window.addEventListener('resize', () => {
const currentHeight = document.documentElement.clientHeight;
const input = document.activeElement;
if (originalHeight > currentHeight) {
// 软键盘弹出
if (input.tagName === 'INPUT' || input.tagName === 'TEXTAREA') {
input.scrollIntoView({ block: 'center' });
}
} else {
// 软键盘收起
window.scrollTo(0, 0);
}
});
CSS处理:
/* 防止键盘顶起页面 */
.container {
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
overflow: auto;
-webkit-overflow-scrolling: touch;
}
移动端适配是一个系统工程,需要在项目开始时就建立完整的适配方案,而不是在遇到问题时临时处理。
- 上一篇: 测试开发如何快速上手Vue前端开发(下)
- 下一篇: php 二维数组某个字段排序
猜你喜欢
- 2025-01-07 测试开发如何快速上手Vue前端开发(下)
- 2025-01-07 WordPress 6.2 引进了速度更快的 HTML 处理 API
- 2025-01-07 5分钟看懂独立站SEO新手指南(下)
- 2025-01-07 是什么组成了html?html组成元素有哪些?
- 2025-01-07 爬虫批量获取图片的思路与方法
- 2025-01-07 在企业网站建设中如何正确使用H标签?
- 2025-01-07 自适应设计:自适应图片的完整教程
- 2025-01-07 「转行测试开发-HTML」(六)---div和span、图像标签的使用
- 2025-01-07 企业SEO网站优化方案流程
- 2025-01-07 JSP页面实现验证码校验
- 最近发表
-
- 表格存储 SQL 查询多元索引(表格存储 sql 查询多元索引的方法)
- 数据库教程-SQL Server多条件模糊查询
- Twitch宣布放弃Flash并逐步转型至HTML5平台
- 移动平台最强播放器MX Player:终于支持安卓5.0了!
- win10 NFS+黑群晖远程加载管理Windows文件夹(读写NTFS格式+高清播放器)
- Android端VLC 3.3版本发布,重新设计播放器界面
- 不仅仅被苹果封杀!Youtube宣布迁移Flash
- 揭开网站背后的魔法:B/S系统原来这么简单!
- Adobe Animate (An) 2020网页设计软件下载和安装教程
- Adobe发布“巨量”安全更新:遏制Flash“祸害”Linux
- 标签列表
-
- xml (46)
- css animation (57)
- array_slice (60)
- htmlspecialchars (54)
- position: absolute (54)
- datediff函数 (47)
- array_pop (49)
- jsmap (52)
- toggleclass (43)
- console.time (63)
- .sql (41)
- ahref (40)
- js json.parse (59)
- html复选框 (60)
- css 透明 (44)
- css 颜色 (47)
- php replace (41)
- css nth-child (48)
- min-height (40)
- xml schema (44)
- css 最后一个元素 (46)
- location.origin (44)
- table border (49)
- html tr (40)
- video controls (49)