CSS自适应分辨率 postcss-pxtorem(适用于 Vite)

前言

此篇是基于 Vite + Vu3 项目的 CSS 自适应分辨率!
如果想知道基于 Webpack + Vue2 可移步 《CSS自适应分辨率 amfe-flexible 和 postcss-pxtorem(适用于 Webpack)》
项目对应的主要插件版本如下:

"vite": "^4.4.5"
"vue": "^3.3.4"
"@vitejs/plugin-vue": "^4.2.3"

CSS 插件

postcss-pxtorem:使用 rem 代替 pxpostcss 插件,它可以自动将 px 转换成 rem,并且对一些特殊情况进行处理。

额外说明:插件 amfe-flexible 不再使用,最近更新是 6 年前…且在 Vite 中会报错,稍后会有替代方案!

安装

最新版 v6.1.0 报错,暂不细查原因… 使用固定版本 v6.0.0 ,此版本功能满足需求…
额外说明:最新版 v6.1.0 ,发布于 2024.01.20(写此篇文章的前5天左右)。而上个版本 v6.0.0 发布于 3 年前

npm i postcss-pxtorem@6.0.0 -D

配置

  1. 文件 index.html配置
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
  1. 根目录下配置文件 postcss.config.js。如下仅有部分配置,想了解更多配置可进入此处

    module.exports = {
        "plugins": {
            "postcss-pxtorem": {
                rootValue: 16, // 16px = 1rem
                // unitPrecision: 5,
                propList: ['*'],
                // selectorBlackList: ['el-',], //
                // replace: true,
                // mediaQuery: false,
                // minPixelValue: 0
            }
        }
    }
    
  2. 根目录下新建 utils/rem.js 文件。其实是替代了插件 postcss-pxtorem 的功能。使用 window.onresize 监听窗口变化。

    // 设置 rem 函数
    function setRem() {
        // 1920 默认大小16px; 1920px = 120rem ;每个元素px基础上/16
        const screenWidth = 1920
        const scale = screenWidth / 16
        const htmlWidth = document.documentElement.clientWidth || document.body.clientWidth
        // 得到html的Dom元素
        const htmlDom = document.getElementsByTagName('html')[0]
        // 设置根元素字体大小
        htmlDom.style.fontSize = htmlWidth / scale + 'px'
      }
    // 初始化
    setRem()
    // 改变窗口大小时重新设置 rem
    window.onresize = function() {
        setRem()
    }
    

    或按高度来,适用于大屏。特别是页面左右两侧有大屏选项卡时。注意差异在第 2、4 行。按照高度

        // 按高度来
        const screenHeight = 1080
        const scale = screenHeight / 16
        const htmlHeight = document.documentElement.clientHeight || document.body.clientHeight
            // 得到html的Dom元素
        const htmlDom = document.getElementsByTagName('html')[0]
            // 设置根元素字体大小
        htmlDom.style.fontSize = htmlHeight / scale + 'px'
    

    4.main.js 引入

    import '@/utils/rem.js'
    

注意事项

  • 如果 CSS 代码在 css 文件或 <script> 标签内,直接使用单位 px,框架自动转 rem

  • 如果 CSS 代码写在 html 标签上,无法识别和转换,请修改。请尽量使用类名

    <!-- 无法转换 -->
    <span style="width:20px;height:20px;"></span>
    
    <!-- 可转换 -->
    <span class="top-left"></span>
    
  • 图片修改,且注意 <img> 图片也要设置高宽;

    background: url("/images/bigScreen/biaoti.png");
    // 必须加上 cover
    background-size: cover;
    

如果报错

报错 ReferenceError: module is not defined in ES module scope
在这里插入图片描述
根据上图报错提示,将文件 postcss.config.js 后缀改为 postcss.config.cjs。注意是 .cjs

VSCode 插件推荐

扩展中搜 cssrem,安装即可:

在这里插入图片描述

鼠标放在 px 那行,上右侧就会自动换算成 rem

在这里插入图片描述

  • 19
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vite 中使用 postcss-pxtorem 插件可以将 CSS 文件中的像素单位(px)转换为 rem 单位。这样做的好处是可以方便地适配不同的屏幕尺寸。 以下是在 Vite 中使用 postcss-pxtorem 的步骤: 1. 首先,确保你的项目已经安装了 postcsspostcss-pxtorem 插件。你可以使用以下命令进行安装: ```shell npm install postcss postcss-pxtorem --save-dev ``` 2. 在项目根目录下创建一个 postcss.config.js 文件,并添加以下内容: ```javascript module.exports = { plugins: [ require('postcss-pxtorem')({ rootValue: 16, // 屏幕宽度基准值,比如设计稿是以 375px 宽度为基准,设置为 16 则表示 1rem = 16px propList: ['*'], // 需要转换的 CSS 属性,* 表示所有 selectorBlackList: [], // 需要忽略的 CSS 选择器,比如 '.ignore' 表示不转换 .ignore 类下的样式 minPixelValue: 2 // 小于该值的像素单位不进行转换 }) ] }; ``` 3. 在 Vite 的配置文件 vite.config.js 中添加 postcss 插件配置。假设你已经有一个名为 `vue.config.js` 的配置文件,你可以修改它如下: ```javascript import { defineConfig } from 'vite'; export default defineConfig({ // 其他配置项... css: { postcss: { plugins: [ require('postcss') ] } } }); ``` 或者,如果你使用的是 Vite 1.x 版本,可以修改为: ```javascript module.exports = { // 其他配置项... css: { postcss: { plugins: [ require('postcss') ] } } }; ``` 4. 现在你可以在 CSS 文件中使用像素单位了,它们会自动被转换为 rem 单位。例如: ```css .container { width: 375px; font-size: 16px; padding: 10px 20px; } ``` 经过 postcss-pxtorem 插件的转换后,会变成: ```css .container { width: 23.4375rem; font-size: 1rem; padding: 0.625rem 1.25rem; } ``` 这样,你就成功地在 Vite 中使用了 postcss-pxtorem 插件来进行像素单位转换了。希望对你有所帮助!如果还有其他问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值