Example code for implementing large screen adaptation on PC using vue+px2rem (rem adaptation)

Example code for implementing large screen adaptation on PC using vue+px2rem (rem adaptation)

Configuration Preface

Project 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 steps

The 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
        ]
      }
    }
  }
}

Summarize

This 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:
  • Example of how to adapt the Vue project to the large screen

<<:  Detailed Analysis of or, in, union and Index Optimization in MySQL

>>:  Windows Server 2019 IIS10.0+PHP(FastCGI)+MySQL Environment Construction Tutorial

Blog    

Recommend

How to install MySQL 5.7.17 and set the encoding to utf8 in Windows

download MySQL official download, select Windows ...

HTML head structure

The following introduces the commonly used head s...

Solution to overflow:hidden failure in CSS

Cause of failure Today, when I was writing a caro...

Two usages of iFrame tags in HTML

I have been working on a project recently - Budou...

Measured image HTTP request

Please open the test page in a mainstream browser...

Six ways to reduce the size of Docker images

Since I started working on Vulhub in 2017, I have...

WeChat applet development chapter: pitfall record

Recently, I participated in the development of th...

Nginx domain name SSL certificate configuration (website http upgraded to https)

Preface HTTP and HTTPS In our daily life, common ...

How to use dynamic parameters and calculated properties in Vue

1. Dynamic parameters Starting from 2.6.0, you ca...

How to add fields and comments to a table in sql

1. Add fields: alter table table name ADD field n...

Detailed explanation of HTML basics (Part 2)

1. List The list ul container is loaded with a fo...

How to shut down/restart/start nginx

closure service nginx stop systemctl stop nginx s...

How to manage large file uploads and breakpoint resume based on js

Table of contents Preface Front-end structure Bac...

Introduction to user management under Linux system

Table of contents 1. The significance of users an...