移动端配置postcss-pxtorem

  1. 安装依赖:postcss-pxtorem 5.1.1
  2. vue2项目 在根目录下新增.postcssrc.js 关键要配置rootValue和propList
module.exports = {
  plugins: [
      'autoprefixer',
      ['postcss-pxtorem', {
        rootValue: 100, // 换算基数 12*100
        unitPrecision: 3, 
        propList: ['*'],
        exclude: /(node_module)/, // 默认false,,过滤/(node_module)/ 。如果想把前端UI框架内的px也转换成rem,就不要过滤
        mediaQuery: false, // (布尔值)允许在媒体查询中转换px。
        minPixelValue: 1 // 设置要替换的最小像素值
      }]
    ]
};

3、在移动端的入口文件配置设置根目录font-size的方法

  • rem 适配方法
// 设备判断
export default function remSize(noRest) {
  let winWidth;
  // 获取窗口宽度
  if (window.innerWidth) {
    winWidth = window.innerWidth;
  } else if ((document.body) && (document.body.clientWidth)) {
    winWidth = document.body.clientWidth;
  }
  // 通过深入Document内部对body进行检测,获取窗口大小
  if (
    document.documentElement
    && document.documentElement.clientHeight
    && document.documentElement.clientWidth
  ) {
    winWidth = document.documentElement.clientWidth;
  }
  // 修改font-size
  const fontSize = ((winWidth / 375) * 100).toFixed(4);
  document.documentElement.style.fontSize = `${fontSize}px`;

  // 适配对font-size额外处理的手机
  const nowFontSize = parseFloat(getComputedStyle(document.documentElement, false)['font-size']).toFixed(4);
  if (noRest !== true && `${nowFontSize}` !== fontSize) {
    document.documentElement.style.fontSize = `${(fontSize * fontSize) / nowFontSize}px`;
  }
}

  • main.js中使用
import remSize from './remSize';

let noReset;

if (window.top === window.self) {
  window.onresize = () => {
    remSize(noReset);
  };

  // 安卓10以上不需要处理修改系统字体大小
  if (判断是否安卓) {
    Vue.prototype.$isAndroid10.getHTTPRequestHeaders().then(({ data = {} }) => {
      const version = data['x-rpc-sys_version'] || '';
      const [v] = version.split('.');
      noReset = Number(v) >= 10;
      remSize(noReset);
    });
  } else {
    remSize(noReset);
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
移动端进行REM适配可以使用PostCSS插件postcss-pxtorem来自动转换像素单位为REM单位。面是配置postcss-pxtorem的骤: 1. 首先,确保你已在项目安装了postcsspostcsspxtorem插件。 2. 在项目根目录下创建一个postcss.config.js文件,并在该文件进行配置。 ```javascript module.exports = { plugins: { 'postcss-pxtorem': { rootValue: 16, // 根据设计稿的实际尺寸设置 unitPrecision: 5, // 转换后的REM值保留的小数位数 propList: ['*'], // 需要转换的CSS属性,默认为全部 selectorBlackList: [], // 需要忽略的CSS选择器,例如['.ignore'],可以使用正则表达式 replace: true, // 是否替换原有的CSS规则 mediaQuery: false, // 是否将媒体查询的像素单位也转换为REM单位 minPixelValue: 0 // 小于该数值的像素单位不转换 } } } ``` 上述配置,rootValue代表根元素(html)的字体大小,一般与设计稿的实际尺寸对应。unitPrecision用于设置转换后的REM值保留的小数位数。propList是一个数组,用于指定需要转换的CSS属性,默认为全部。selectorBlackList用于指定需要忽略的CSS选择器,可以使用正则表达式。replace用于设置是否替换原有的CSS规则。mediaQuery用于指定是否将媒体查询的像素单位也转换为REM单位。minPixelValue用于设置小于该数值的像素单位不转换。 3. 在项目postcss.config.js文件添加配置后,你可以在构建工具(例如Webpack)的配置文件使用postcss-loader加载并应用PostCSS插件。 ```javascript module.exports = { // ... module: { rules: [ // ... { test: /\.css$/, use: ['style-loader', 'css-loader', 'postcss-loader'] }, // ... ] } // ... } ``` 以上配置,使用了style-loader、css-loader和postcss-loader来处理CSS文件。具体的配置方式可能会因你所使用的构建工具而有所不同,请根据实际情况进行相应的调整。 总之,通过配置postcss-pxtorem插件,你可以实现移动端REM适配,使得页面在不同设备上展示更加统一和适配。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值