移動(dòng)端屏幕適配方案

根據(jù)移動(dòng)端配置,先安裝

安裝

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

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

配置postcss.config.js

在項(xiàng)目根目錄創(chuàng)建文件postcss.config.js ;
按需輸入以下內(nèi)容:

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')
  },
  // 模擬器平臺(tái)
  isSimulatorApp() {
    const platform = navigator.platform.toLowerCase()
    return !((this.isIphone() && platform.includes('iphone')) || (this.isAndroid() && platform.includes('linux')) || this.isHarmonyOs())
  },
  // 微信平臺(tái)
  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 () {
    // 動(dòng)態(tài)設(shè)置根元素字體大小
    function setRemUnit() {
      // 屏幕寬度(設(shè)備獨(dú)立像素)
      const deviceWidth = document.documentElement.clientWidth || window.innerWidth;
      // 限制最大寬度(避免在平板等大屏設(shè)備上過大)
      const maxWidth = 750;
      const scale = deviceWidth > maxWidth ? maxWidth / deviceWidth : 1;
      // 根元素字體大小 = 屏幕寬度 * scale / 10(與 rootValue 對(duì)應(yīng))
      document.documentElement.style.fontSize = (deviceWidth * scale) / 10 + 'px';
    }

    // 初始化
    setRemUnit();
    // 監(jiān)聽窗口大小變化(橫豎屏切換、窗口縮放)
    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'
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • """1.個(gè)性化消息: 將用戶的姓名存到一個(gè)變量中,并向該用戶顯示一條消息。顯示的消息應(yīng)非常簡(jiǎn)單,如“Hello ...
    她即我命閱讀 5,023評(píng)論 0 6
  • 為了讓我有一個(gè)更快速、更精彩、更輝煌的成長(zhǎng),我將開始這段刻骨銘心的自我蛻變之旅!從今天開始,我將每天堅(jiān)持閱...
    李薇帆閱讀 2,244評(píng)論 1 4
  • 似乎最近一直都在路上,每次出來走的時(shí)候感受都會(huì)很不一樣。 1、感恩一直遇到好心人,很幸運(yùn)。在路上總是...
    時(shí)間里的花Lily閱讀 1,739評(píng)論 1 3
  • 1、expected an indented block 冒號(hào)后面是要寫上一定的內(nèi)容的(新手容易遺忘這一點(diǎn)); 縮...
    庵下桃花仙閱讀 1,077評(píng)論 1 2
  • 一、工具箱(多種工具共用一個(gè)快捷鍵的可同時(shí)按【Shift】加此快捷鍵選取)矩形、橢圓選框工具 【M】移動(dòng)工具 【V...
    墨雅丫閱讀 1,514評(píng)論 0 0

友情鏈接更多精彩內(nèi)容