移动端屏幕适配方案

根据移动端配置,先安装

安装

// npm
npm install postcss-pxtorem --save-dev

// yarn
yarn add postcss-pxtorem --save-dev

配置postcss.config.js

在项目根目录创建文件postcss.config.js ;
按需输入以下内容:

module.exports = {
  plugins: {
    'postcss-pxtorem': {
      rootValue: 75,
      propList: ['*'], // Convert all properties
      unitPrecision: 6, // Decimal places for rem values
      selectorBlackList: ['.van'], // Exclude .van-* classes
      replace: true,
      mediaQuery: false, // Don't convert media queries
      minPixelValue: 2 // Minimum pixel value to convert
    }
  }
}

新建文件 utils/platform.js

const navigator = window.navigator
export default {
  isIphone() {
    const userAgent = navigator.userAgent.toLowerCase()
    return /\(i[^;]+;( U;)? CPU.+Mac OS X/gi.test(userAgent)
  },
  isIphoneXLater() {
    return this.isIphone() && window.screen.height >= 812
  },
  isAndroid() {
    const userAgent = navigator.userAgent.toLowerCase()
    return /android|adr/gi.test(userAgent)
  },
  isApp() {
    const userAgent = navigator.userAgent.toLowerCase()
    return userAgent.includes('@xxxxx')
  },
  // 模拟器平台
  isSimulatorApp() {
    const platform = navigator.platform.toLowerCase()
    return !((this.isIphone() && platform.includes('iphone')) || (this.isAndroid() && platform.includes('linux')) || this.isHarmonyOs())
  },
  // 微信平台
  isWechat() {
    const userAgent = navigator.userAgent.toLowerCase()
    return userAgent.includes('micromessenger') && !userAgent.includes('wxwork')
  },
  isHarmonyOs: function() {
    var tmp = navigator.userAgent.toLowerCase();
    var harmonyos = tmp.indexOf('dcfundharmonyos') > -1;
    return !!harmonyos;
  },
  isHideHeaderWebview() {
    const userAgent = navigator.userAgent.toLowerCase()
    return userAgent.includes('@hideheader')
  }
}

新建文件 utils/rem.js


import platform from '@/utils/platform'
  ; (function () {
    // 动态设置根元素字体大小
    function setRemUnit() {
      // 屏幕宽度(设备独立像素)
      const deviceWidth = document.documentElement.clientWidth || window.innerWidth;
      // 限制最大宽度(避免在平板等大屏设备上过大)
      const maxWidth = 750;
      const scale = deviceWidth > maxWidth ? maxWidth / deviceWidth : 1;
      // 根元素字体大小 = 屏幕宽度 * scale / 10(与 rootValue 对应)
      document.documentElement.style.fontSize = (deviceWidth * scale) / 10 + 'px';
    }

    // 初始化
    setRemUnit();
    // 监听窗口大小变化(横竖屏切换、窗口缩放)
    window.addEventListener('resize', setRemUnit);
    window.addEventListener('orientationchange', setRemUnit);
    platform.isIphone() && document.body.addEventListener("focusout", function () {
      setTimeout(function () {
        document.body.scrollIntoView()
      }, 0)
    })
    window.addEventListener('touchend', function (e) {
      var tag = document.activeElement.tagName || "";
      if ((tag !== 'INPUT' && tag !== 'TEXTAREA') || (e && e.target && e.target.tagName !== 'INPUT' && e.target.tagName !== 'TEXTAREA')) {
        document.activeElement.blur()
      }
    })
  })();

然后在main.js引入即可

import './utils/rem.js'
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • """1.个性化消息: 将用户的姓名存到一个变量中,并向该用户显示一条消息。显示的消息应非常简单,如“Hello ...
    她即我命阅读 8,906评论 0 6
  • 为了让我有一个更快速、更精彩、更辉煌的成长,我将开始这段刻骨铭心的自我蜕变之旅!从今天开始,我将每天坚持阅...
    李薇帆阅读 6,330评论 1 4
  • 似乎最近一直都在路上,每次出来走的时候感受都会很不一样。 1、感恩一直遇到好心人,很幸运。在路上总是...
    时间里的花Lily阅读 5,411评论 1 3
  • 1、expected an indented block 冒号后面是要写上一定的内容的(新手容易遗忘这一点); 缩...
    庵下桃花仙阅读 3,803评论 0 2
  • 一、工具箱(多种工具共用一个快捷键的可同时按【Shift】加此快捷键选取)矩形、椭圆选框工具 【M】移动工具 【V...
    墨雅丫阅读 3,870评论 0 0

友情链接更多精彩内容