前端UI适配

308 阅读3分钟

业界通用方案

方法一:引入淘宝开源的可伸缩布局方案

引入淘宝开源的可伸缩布局方案:github.com/amfe/lib-fl… 淘宝的其实也和viewport的有点像,但是它主要是根据设备设备像素比设置scale的值,保持视口device-width始终等于设备物理像素,屏幕大小动态计算根字体大小,具体是将屏幕划分为10等分。这块也可以直接用js实现,后面会提到 具体引入和使用方法,移步github查看,非常详细。 

方法二:viewport 的使用

github里边,有提到 viewport 的使用。我感觉这篇文章关于viewport 的介绍特别详细,包括比例、是否缩放等的属性介绍特别的详细,虽然文章的内容一大片的字看起来很多,但是请耐心看完,都是干货能很好的让你认识viewport。如果比较着急,请继续往下看总结图吧 www.cnblogs.com/2050/p/3877… 

关于 viewport 的,这块直接引用上面文章的内容,我感觉也是最干脆最直接的总结了吧 。

方法三:使用js+viewport动态设置手动适配rem

我的编辑器是vscode,添加了插件cssrem自动转换。

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <!-- <meta name="viewport" content="width=device-width,initial-scale=1.0"> -->
    <meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
    <!-- 启用360浏览器的极速模式(webkit) -->
    <meta name="renderer" content="webkit">
    <!-- 避免IE使用兼容模式 -->
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <!-- 针对手持设备优化,主要是针对一些老的不识别viewport的浏览器,比如黑莓 -->
    <meta name="HandheldFriendly" content="true">
    <!-- 微软的老式浏览器 -->
    <meta name="MobileOptimized" content="320">
    <!-- uc强制竖屏 -->
    <meta name="screen-orientation" content="portrait">
    <!-- QQ强制竖屏 -->
    <meta name="x5-orientation" content="portrait">
    <!-- UC强制全屏 -->
    <meta name="full-screen" content="yes">
    <!-- QQ强制全屏 -->
    <meta name="x5-fullscreen" content="true">
    <!-- UC应用模式 -->
    <meta name="browsermode" content="application">
    <!-- QQ应用模式 -->
    <meta name="x5-page-mode" content="app">
    <!-- windows phone 点击无高光 -->
    <meta name="msapplication-tap-highlight" content="no">
    <meta content="telephone=no" name="format-detection" />
    <meta name="huaban" content="nopin" />
    <link rel="icon" type="image/x-icon" href="/favicon.ico">
    <title>新茶饮</title>
    <script src="/config.js"></script>
   <script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
  </head>
  <body>
    <div id="app"></div>
    <!-- 
    在iphone 5 中1rem=16px; 
    html font-size =16px=1rem;
  -->
  <script>
    //得到手机屏幕的宽度
    let htmlWidth = document.documentElement.clientWidth || document.body.clientWidth;
    console.log('htmlWidth',htmlWidth)
    //得到html的Dom元素
    let htmlDom = document.getElementsByTagName('html')[0];
    // if(htmlWidth>640){//超过640大小的,字体根部都是16px
    //   htmlWidth=640;
    //   console.log('htmlWidth-true',htmlWidth)
    // }
    //设置根元素字体大小
    htmlDom.style.fontSize = htmlWidth / 40 + 'px';

  </script>
    
  </body>
</html>

方法四:根据css的媒体查询动态设置根部html字体大小

html {font-size: 625%; /*100 ÷ 16 × 100% = 625%*/}

@media screen and (min-width:360px) and (max-width:374px) and (orientation:portrait) {
    html { font-size: 703%; }
}
@media screen and (min-width:375px) and (max-width:383px) and (orientation:portrait) {
    html { font-size: 732.4%; }
}
@media screen and (min-width:384px) and (max-width:399px) and (orientation:portrait) {
    html { font-size: 750%; }
}
@media screen and (min-width:400px) and (max-width:413px) and (orientation:portrait) {
    html { font-size: 781.25%; }
}
@media screen and (min-width:414px) and (max-width:431px) and (orientation:portrait){
    html { font-size: 808.6%; }
}
@media screen and (min-width:432px) and (max-width:479px) and (orientation:portrait){
    html { font-size: 843.75%; }
}

不规则尺寸屏幕适配

以设计图750*1624为例,需要适配各种屏幕。

适配关键点

1、px2rem

安装并设置对应的webpack插件postcss-px2rem-exclude,所有样式包括宽高、定位、字体大小根据设计图给定的px设置,px2rem会自动转换为rem单位。

