SoFunction
Updated on 2025-04-13

Summary of 3 mainstream adaptation solutions for front-end large screen development

summary

Currently, there are three mainstream types:

vh scheme:

  • Implementation method: According to the size of the design draft,pxConvert tovwandvh
  • Advantages: 1. The width and height of the chart can be calculated dynamically, and the font, etc. are relatively flexible. 2. When the screen ratio is inconsistent with the ui draft, there will be no white space on both sides.
  • Disadvantages: Each chart needs to be adapted separately for font, spacing and displacement, which is more troublesome.

plan:

  • Implementation method: byscaleProperties, overall equal-scale scaling of the chart according to the screen size
  • Advantages: 1. Small code volume, simple adaptation 2. After processing once, there is no need to adapt separately in each chart.
  • Disadvantages: 1. Because it is scaled according to the ratio of ui manuscripts, when the ratio of the large screen and ui manuscripts is different, there will be blank space around it. 2. When the scaling ratio is too large, the font will be a little blurred, just a little bit. 3. When the scaling ratio is too large, the hot zone of the event will be offset.

+ vw vh scheme:

  • Implementation method: 1. Obtain the reference value of rem 2. Dynamic calculationfont-size of html root element3. Dynamically calculate fonts, spacing, displacement, etc. in the chart through vw vh
  • Advantages: The layout has a small amount of adaptive code and simple adaptation
  • Disadvantages: 1. Because it is scaled according to the ratio of ui draft, when the ratio of the large screen and ui draft is different, there will be blank space around it. 2. The chart needs to be adapted individually to font, spacing, and displacement.

Plan 1, vw vh

illustrate:

1. When the screen size ratio is larger than the design draft, the page feels compressed

2. When the screen size ratio is smaller than the design draft, the page feels enlarged

According to the size of the design draft,pxConvert tovwandvh, the conversion formula is as follows

Assume that the design draft size is 1920*1080(Be sure to ask clearly before doing it ui Size of design draft)
Right now: Web page width=1920px Web page height=1080px 
Web page width=100vw Web page width=100vh 
so,exist 1920px*1080px at screen resolution 1920px = 100vw 1080px = 100vh

This way,With one width 300px and 200px of div Say,其所占of宽高,by vw and vh For units,The calculation method is as follows:

vwDiv = (300px / 1920px) * 100vw
vhDiv = (200px / 1080px) * 100vh

so,就exist 1920*1080 at screen resolution,Calculated a single div of宽高 
When the screen is enlarged or reduced,div 还是by vw and vh 作为宽高of,就会自动适应不同分辨率of屏幕

1. css plan - sass

// Use scss' math function, /documentation/breaking-changes/slash-div@use "sass:math";

// Default design draft width$designWidth: 1920;
// The height of the default design draft$designHeight: 1080;

// Function converted to vw@function vw($px) {
  @return ($px, $designWidth) * 100vw;
}

// Function converted to vh@function vh($px) {
  @return ($px, $designHeight) * 100vh;
}

Just inConfigure it inThe path can be used globally

const path = require("path");

function resolve(dir) {
  return (__dirname, dir);
}

 = {
  publicPath: "",
  configureWebpack: {
    name: "app name",
    resolve: {
      alias: {
        "@": resolve("src"),
      },
    },
  },
  css: {
    // Global configuration, detailed configuration reference vue-cli official website    loaderOptions: {
      sass: {
        prependData: `@import "@/styles/";`,
      },
    },
  },
};

Use in vue

<template>
    <div class="box">			
    </div>
</template>

<script>
export default{
    name: "Box",
}
</script>

<style lang="scss" scoped="scoped">
/*
  Directly use the vw and vh functions to pass the pixel value in, and the specific vw vh unit is obtained
  */
.box{
    width: vw(300);
    height: vh(100);
    font-size: vh(16);
    background-color: black;
    margin-left: vw(10);
    margin-top: vh(10);
    border: vh(2) solid red;
}
</style>

2. Define js style processing function

// Define the width and height of the design draftconst designWidth = 1920
const designHeight = 1080

// px to vwexport const px2vw = (_px) => {
    return (_px * 100) / designWidth + 'vw'
}
export const px2vh = (_px) => { return (_px * 100.0) / designHeight + 'vh'; }; 
export const px2font = (_px) => { return (_px * 100.0) / designWidth + 'vw'; };

After the screen changes, the chart will be automatically adjusted

Encapsulate custom instructions

// 
import * as ECharts from "echarts";
import elementResizeDetectorMaker from "element-resize-detector";
import Vue from "vue";
const HANDLER = "_vue_resize_handler";
function bind(el, binding) {
  el[HANDLER] = 
    ? 
    : () => {
        let chart = (el);
        if (!chart) {
          return;
        }
        ();
      };
  // Listen to the binding div size changes, update the echarts size  elementResizeDetectorMaker().listenTo(el, el[HANDLER]);
}
function unbind(el) {
  // ("resize", el[HANDLER]);
  elementResizeDetectorMaker().removeListener(el, el[HANDLER]);
  delete el[HANDLER];
}
// Custom directive: v-chart-resize Example: v-chart-resize="fn"("chart-resize", { bind, unbind });

