【Vue】Vue3 pc 端配置 rem 响应式

安装插件

shell 复制代码
npm install postcss-pxtorem && amfe-flexible

main.ts

ts 复制代码
import 'amfe-flexible';

vite.config.ts

ts 复制代码
import postCssPxToRem from 'postcss-pxtorem';

...

  // 响应式设置
  css: {
    postcss: {
      plugins: [
        postCssPxToRem({
          rootValue: 192, // 1rem的大小
          propList: ['*'], // 需要转换的属性,这里选择全部都进行转换
        }),
      ],
    },
  },

App.vue

ts 复制代码
import { BrowserSize } from '@/utils/index';
onMounted(() => {
  BrowserSize();
});

// @/utils/index
export function BrowserSize() {
  var docEl = document.documentElement,
    clientWidth: number,
    clientHeight: number,
    clientWidth = docEl.clientWidth;
  clientHeight = docEl.clientHeight;
  if (!clientWidth) return;
  if (!docEl.addEventListener) return;
  // clientWidth = 1920;

  // if (clientWidth < 1920) {
  //   clientWidth = clientWidth;
  // }

  docEl.style.fontSize = clientWidth / 10 + 'px';
}
相关推荐
RDCJM32 分钟前
Spring Boot + Vue 全栈开发实战指南
vue.js·spring boot·后端
Shi_haoliu8 小时前
openClaw源码部署-linux
前端·python·ai·openclaw
程序员小寒9 小时前
前端性能优化之白屏、卡顿指标和网络环境采集篇
前端·javascript·网络·性能优化
烛阴9 小时前
Claude CLI AskUserQuestion 工具详解:让 AI 开口问你
前端·claude
wal13145209 小时前
OpenClaw教程(九)—— 彻底告别!OpenClaw 卸载不残留指南
前端·网络·人工智能·chrome·安全·openclaw
mon_star°10 小时前
在TypeScript中,接口MenuItem定义中,为什么有的属性带问号?,有的不带呢?
前端
牛奶10 小时前
分享一个开源项目,让 AI 辅助开发真正高效起来
前端·人工智能·全栈
次顶级11 小时前
表单多文件上传和其他参数处理
前端·javascript·html
kuntli11 小时前
Vue生命周期全解析
vue.js