前端大屏适配方案

时间:2025-04-08 07:51:10

屏幕适配尺寸:

① 室内P1.5全彩屏:尺寸3.84 * 2.5 = 9.6平米;分辨率2064 * 1290
② 显示尺寸98inch,分辨率3840 * 2160,显示比例16 : 9

适配按16:9

1、px 转换 rem:

使用统一的转换插件

(function(win, lib) {
  let doc = 
  let docEl = 
  let metaEl = ('meta[name="viewport"]')
  let flexibleEl = ('meta[name="flexible"]')
  let dpr = 0
  let scale = 0
  let tid
  let flexible =  || ( = {})

  if (metaEl) {
    ('将根据已有的meta标签来设置缩放比例')
    let match = ('content').match(/initial\-scale=([\d\.]+)/)
    if (match) {
      scale = parseFloat(match[1])
      dpr = parseInt(1 / scale)
    }
  } else if (flexibleEl) {
    let content = ('content')
    if (content) {
      let initialDpr = (/initial\-dpr=([\d\.]+)/)
      let maximumDpr = (/maximum\-dpr=([\d\.]+)/)
      if (initialDpr) {
        dpr = parseFloat(initialDpr[1])
        scale = parseFloat((1 / dpr).toFixed(2))
      }
      if (maximumDpr) {
        dpr = parseFloat(maximumDpr[1])
        scale = parseFloat((1 / dpr).toFixed(2))
      }
    }
  }

  if (!dpr && !scale) {
    let isAndroid = (/android/gi)
    let isIPhone = (/iphone/gi)
    let devicePixelRatio = 
    if (isIPhone) {
      // iOS下,对于2和3的屏,用2倍的方案,其余的用1倍方案
      if (devicePixelRatio >= 3 && (!dpr || dpr >= 3)) {
        dpr = 3
      } else if (devicePixelRatio >= 2 && (!dpr || dpr >= 2)) {
        dpr = 2
      } else {
        dpr = 1
      }
    } else {
      // 其他设备下,仍旧使用1倍的方案
      dpr = 1
    }
    scale = 1 / dpr
  }

  ('data-dpr', dpr)
  if (!metaEl) {
    metaEl = ('meta')
    ('name', 'viewport')
    ('content', 'initial-scale=' + scale + ', maximum-scale=' + scale + ', minimum-scale=' + scale + ', user-scalable=no')
    if () {
      (metaEl)
    } else {
      let wrap = ('div')
      (metaEl)
      ()
    }
  }

  function refreshRem() {
    let width = ().width
    if (width / dpr > 540) {
      width = width * dpr
    }
    let rem = width / 10
     = rem + 'px'
     =  = rem
  }

  ('resize', function() {
    clearTimeout(tid)
    tid = setTimeout(refreshRem, 300)
  }, false)
  ('pageshow', function(e) {
    if () {
      clearTimeout(tid)
      tid = setTimeout(refreshRem, 300)
    }
  }, false)

  if ( === 'complete') {
     = 12 * dpr + 'px'
  } else {
    ('DOMContentLoaded', function(e) {
       = 12 * dpr + 'px'
    }, false)
  }


  refreshRem()

   =  = dpr
   = refreshRem
  flexible.rem2px = function(d) {
    let val = parseFloat(d) * 
    if (typeof d === 'string' && (/rem$/)) {
      val += 'px'
    }
    return val
  }
  flexible.px2rem = function(d) {
    let val = parseFloat(d) / 
    if (typeof d === 'string' && (/px$/)) {
      val += 'rem'
    }
    return val
  }

})(window, window['lib'] || (window['lib'] = {}))

安装 px2rem-loader

npm install px2rem-loader --save-dev

配置 px2rem-loader

vue-cli
在build/中,找到

const px2remLoader = {
    loader: 'px2rem-loader',
    options: {
       // 以设计稿1920为例, 1920 / 10 = 192.0
      remUnit: 192.0
    }
  }
