This is the first article in the Year of the Rat!
It is also the first task to complete the development of the WeChat public account mobile terminal project after the forced holiday when we just started working remotely!
Without much talk, vue goes!
1、 Installing the lib flexible plug-in
npm i -S amfe-flexible
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
//Directly import it in the main.js file (the file is in the src directory) import 'amfe-flexible/index.js'; //Or add this tag in the index.html file <script src="./node_modules/amfe-flexible/index.js"></script>
2、 Install the postcss pxtorem plug-in
npm install postcss-pxtorem --save-dev
module.exports = { css: { loaderOptions: { postcss: { plugins: [ Require ('postcss-pxtorem ') ({//Convert px units to rem units RootValue: 75,//The converted base (divide the design draft with a width of 750px into 10 equal parts, each of which is 75px, that is, 1rem=75px) propList: ['*'], // selectorBlackList: [ // "van-", // "weui" // ], //Styles starting with "van -" and "weui" do not automatically perform rem conversion }) ] } } } };
The key point is here, circle it and take the exam!
//Write in main.js //REM adaptation: set the size of font size in HTML function setRem () { let htmlWidth = document.documentElement.clientWidth || document.body.clientWidth; let htmlDom = document.getElementsByTagName('html')[0]; htmlDom.style.fontSize = (htmlWidth / 10) > 75 ? "75px" : htmlWidth / 10 + 'px'; // Set the maximum 1rem not to exceed 75px // htmlDom.style.fontSize = htmlWidth / 10 + 'px'; // Previously, 1 rem was equal to screen width divided by 10 } setRem(); // initialization window.onresize = function () { setRem(); }
/*Set the style for the largest parent container, which may be # app in the App.vue file*/ #app { max-width: 750px; /* The plug-in will automatically parse into 10rem*/ margin: 0 auto; }