当前位置:首页 > 技术分析 > 正文内容

8个移动端适配技巧,兼容性问题减少90%

ruisui882个月前 (05-03)技术分析21

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

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

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

扫描二维码推送至手机访问。

版权声明:本文由ruisui88发布,如需转载请注明出处。

本文链接:http://www.ruisui88.com/post/3769.html

分享给朋友:

“8个移动端适配技巧,兼容性问题减少90%” 的相关文章

K8S NFS 共享存储

NFS 共享存储前面我们学习了 hostPath 与 Local PV 两种本地存储方式,但是平时我们的应用更多的是无状态服务,可能会同时发布在不同的节点上,这个时候本地存储就不适用了,往往就需要使用到共享存储了,比如最简单常用的网络共享存储 NFS,本节课我们就来介绍下如何在 Kubernetes...

HTML5最新版本介绍

HTML5是HTML4.01和XHTML1.0之后超文本标记语言的最新版本,由一群自由思想者设计,最终实现了多媒体支持、交互性、更智能的表单和更好的语义标注。 HTML 5不只是 HTML规范的最新版本,它是用于生成现代 Web内容的一系列相关技术的总称,其中最重要的三个技术是:HTML5核心规范...

vue v-html动态生成的html怎么加样式/事件

1、动态生成的html,样式不生效//html 布局 <view v-html="html"> {{html}} </view> //动态生成的元素 <view class="btngo" @tap="handleLink...

vue 异步更新那点事儿 #web前端

异步更新那点事儿。wue & vueuse官方团队成员。看一下群友投稿的问题。什么问题?就是它这边有一个组件,这个组件里面有个userID,然后这个userID通过props传给了子组件,子组件是userinfo,它里面是用来渲染用户信息的。渲染用户信息的同时,userinfo这个组件又暴露...

微信研发新功能,或许有你最期待的

微信在我们日常社交中担任着非常重要的角色,不管是用于学习还是工作,我们越来越离不开微信,微信的任何一个小的变化都会影响到现如今超过12亿的微信用户。就在前一段时间,微信更新了一个“拍一拍”的功能,只要双击好友头像,头像就会有抖动并带有文字提示,一时间众多网友在朋友圈疯狂刷屏,虽然觉得这个功能毫无用处...

微信开发的五大价值应用

企业形象展示微网站是企业在移动互联网时代完美展示企业及品牌形象的最佳选择,表现内容丰富、实时更新、形式多样化,保证品牌形象的有效传播!微网站带来的轻营销模式,更适应现代网站的发展模式,所以微网站的开发也具有更好的商业营销效果,其面对的受众是7亿多的微信用户,蕴含着无限的商机。将企业微网站植入微信公众...