2、所有设备,页面水平居中显示

根组件布局外层使用flex布局,使得页面显示区域水平居中。第二层设置宽度100%,最大宽度750px(px2rem会自动转为7.5rem),高度100vh。如果不希望滚动则设置overflow:hidden

css 代码解读复制代码<div class='out'>
  <div class='inner'>
      ...
   </div>
</div>

.out{
   display:flex;
   justify-content:center;
}

.inner{
   width:100%;
   max-width:750px;
   height:100vh;
   overflow:hidden;
   position:absolute;
}

3、所有设备,定位元素都限制在页面最大可显示区域内

页面定位元素使用absolute定位,相对于父元素定位。

4、所有设备,页面宽高和字体大小等都符合视觉效果

媒体查询限制html根标签font-size。

css 代码解读复制代码@media screen and (max-width: 320px) {
    html {
        font-size: 2.1rem !important;
    }
}
@media screen and (min-width: 481px) and (max-width: 750px) {
    html {
        font-size: 3.2rem !important;
    }
}
@media screen and (min-width: 751px) {
    html {
        font-size: 4.6rem !important;
    }
}

5、背景图以中心原点定位,尽可能显示最多内容

动态计算屏幕比例。

设置背景图定位为中心原点background-position: center;

不规则宽屏适配:屏幕实际宽高比大于设计稿宽高比750/1624,水平方向100%,垂直方向(100*(屏幕实际宽高比/设计稿宽高比))%。

不规则竖屏适配:屏幕实际宽高比小于等于设计稿宽高比750/1624,垂直方向100%,水平方向100*(设计稿宽高比/屏幕实际宽高比))%。

ini 代码解读复制代码// 屏幕实际宽高、比例
const availWidth = document.querySelector('#pageContainer')?.getBoundingClientRect().width ||
                    document.documentElement.clientWidth ||
                    window.screen.availWidth ||
                    window.innerWidth;

const availHeight = document.querySelector('#pageContainer')?.getBoundingClientRect().height ||
                    document.documentElement.clientHeight ||
                    window.screen.availHeight ||
                    window.innerHeight;

const availRatio = availWidth / availHeight;

// 设计稿宽高、比例
const normalWidth = 750;
const normalHeight = 1624;
const normalRatio = normalWidth / normalHeight;

// 设置背景图定位
.bg {
    position: absolute;
    width: 100%;
    height: 100%;
    background-size: 100% auto;
    background-repeat: no-repeat;
    background-position: center;
    bottom: 0;
}
// 设置背景图宽高
style={{
    backgroundSize:(availRatio > normalRatio && `100% ${(100 * availRatio) / normalRatio}%`) ||
                        `${(100 * normalRatio) / availRatio}% 100%`}}

页面定位元素适配

待完善。。。

参考:

H5移动端适配方案rem/vw

pc端自适应方案

前端基础知识概述 -- 移动端开发的屏幕、图像、字体与布局的兼容适配

移动端高清、多屏适配方案

viewport和1px | 工具人: 这是1px,设计师: 不,这不是

移动端UI一致性解决方案

Web移动端适配方案

移动端适配及PC端适配心得总结体会(一) (可能比较全

自适应布局方案

愈发熟练的 CSS 技巧

web移动端布局的那些事儿

茴字的四种写法—移动适配方案的进化

前端程序员要懂的 UI 设计知识

移动端适配知识你到底知多少

都2020移动端适配你还用flexible.js吗?vw+rem一行代码搞定

REM:web app适配的秘密武器

关于移动端适配,你必须要知道的

如何写一个适配iPhoneX的底部导航

移动端适配问题解决方案

移动端自适应个人理解与收集——rem

轻松掌握移动端web开发【尺寸适配】常用解决方案

面试官:你了解过移动端适配吗?

移动端适配方案总结

拿到一份设计稿,我该如何进行移动端开发?

【移动端适配】用vw、vh+媒体查询打造最完美的移动端适配方案

骚年你的屏幕适配方式该升级了!-今日头条适配方案

移动端适配总结

真的,移动端尺寸自适应与dpr无关

如何利用vw+rem进行移动端布局

移动web必会技能--屏幕适配(物理像素CSS像素)

flex深度剖析-解决移动端适配问题!

第三代移动端布局方案

论低于 12px 字体处理方案

手把手带你撸一个网易云音乐首页 | 适配篇

px、rpx、em、rem 、vw/vh、百分比的区别?

前端兼容性问题 - 屏幕尺寸兼容

前端兼容性问题 - CSS、JS 兼容

谈谈 H5 移动端适配原理