跳到主要内容

适配

  • px 转成 rem
    • amfe-flexible
  • px 转成 vw/vh

px 转成 rem​

只会自动转换 CSS 文件/标签中的 px,不会自动转换 JS 里动态生成的 style(如行内样式)

  • css样式写px可以自动转换rem
  • style行内样式无法自动转换,需要通过工具方法来实现

px2rem方法​

  • base 参数要和你 amfe-flexible 或 postcss-px2rem 的 rootValue 保持一致(常见 75 或 37.5)。
js
// src/utils/px2rem.ts
export function px2rem(px: number, base: number = 75): string {
// base=75 适配 750 设计稿,base=37.5 适配 375 设计稿
return `${px / base}rem`
}

方案:

  • amfe-flexible
  • px2rem