Adaptive size adaptation between charts, such as text, spacing, displacement, etc.

  • The principle is to calculate the ratio of the current screen width to the default design width, multiply the original size by this value, and write a tool function

Plan 2: scale

Through the scale attribute of css, the overall equal-scale scale of the chart is scaled according to the screen size, thereby achieving an adaptive effect

1. When the screen size ratio is greater than the design draft, the left and right pages will be blank, the upper and lower parts will be full and centered, and the display ratio will remain 16:9

2. When the screen size ratio is smaller than the design draft, the page will be left blank, the left and right are full and the upper and lower center, and the display ratio will remain 16:9

html part

<div className="screen-wrapper">
    <div className="screen" >
    </div>
 </div>

js part

&lt;script&gt;
export default {
    mounted() {
        // Initialize adaptation -- start adapting once when it is just displayed        handleScreenAuto();
        // Bind adaptive function --- to prevent the browser bar from being adapted after it changes         = () =&gt; handleScreenAuto();
    },
    destroyed() {
         = null;
    },
    methods: {
        handleScreenAuto() {
            const designDraftWidth = 1920; //Width of the design draft            const designDraftHeight = 960; //The height of the design draft            // The proportions adapted according to the changes in the screen            const scale =  /  &lt; designDraftWidth / designDraftWidth ? 
             / designDraftWidth : 
             / designDraftHeight;
            // Scaling            ('#screen'). = `scale(${scale}) translate(-50%, -50%)`;
        }
    }
}
&lt;/script&gt;

CSS part

/*
   In addition to the width and height of the design draft are determined based on your own design draft, copy and paste the other ones.
 */  
.screen-root {
    height: 100%;
    width: 100%;
    .screen {
        display: inline-block;
        width: 1920px;  //Width of the design draft        height: 960px;  //The height of the design draft        transform-origin: 0 0;
        position: absolute;
        left: 50%;
        top: 50%;
    }
}

Plan 3: rem + vw wh

1. When the screen size ratio is greater than the design draft, the left and right pages will be blank, the upper and lower parts will be full and centered, and the display ratio will remain 16:9

2. When the screen size ratio is smaller than the design draft, the page will be left blank, the left and right are full and the upper and lower center, and the display ratio will remain 16:9

About rem

rem(font size of the root element), is a new size unit added in css3, that is, the size relative to the root element font-size value.

Adaptive thinkingDynamically calculate the fontsize of the page to change the size of rem.

  • Divide the screen into 10 copies, take 1920 * 1080 as an example, calculate firstRem's reference value:1920/10 = 192
  • Convert the original px of all elements, such as length, width, position, font size, etc. to rem
  • After the web page is loaded, use js to calculate the width of the current browser and set the font-size of the html to (current window width / 10). In this way, 10rem is exactly equal to the width of the browser window, which can ensure 100% width and scale the page of the design draft in proportion.

rem + vw vh solution three things

  • Get the reference value of rem
  • Write a piece of js in the page and dynamically calculate the font-size of the root element of the html
  • After the screen changes, the chart will be automatically adjusted and the chart font, spacing, displacement, etc. will be adapted.

1. Obtain the reference value of rem

  • Install firstpostcss-px-to-remThis bag
  • Create a new project root directory.Configuration File
 = {
  plugins: {
    autoprefixer: {},
    "postcss-px-to-rem": {
      unitToConvert: 'px', // (String) The unit to be converted is px by default.      widthOfDesignLayout: 1920, // (Number) The width of the design layout.  For PC dashboards, it is generally 1920.      unitPrecision: 3, // (Number) Decimal number that allows rem units to grow.      selectorBlackList: ['.ignore', '.hairlines'], // (Array) Selector to ignore and keep as px.      minPixelValue: 1, // (Number) Sets the minimum pixel value to replace.      mediaQuery: false // (Boolean) allows converting px in media queries.    }
  }
}

After the configuration is completed, the px in the page will be converted to rem

2、Dynamically calculate the font-size of the html root element

Create a new file in the tool function file for dynamic calculation of font-size

(function init(screenRatioByDesign = 16 / 9) {
    let docEle = 
    function setHtmlFonstSize() {
        var screenRatio =  / ;
        var fontSize = (
            screenRatio > screenRatioByDesign ? (screenRatioByDesign / screenRatio) : 1
        ) *  / 10;
         = (3) + "px";
        ();
    }
    setHtmlFontSize()
    ('resize', setHtmlFontSize)
})

Introduce files in the entry file

import './utils/';

3. Screen changes, chart adaptive

After the screen changes, the chart automatically adjusts the font, spacing, displacement, etc. Here, please refer to the above implementation method of vw vh

Summarize

This is the article about the three mainstream adaptation solutions for front-end large screen development. For more related contents of front-end large screen adaptation solutions, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!