Example of how to adapt the Vue project to the large screen

Example of how to adapt the Vue project to the large screen

A brief analysis of rem

First of all, rem is a CSS unit. Compared with the fixed pixel unit of px, rem is more flexible, and vm is also relatively good now. If you have never known it, you can take a look at it first.

rem adaptive. CSS3 REM sets font size

font size of the root element.

Simply put, rem is calculated based on the font size of the HTML element. Our requirement is that the size of our elements can change accordingly according to different resolutions, so that we can achieve a very comfortable visual effect. Think about when we are working on the PC side, we usually fix the middle size, for example 1200px pixels, and then the minimum is 1200px, leaving blank space on both sides, so that no matter we zoom in or out, it will not affect the effect. But now with the emergence of various mobile screens, adaptability is even stronger. To change the font size of HTML according to different resolutions, we write rem in the page. Since rem is calculated relative to the font size of the root element, an adaptive effect can be achieved.

1. Adaptation method

The adaptation solution uses rem layout. According to the different screen resolutions, the font-size of the root element html is adjusted, so that the width and height of each element can be automatically changed to adapt to different screens.

2. Use the postcss-px2rem-exclude plugin

Installnpm install postcss-px2rem-exclude --save-dev

Create a postcss.config.js file in the project root directory

module.exports = {
  plugins: {
    autoprefixer: {},
    'postcss-px2rem-exclude': {
      remUnit: 192
      // exclude: /node_modules|folder_name/i,
    }
  }
}

3. Install flexible.js (recommended to put it locally)

Installation command npm install lib-flexible

(function(win, lib) {
  var doc = win.document
  var docEl = doc.documentElement
  var metaEl = doc.querySelector('meta[name="viewport"]')
  var flexibleEl = doc.querySelector('meta[name="flexible"]')
  var dpr = 0
  var scale = 0
  var tid
  var flexible = lib.flexible || (lib.flexible = {})

  if (metaEl) {
    console.warn('The zoom ratio will be set according to the existing meta tags')
    var match = metaEl
      .getAttribute('content')
      // eslint-disable-next-line no-useless-escape
      .match(/initial\-scale=([\d\.]+)/)
    if (match) {
      scale = parseFloat(match[1])
      dpr = parseInt(1 / scale)
    }
  } else if (flexibleEl) {
    var content = flexibleEl.getAttribute('content')
    if (content) {
      // eslint-disable-next-line no-useless-escape
      var initialDpr = content.match(/initial\-dpr=([\d\.]+)/)
      // eslint-disable-next-line no-useless-escape
      var maximumDpr = content.match(/maximum\-dpr=([\d\.]+)/)
      if (initialDpr) {
        dpr = parseFloat(initialDpr[1])
        scale = parseFloat((1 / dpr).toFixed(2))
      }
      if (maximumDpr) {
        dpr = parseFloat(maximumDpr[1])
        scale = parseFloat((1 / dpr).toFixed(2))
      }
    }
  }

  if (!dpr && !scale) {
    // var isAndroid = win.navigator.appVersion.match(/android/gi);
    var isIPhone = win.navigator.appVersion.match(/iphone/gi)
    var devicePixelRatio = win.devicePixelRatio
    if (isIPhone) {
      // Under iOS, for 2 and 3 screens, use the 2x solution, and for the rest, use the 1x solution if (devicePixelRatio >= 3 && (!dpr || dpr >= 3)) {
        dpr = 3
      } else if (devicePixelRatio >= 2 && (!dpr || dpr >= 2)) {
        dpr = 2
      } else {
        dpr = 1
      }
    } else {
      // For other devices, the 1x solution is still used dpr = 1
    }
    scale = 1 / dpr
  }

  docEl.setAttribute('data-dpr', dpr)
  if (!metaEl) {
    metaEl = doc.createElement('meta')
    metaEl.setAttribute('name', 'viewport')
    metaEl.setAttribute('content', 'initial-scale=' + scale + ', maximum-scale=' + scale + ', minimum-scale=' + scale + ', user-scalable=no')
    if (docEl.firstElementChild) {
      docEl.firstElementChild.appendChild(metaEl)
    } else {
      var wrap = doc.createElement('div')
      wrap.appendChild(metaEl)
      doc.write(wrap.innerHTML)
    }
  }

  function refreshRem() {
    var width = docEl.getBoundingClientRect().width
    if (width / dpr > 540) {
      width = width * dpr
    }
    var rem = width / 10
    docEl.style.fontSize = rem + 'px'
    flexible.rem = win.rem = rem
  }

  win.addEventListener(
    'resize',
    function() {
      clearTimeout(tid)
      tid = setTimeout(refreshRem, 300)
    },
    false
  )
  win.addEventListener(
    'pageshow',
    function(e) {
      if (e.persisted) {
        clearTimeout(tid)
        tid = setTimeout(refreshRem, 300)
      }
    },
    false
  )

  if (doc.readyState === 'complete') {
    doc.body.style.fontSize = 12 * dpr + 'px'
  } else {
    doc.addEventListener(
      'DOMContentLoaded',
      function() {
        doc.body.style.fontSize = 12 * dpr + 'px'
      },
      false
    )
  }

  refreshRem()

  flexible.dpr = win.dpr = dpr
  flexible.refreshRem = refreshRem
  flexible.rem2px = function(d) {
    var val = parseFloat(d) * this.rem
    if (typeof d === 'string' && d.match(/rem$/)) {
      val += 'px'
    }
    return val
  }
  flexible.px2rem = function(d) {
    var val = parseFloat(d) / this.rem
    if (typeof d === 'string' && d.match(/px$/)) {
      val += 'rem'
    }
    return val
  }
})(window, window['lib'] || (window['lib'] = {}))

