移动端网页兼容适配方案小结


前言

移动端适配一直是前端开发中的重点难题,分享下常见的移动端兼容处理方案。

在这里插入图片描述


一、使用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:适配刘海屏

二、使用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 */
}

三、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;
    }
}

四、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);
    }
}

五、安全区域适配

适配 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);
}

六、图片适配方案

针对 不同分辨率 设备的 图片 适配策略。

<!-- 使用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;
}

七、横屏适配处理

处理 横屏模式 下的布局适配。

/* 检测横屏 */
@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('横屏');
    }
});

八、软键盘弹出处理

处理 软键盘弹出 时的页面适配问题。

// 监听软键盘
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;
}

总结

移动端适配是一个系统工程,需要在项目开始时就建立完整的适配方案,而不是在遇到问题时临时处理。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Microi风闲

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值