Vue large screen display adaptation method

Vue large screen display adaptation method

This article example shares the specific code for vue large screen display adaptation for your reference. The specific content is as follows

1. Create a file cv in the utils folder with the following code

export function useIndex(appRef) {
  // * appRef points to the outermost container // * Timing function let timer = null
  // * Default scaling value const scale = {
    width: '1',
    height: '1'
  }
  // * Design draft size (px)
  const baseWidth = 1920
  const baseHeight = 1080

  // * The ratio to be maintained (default 2)
  // const baseProportion = parseFloat((baseWidth / baseHeight).toFixed(5))
  const baseProportion = 2
  const calcRate = () => {
    // Current aspect ratio const currentRate = parseFloat((window.innerWidth / window.innerHeight).toFixed(5))
    if (appRef) {
      if (currentRate > baseProportion) {
        // Indicates wider scale.width = ((window.innerHeight * baseProportion) / baseWidth).toFixed(5)
        scale.height = (window.innerHeight / baseHeight).toFixed(5)
        appRef.style.transform = `scale(${scale.width}, ${scale.height}) translate(-50%, -50%)`
      } else {
        // Indicates higher scale.height = ((window.innerWidth / baseProportion) / baseHeight).toFixed(5)
        scale.width = (window.innerWidth / baseWidth).toFixed(5)
        appRef.style.transform = `scale(${scale.width}, ${scale.height}) translate(-50%, -50%)`
      }
    }
  }

  const resize = () => {
    clearTimeout(timer)
    timer = setTimeout(() => {
      calcRate()
    }, 200)
  }

  // Change the window size and redraw const windowDraw = () => {
    window.addEventListener('resize', resize)
  }

  return {
    appRef,
    calcRate,
    windowDraw
  }
}

2.app.vue structure style

<template>
  <div ref="appRef" class="main">
    <div class="layout-container">
    </div>
  </div>
</template>

<script>
import { useIndex } from '@/utils/utilsDram.js'
export default {
  mounted () {
    const { calcRate, windowDraw } = useIndex(this.$refs.appRef)
    calcRate()
    windowDraw()
  }
}
</script>
<style lang="scss" scoped>
.main {
  color: #d3d6dd;
  width: 1920px;
  height: 1080px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  transform-origin: left top;

  .layout-container {
    width: 100%;
    height: 100%;
    }
   }
</style>

3. The font size and box width are directly set to px. No matter how you zoom in or out, it will remain the same as before. The basic page size will not be too different. The following figure shows the effect of zooming in 500 times.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Example code for implementing large screen adaptation on PC using vue+px2rem (rem adaptation)
  • Example of how to adapt the Vue project to the large screen

<<:  Write a formal blog using XHTML CSS

>>:  Windows Server 2016 Quick Start Guide to Deploy Remote Desktop Services

Recommend

The principle and application of MySQL connection query

Overview One of the most powerful features of MyS...

MySQL 8.0.23 installation super detailed tutorial

Table of contents Preface 1. Download MySQL from ...

Detailed explanation of how to use Teleport, a built-in component of Vue3

Table of contents 1. Teleport usage 2. Complete t...

Example of implementing text wrapping in html (mixed text and images in html)

1. Text around the image If we use the normal one...

How to install Solr 8.6.2 in Docker and configure the Chinese word segmenter

1. Environment version Docker version 19.03.12 ce...

The top fixed div can be set to a semi-transparent effect

Copy code The code is as follows: <!DOCTYPE ht...

JavaScript - Using slots in Vue: slot

Table of contents Using slots in Vue: slot Scoped...

Complete MySQL Learning Notes

Table of contents MyISAM and InnoDB Reasons for p...

Detailed explanation of Vue filter implementation and application scenarios

1. Brief Introduction Vue.js allows you to define...

React implements the expansion and collapse function of complex search forms

Give time time and let the past go. In the previo...

How to install MySQL under Linux (yum and source code compilation)

Here are two ways to install MySQL under Linux: y...

Detailed explanation of count(), group by, order by in MySQL

I recently encountered a problem when doing IM, a...

Vue implements carousel animation

This article example shares the specific code of ...

Detailed examples of the difference between methods watch and computed in Vue.js

Table of contents Preface introduce 1. Mechanism ...