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 analysis of MySQL 8.0 memory consumption

Table of contents 1. innodb_buffer_pool_size 2. i...

Detailed analysis of the blocking problem of js and css

Table of contents DOMContentLoaded and load What ...

How to use VirtualBox to build a local virtual machine environment on Mac

1. Big Data and Hadoop To study and learn about b...

MySql 5.7.20 installation and configuration of data and my.ini files

1. First download from the official website of My...

How to install and configure WSL on Windows

What is WSL Quoting a passage from Baidu Encyclop...

Problems encountered when uploading images using axios in Vue

Table of contents What is FormData? A practical e...

Linux command line quick tips: How to locate a file

We all have files stored on our computers -- dire...

Teach you how to build the vue3.0 project architecture step by step

Table of contents Preface: 1. Create a project wi...

Vue implements bottom query function

This article example shares the specific code of ...

Beginners learn some HTML tags (1)

Beginners can learn HTML by understanding some HT...

9 super practical CSS tips to help designers and developers

A web designer's head must be filled with a lo...

Building a selenium distributed environment based on docker

1. Download the image docker pull selenium/hub do...