vue+vite2.0使用postcss-pxtorem实现大屏(或移动端)布局自适应(px转rem)

1 篇文章 0 订阅

第一种用法:

 postcss-pxtorem

​​​​​​PostCSS 的一个插件,可以从像素单位生成 rem 单位。

amfe-flexible

amfe-flexible是配置可伸缩布局方案,主要是将1rem设为viewWidth/10

1、安装

npm install postcss-pxtorem --save
npm install amfe-flexible --save
npm install autoprefixer --save


2、main.js中引入

import 'amfe-flexible'  //或者 import 'amfe-flexible/index.js'


3、vite.config.js中配置 我这里引入了vant4 注意要忽略vant 不进行rem转换

import { fileURLToPath, URL } from "node:url";
import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import Components from "unplugin-vue-components/vite";
import { VantResolver } from "unplugin-vue-components/resolvers";
import autoprefixer from "autoprefixer";
import postCssPxToRem from "postcss-pxtorem";
// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    Components({
      resolvers: [VantResolver()],
    }),
  ],
  css: {
    postcss: {
      plugins: [
        autoprefixer({
          overrideBrowserslist: [
            "Android 4.1",
            "iOS 7.1",
            "Chrome > 31",
            "ff > 31",
            "ie >= 8",
          ],
        }),
        postCssPxToRem({
          // 自适应,px>rem转换
          rootValue: 75, // 75表示750设计稿,37.5表示375设计稿
          propList: ["*"], // 需要转换的属性,这里选择全部都进行转换 也可以'font', 'font-size', 'line-height', 'letter-spacing'等
          selectorBlackList: [".van"], // 过滤掉van-开头的class,不进行rem转换
          exclude: "/node_modules", // 忽略包文件转换rem
        }),
      ],
    },
  },
  resolve: {
    alias: {
      "@": fileURLToPath(new URL("./src", import.meta.url)),
    },
  },
});


 

第二种用法:

1.安装

npm i postcss-pxtorem -S

2.与package.json同级目录创建postcss.config.js文件

module.exports = {
    plugins: {
        autoprefixer: {
            overrideBrowserslist: [
                "Android 4.1",
                "iOS 7.1",
                "Chrome > 31",
                "ff > 31",
                "ie >= 8",
                "last 10 versions", // 所有主流浏览器最近10版本用
            ],
            grid: true,
        },
        'postcss-pxtorem': {
            rootValue: 37.5,
            propList: ['*'],
            unitPrecision: 5
        }
    }
}

3.安装 amfe-flexible

npm i amfe-flexible -D

4.main.ts文件中 import 一下

import ‘amfe-flexible/index.js’

5.可能会出现下面情况

[vite] Internal server error: Loading PostCSS Plugin failed: Cannot find module 'autoprefixer'

这时候尝试安装 autoprefixer就可以了

npm i autoprefixer

6.引入一个自适应js

// rem等比适配配置文件
// 基准大小
// baseSize = 16 (fontSize = 16)
// 设置 rem 函数
function setRem() {
  // 当前页面宽度相对于 1920宽的缩放比例,可根据自己需要修改。
  const scale = document.documentElement.clientWidth / 1920; //当前设计稿为1920  如果是750则 替换为 750
  // 设置页面根节点字体大小(“Math.min(scale, 2)” 指最高放大比例为2,可根据实际业务需求调整)
  document.documentElement.style.fontSize = 16 * Math.min(scale, 2) + 'px';
}
// 初始化
setRem();
// 改变窗口大小时重新设置 rem
window.onresize = function () {
  setRem();
  // window.location.reload();
};

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值