Configuration PrefaceProject construction: built based on vue-cli3, using postcss-px2rem px2rem-loader for rem adaptation Implementation principle: Every time we package, webpack automatically converts px units into rem units by using the plug-in postcss-px2rem px2rem is a plug-in that automatically converts px to rem, helping developers reduce the calculation process of mutual conversion between pixels. There is a pit ahead: some components of the UI framework use JavaScript to write CSS as inline styles directly in HTML tags. The relevant CSS will not be read during packaging and adaptation, so you need to configure the relevant styles, and "!important" is required in the style to override the style. Implementation stepsThe first step is to install postcss-px2rem and px2rem-loader Open the command line tool and enter the following command to install the plug-in (of course, it will be faster to install it using the Taobao mirror cnpm) npm install postcss-px2rem px2rem-loader --save The second step is to create a new rem.js file in the util directory in the root directory src. // rem proportional adaptation configuration file // Base size const baseSize = 16 // Set rem function function setRem () { // The scaling ratio of the current page width relative to 1920 width, which can be modified according to your needs. const scale = document.documentElement.clientWidth / 1920 // Set the font size of the root node of the page ("Math.min(scale, 2)" means the maximum magnification ratio is 2, which can be adjusted according to actual business needs) document.documentElement.style.fontSize = baseSize * Math.min(scale, 2) + 'px' } // Initialize setRem() // Reset rem when changing window size window.onresize = function () { setRem() } The third step is to introduce the adapter file in main.js import './util/rem' Step 4: Configure the plugin in vue.config.js // Import the proportional adaptation plug-in const px2rem = require('postcss-px2rem') // Configure the basic size const postcss = px2rem({ // Base size baseSize, needs to be the same as in rem.js remUnit: 16 }) // Use the proportional adaptation plugin module.exports = { lintOnSave: true, css: { loaderOptions: { postcss: { plugins: [ postcss ] } } } } SummarizeThis is the end of this article about vue+px2rem to achieve large-screen adaptation on PC. For more relevant vue+px2rem large-screen adaptation content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Detailed Analysis of or, in, union and Index Optimization in MySQL
>>: Windows Server 2019 IIS10.0+PHP(FastCGI)+MySQL Environment Construction Tutorial
download MySQL official download, select Windows ...
The following introduces the commonly used head s...
Cause of failure Today, when I was writing a caro...
I have been working on a project recently - Budou...
Please open the test page in a mainstream browser...
Since I started working on Vulhub in 2017, I have...
Recently, I participated in the development of th...
Preface HTTP and HTTPS In our daily life, common ...
1. Dynamic parameters Starting from 2.6.0, you ca...
1. Add fields: alter table table name ADD field n...
1. List The list ul container is loaded with a fo...
closure service nginx stop systemctl stop nginx s...
Table of contents Preface Front-end structure Bac...
Table of contents 1. The significance of users an...
We know that there are two ways to receive incomi...