本文主要是介绍uniapp ui库 px 转 rpx,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
项目最层新建pxToRpx.js
yarn add postcss --save-dev
pxToRpx.js的代码const postcss = require('postcss');module.exports = postcss.plugin('postcss-px2rpx', function(opts) {opts = opts || {};var timesBigger = opts.times || 2;return function(root) {root.walkDecls(decl => {const val = decl.value;if (val.indexOf('px') > -1) {decl.value = val.replace(/(\d+)px/g, function(match, num) {return num * timesBigger + 'rpx';});}});};
});
配置vue.config.js
module.exports = {css: {loaderOptions: {postcss: {plugins: [require("./pxToRpx.js")({times: 2,//转换倍数})]}}}
}
这篇关于uniapp ui库 px 转 rpx的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!