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

Detailed explanation of mysql5.6 master-slave setup and asynchronous issues

Table of contents 1. MySQL master-slave replicati...

Mini Program natively implements left-slide drawer menu

Table of contents WXS Response Event Plan A Page ...

Docker+selenium method to realize automatic health reporting

This article takes the health reporting system of...

Solution to 2059 error when connecting Navicat to MySQL

Recently, when I was learning Django, I needed to...

Analysis of MySQL's method of exporting to Excel

This article describes how to use MySQL to export...

Detailed explanation of JavaScript implementation of hash table

Table of contents 1. Hash table principle 2. The ...

Implementation of Docker batch container orchestration

Introduction Dockerfile build run is a manual ope...

jQuery implements a simple carousel effect

Hello everyone, today I will share with you the i...

MySQL Basics in 1 Hour

Table of contents Getting Started with MySQL MySQ...

The difference between MySQL execute, executeUpdate and executeQuery

The differences among execute, executeUpdate, and...

How to make your browser talk with JavaScript

Table of contents 1. The simplest example 2. Cust...

Notes on using the blockquote tag

<br />Semanticization cannot be explained in...

CSS3 achieves various border effects

Translucent border Result: Implementation code: &...

Implementation of multi-environment configuration (.env) of vue project

Table of contents What is multi-environment confi...

Detailed explanation of the difference between uniapp and vue

Table of contents 1. Simple page example 2.uni-ap...