postcss-px-to-viewport做前端自适应,适用于pc和移动
注意 ⚠️:
1、直接在dom元素上使用 style 是不会被转换的
2、用类选择器、id选择器等才会被转换
3、element-ui、vant-ui 等UI组件库 默认会被转换
1.下载 postcss-px-to-viewport 插件
npm i postcss-px-to-viewport --save-dev
2.在vite.config.ts中添加配置
第一种配置:
import pxtovw from 'postcss-px-to-viewport'
const loder_pxtovw = pxtovw({
//这里是设计稿宽度 自己修改
viewportWidth: 1920,
viewportUnit: 'vw'
})
export default defineConfig({
plugins: [vue()],
css: {
postcss: {
plugins: [loder_pxtovw]
}
}
})
第二种配置:
postcss: {
plugins: [
require("postcss-px-to-viewport")({
unitToConvert: "px",// 要转化的单位
viewportWidth: 750,// UI设计稿的宽度
unitPrecision: 3,// 转换后的精度,即小数点位数
propList: [
"*"
],// 指定转换的css属性的单位,*代表全部css属性的单位都进行转换
viewportUnit: "vw",// 指定需要转换成的视窗单位,默认vw
fontViewportUnit: "vw",// 指定字体需要转换成的视窗单位,默认vw
landscapeUnit: 'vh',// 横屏时使用的单位
landscapeWidth: 667,// 横屏时使用的视口宽度
selectorBlackList: [],// 指定不转换为视窗单位的类名
minPixelValue: 1,// 默认值1,小于或等于1px则不进行转换
mediaQuery: false,// 是否在媒体查询的css代码中也进行转换,默认false
replace: true,// 是否转换后直接更换属性值
// landscape: false, // 是否处理横屏情况
exclude: /(\/|\\)(node_modules)(\/|\\)/, // 设置忽略文件,用正则做目录名匹配
})
]
}
of course like your web site but you need to test the spelling on quite a few of your posts. A number of them are rife with spelling problems and I in finding it very troublesome to inform the reality nevertheless I will surely come again again.
新年快乐!
十天看一部剧,还可以吧
@梦不见的梦 行,谢谢提醒,我优化一下
网站的速度有待提升,每次打开都要转半天还进不来呢
@React实战爱彼迎项目(二) - 程序员鸡皮 哪里有问题了,报错了吗?
@Teacher Du 那是怕你们毕不了业,我大学那会儿给小礼品
我们大学那会,献血还给学分~
@ab 我想去学网安,比如网警,但分也贼高😕
@夜 加油,你一样也可以成为程序员的,需要学习资料可以V我