Sample code for implementing PC resolution adaptation in Vue

Sample code for implementing PC resolution adaptation in Vue

plan

  • lib-flexible + px2remLoader
  • lib-flexible : Alibaba's scalable layout solution
  • px2rem-loader : px to rem

Install Dependencies

npm install px2rem-loader -D
npm install lib-flexible -S

Introducing dependencies

main.js imports lib-flexible

import 'lib-flexible'

Convert px to rem

options of vue-loader and other style file loader are ultimately generated by the methods in build/utils.js . We only need to add a px2remLoader after cssLoader . The remUnit option of px2rem-loader means 1rem = how many pixels. Combined with the solution of lib-flexible , we set options.remUnit of px2remLoader to 1/10 of the width of the design draft. Here we assume that the width of the design draft is 1920px

Add px2remLoader in build/utils.js

const cssLoader = {
    loader: 'css-loader',
    options:
      sourceMap: options.sourceMap
    }
  }

  // Add code, px to rem configuration (need to add px2remloader to the loaders array)
  const px2remLoader = {
    loader: 'px2rem-loader',
    options:
      remUnit: 192, //According to the visual draft, rem is one tenth of px, 1920px 192 rem
      // remPrecision: 8 // How many decimal places are retained in the converted rem}
  }

Put it in the loaders array

// generate loader string to be used with extract text plugin
function generateLoaders (loader, loaderOptions) {
   const loaders = options.usePostCSS ? [cssLoader, postcssLoader, px2remLoader] : [cssLoader, px2remLoader]

   if (loader) {
     loaders.push({
       loader: loader + '-loader',
       options: Object.assign({}, loaderOptions, {
         sourceMap: options.sourceMap
       })
     })
   }
   //...
 }

Modify flexible.js

Global search for flexible.js

Modify the code to suit the PC side

function refreshRem(){
   var width = docEl.getBoundingClientRect().width;
    if (width / dpr > 540) {
        width = width * dpr;
    }
    //Scaling ratio, can be modified according to actual situation var rem = width / 8;
    docEl.style.fontSize = rem + 'px';
    flexible.rem = win.rem = rem;
}

For styles that you do not want to be converted, you can add /*no*/ after them to ensure that they are not converted.

Reference blog

VUE PC-side adaptation solution flexible + px2remLoader感謝大佬
Vue realizes PC resolution adaptation感謝大佬

This concludes this article about the sample code for Vue's PC-side resolution adaptation. For more information about Vue's PC-side resolution adaptation, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Vue implements PC resolution adaptation operation
  • Vue implements the code of floating ball on the side of PC
  • Create a PC-side Vue UI component library from scratch (counter component)
  • Vue achieves scrolling to the bottom and turning pages (PC side)
  • Example code of Vue to realize PC recording function
  • Vue2's simple PC-side SMS verification code problem and solution
  • Vue uses localStorage to save login information for mobile and PC
  • Example of using Vue to implement a simple keyboard (supports mobile and PC)

<<:  Summary of some related operations of Linux scheduled tasks

>>:  How to convert mysql bin-log log files to sql files

Recommend

A detailed introduction to deploying RabbitMQ environment with docker

Prerequisites: Docker is already installed 1. Fin...

How to configure NAS on Windows Server 2019

Preface This tutorial installs the latest version...

Installation and configuration tutorial of MySQL 8.0.16 under Win10

1. Unzip MySQL 8.0.16 The dada folder and my.ini ...

How to set static IP in CentOS7 on VirtualBox6 and what to note

Install CentOS 7 after installing VirtualBox. I w...

A brief introduction to bionic design in Internet web design

When it comes to bionic design, many people will t...

MySQL-group-replication configuration steps (recommended)

MySQL-Group-Replication is a new feature develope...

Vue's guide to pitfalls using throttling functions

Preface In a common business scenario, we need to...

Image hover toggle button implemented with CSS3

Result:Implementation Code html <ul class=&quo...

Nexus uses API to operate

Nexus provides RestApi, but some APIs still need ...

Javascript design pattern prototype mode details

Table of contents 1. Prototype mode Example 1 Exa...

A brief discussion on the corresponding versions of node node-sass sass-loader

Table of contents The node version does not corre...

MySQL Quick Data Comparison Techniques

In MySQL operation and maintenance, a R&D col...

Tutorial on deploying jdk and tomcat on centos7 without interface

1. Install xshell6 2. Create a server connection ...

How to implement import and export mysql database commands under linux

1. Export the database using the mysqldump comman...

How to configure two-way certificate verification on nginx proxy server

Generate a certificate chain Use the script to ge...