The code above is different from the installed flexible.js

Modified

  function refreshRem() {
    var width = docEl.getBoundingClientRect().width
    if (width / dpr > 540) {
      width = width * dpr
    }
    var rem = width / 10
    docEl.style.fontSize = rem + 'px'
    flexible.rem = win.rem = rem
  }

Import in main.js

import 'path/flexible.js'

Summarize

This is the end of this article about the adaptation of the Vue project to the large screen. For more relevant content about the adaptation of Vue to the large screen, 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 code for implementing large screen adaptation on PC using vue+px2rem (rem adaptation)

<<:  Summary of how to add root permissions to users in Linux

>>:  MySQL Error 1290 (HY000) Solution

Recommend

Summary of things to pay attention to in the footer of a web page

Lots of links You’ve no doubt seen a lot of sites ...

Implementing password box verification information based on JavaScript

This article example shares the specific code of ...

Examples of using && and || operators in javascript

Table of contents Preface && Operator || ...

Examples of using HTML list tags dl, ul, ol

Copy code The code is as follows: <!-- List ta...

Detailed tutorial on how to create a user in mysql and grant user permissions

Table of contents User Management Create a new us...

Vue implements dynamic circular percentage progress bar

Recently, when developing a small program, I enco...

How to connect to MySQL visualization tool Navicat

After installing Navicat The following error may ...

Detailed tutorial on installing Anaconda3 on Ubuntu 18.04

Anaconda refers to an open source Python distribu...

Tutorial on using portainer to connect to remote docker

Portainer is a lightweight docker environment man...

The difference between hash mode and history mode in vue-router

vue-router has two modes hash mode History mode 1...

MySQL 5.7.17 installation and use graphic tutorial

MySQL is a relational database management system ...

Solutions to common problems using Elasticsearch

1. Using it with redis will cause Netty startup c...

How to maintain a long connection when using nginx reverse proxy

· 【Scene description】 After HTTP1.1, the HTTP pro...

Element Plus implements Affix

Table of contents 1. Component Introduction 2. So...