继续找到generateLoaders中的loaders配置,作出如下配置:
// 注释掉这一行
// const loaders =  ? [cssLoader, postcssLoader] : [cssLoader]
// 修改为
    const loaders =  ? [cssLoader, postcssLoader,px2remLoader] : [cssLoader,px2remLoader]

vue-cli

 = {
    css: {
        loaderOptions: {
            css: {},
            postcss: {
                plugins: [
                    require('postcss-px2rem')({
                        // 以设计稿1920为例, 1920 / 10 = 192.0
                        remUnit: 192.0
                    }),
                ]
            }
        }
    },
}

注意说明:
1、有关px的样式不能写内联样式(style),只能写class
2、最外层图层宽高可使用100vw,100vh。内部模块视情况而定(可采用百分比或者px),建议高度使用vh。
3、自适应屏幕的前提需要保证最终大屏尺寸完整适配,在兼容其他设备屏幕适配。
4、文字的大小,图片的大小不能默认适配,必须设置宽高尺寸,文字尺寸,不然无法转换rem。切换大屏尺寸大小不会改变。

2、Echarts 适配方案:

1、画布的大小可设置百分比,让画布随着父视图改变,也可设置px。
2、Echarts 文字大小,布局位置可按函数(echartsSize)转换。

/* Echarts图表字体、间距自适应  */
// 以设计稿1920为例
export const echartsSize = (size, defalteWidth = 1920) => {
  let clientWidth =  ||  || 
  if (!clientWidth) return size
  let scale = (clientWidth / defalteWidth)
  return Number((size * scale).toFixed(3))
}
// 将其挂载到原型链上,方便全局可使用
 = echartsSize

使用说明:
fontSize: (12),
radius: [(30), (140)],

3、 px 转 vh vw

使用@mixin @include

$defalteWidth: 1920;  // 设计尺寸
$defalteHeight: 1080;
$widthScale: 100/$defalteWidth;
$heightScale: 100/$defalteHeight;
@mixin height($radius) {
    height:($radius*heightScale)*1vh;
  }
  @mixin line-height($radius) {
    // line-height: $radius;
    line-height:($radius*heightScale)*1vh;
  }
  @mixin width($radius) {
    // width: $radius;
    width:($radius*defalteWidth)*1vw;
  }
  @mixin p-l($radius) {
    // padding-left: $radius;
    padding-left:($radius*defalteWidth)*1vw;
  }
  @mixin p-r($radius) {
    // padding-right: $radius;
    padding-right:($radius*defalteWidth)*1vw;
  }
  @mixin p-t($radius) {
    // padding-top: $radius;
    padding-top:($radius*defalteWidth)*1vh;
  }
  @mixin p-b($radius) {
    // padding-bottom: $radius;
    padding-bottom:($radius*defalteWidth)*1vh;
  }
  @mixin m-l($radius) {
    // margin-left: $radius;
     margin-left:($radius*defalteWidth)*1vw;
  }
  @mixin m-r($radius) {
    // margin-right: $radius;
    margin-right:($radius*defalteWidth)*1vw;
  }
  @mixin m-t($radius) {
    // margin-top: $radius;
    margin-top:($radius*defalteWidth)*1vh;
  }
  @mixin m-b($radius) {
    // margin-bottom: $radius;
    margin-bottom:($radius*defalteWidth)*1vh;
  }
  @mixin top($radius) {
    // margin-bottom: $radius;
    top:($radius*defalteWidth)*1vh;
  }
  @mixin bottom($radius) {
    // margin-bottom: $radius;
    bottom:($radius*defalteWidth)*1vh;
  }
  @mixin right($radius) {
    // margin-bottom: $radius;
    right:($radius*defalteWidth)*1vh;
  }
  @mixin left($radius) {
    // margin-bottom: $radius;
    left:($radius*defalteWidth)*1vh;
  }
 // 自行添加需要的属性

@include height(240px);

使用建议
宽高可使用vw/vh,但是需要设置最小宽高度,以免小屏压缩排班错位
字体尺寸使用rem
Echarts图标尺寸使用echartsSize转换