vue can achieve self-adaptation. The methods to achieve self-adaptation are: 1. Install the "scale-box" component through the "npm install" or "yarn add" command, and use "scale-box" to implement self-adaptation. Equipped with zoom; 2. Adapt by setting the device pixel ratio; 3. Set the zoom attribute through JS to adjust the zoom ratio to achieve adaptation.
The operating environment of this tutorial: Windows 10 system, vue2&&vue3 version, Dell G3 computer.
Can vue be adaptive?
able.
Detailed explanation of the three implementation methods of Vue screen adaptation
Using the scale-box component
Attribute:
-
width
Width default1920
-
height
Height default1080
-
bgc
Background color default"transparent"
-
delay
Adaptive scaling anti-shake delay time (ms) Default100
vue2 Version: vue2 large screen adaptation scaling component (vue2-scale-box - npm)
npm install vue2-scale-box
or
yarn add vue2-scale-box
Usage:
<template> <div> <scale-box :width="1920" :height="1080" bgc="transparent" :delay="100"> <router-view /> </scale-box> </div> </template> <script> import ScaleBox from "vue2-scale-box"; export default { components: { ScaleBox }, }; </script> <style lang="scss"> body { margin: 0; padding: 0; background: url("@/assets/bg.jpg"); } </style>
vue3 version: vue3 large screen Adapt scaling component (vue3-scale-box - npm)
npm install vue3-scale-box
or
yarn add vue3-scale-box
Usage:
<template> <ScaleBox :width="1920" :height="1080" bgc="transparent" :delay="100"> <router-view /> </ScaleBox> </template> <script> import ScaleBox from "vue3-scale-box"; </script> <style lang="scss"> body { margin: 0; padding: 0; background: url("@/assets/bg.jpg"); } </style>
Set the device pixel ratio (device pixel ratio)
In the project Create a new devicePixelRatio.js file under utils
class devicePixelRatio { /* 獲取系統(tǒng)類型 */ getSystem() { const agent = navigator.userAgent.toLowerCase(); const isMac = /macintosh|mac os x/i.test(navigator.userAgent); if (isMac) return false; // 目前只針對 win 處理,其它系統(tǒng)暫無該情況,需要?jiǎng)t繼續(xù)在此添加即可 if (agent.indexOf("windows") >= 0) return true; } /* 監(jiān)聽方法兼容寫法 */ addHandler(element, type, handler) { if (element.addEventListener) { element.addEventListener(type, handler, false); } else if (element.attachEvent) { element.attachEvent("on" + type, handler); } else { element["on" + type] = handler; } } /* 校正瀏覽器縮放比例 */ correct() { // 頁面devicePixelRatio(設(shè)備像素比例)變化后,計(jì)算頁面body標(biāo)簽zoom修改其大小,來抵消devicePixelRatio帶來的變化 document.getElementsByTagName("body")[0].style.zoom = 1 / window.devicePixelRatio; } /* 監(jiān)聽頁面縮放 */ watch() { const that = this; // 注意: 這個(gè)方法是解決全局有兩個(gè)window.resize that.addHandler(window, "resize", function () { that.correct(); // 重新校正瀏覽器縮放比例 }); } /* 初始化頁面比例 */ init() { const that = this; // 判斷設(shè)備,只在 win 系統(tǒng)下校正瀏覽器縮放比例 if (that.getSystem()) { that.correct(); // 校正瀏覽器縮放比例 that.watch(); // 監(jiān)聽頁面縮放 } } } export default devicePixelRatio;
Introduce and use it in App.vue
<template> <div> <router-view /> </div> </template> <script> import devPixelRatio from "@/utils/devicePixelRatio.js"; export default { created() { new devPixelRatio().init(); // 初始化頁面比例 }, }; </script> <style lang="scss"> body { margin: 0; padding: 0; } </style>
Set zoom attribute adjustment through JS Scaling ratio
Create a new monitorZoom.js file under the project's utils
export const monitorZoom = () => { let ratio = 0, screen = window.screen, ua = navigator.userAgent.toLowerCase(); if (window.devicePixelRatio !== undefined) { ratio = window.devicePixelRatio; } else if (~ua.indexOf("msie")) { if (screen.deviceXDPI && screen.logicalXDPI) { ratio = screen.deviceXDPI / screen.logicalXDPI; } } else if ( window.outerWidth !== undefined && window.innerWidth !== undefined ) { ratio = window.outerWidth / window.innerWidth; } if (ratio) { ratio = Math.round(ratio * 100); } return ratio; };
Introduce and use it in main.js
import { monitorZoom } from "@/utils/monitorZoom.js"; const m = monitorZoom(); if (window.screen.width * window.devicePixelRatio >= 3840) { document.body.style.zoom = 100 / (Number(m) / 2); // 屏幕為 4k 時(shí) } else { document.body.style.zoom = 100 / Number(m); }
Complete code
import Vue from "vue"; import App from "./App.vue"; import router from "./router"; /* 調(diào)整縮放比例 start */ import { monitorZoom } from "@/utils/monitorZoom.js"; const m = monitorZoom(); if (window.screen.width * window.devicePixelRatio >= 3840) { document.body.style.zoom = 100 / (Number(m) / 2); // 屏幕為 4k 時(shí) } else { document.body.style.zoom = 100 / Number(m); } /* 調(diào)整縮放比例 end */ Vue.config.productionTip = false; new Vue({ router, render: (h) => h(App), }).$mount("#app");
Get the resolution of the screen
Get the width of the screen:
window.screen.width * window.devicePixelRatio
Get the height of the screen:
window.screen.height * window.devicePixelRatio
Mobile terminal adaptation (use postcss-px-to-viewport plug-in)
Official website:http://m.miracleart.cn/link/2dd6d682870e39d9927b80f8232bd276
npm install postcss-px -to-viewport --save-dev
or
yarn add -D postcss-px-to-viewport
Configuration appropriate Configure the parameters of the plug-in (create a .postcssrc.js file in the project root directory [level with the src directory]) and paste the following code
module.exports = { plugins: { autoprefixer: {}, // 用來給不同的瀏覽器自動添加相應(yīng)前綴,如-webkit-,-moz-等等 "postcss-px-to-viewport": { unitToConvert: "px", // 需要轉(zhuǎn)換的單位,默認(rèn)為"px" viewportWidth: 390, // UI設(shè)計(jì)稿的寬度 unitPrecision: 6, // 轉(zhuǎn)換后的精度,即小數(shù)點(diǎn)位數(shù) propList: ["*"], // 指定轉(zhuǎn)換的css屬性的單位,*代表全部css屬性的單位都進(jìn)行轉(zhuǎn)換 viewportUnit: "vw", // 指定需要轉(zhuǎn)換成的視窗單位,默認(rèn)vw fontViewportUnit: "vw", // 指定字體需要轉(zhuǎn)換成的視窗單位,默認(rèn)vw selectorBlackList: ["wrap"], // 需要忽略的CSS選擇器,不會轉(zhuǎn)為視口單位,使用原有的px等單位 minPixelValue: 1, // 默認(rèn)值1,小于或等于1px則不進(jìn)行轉(zhuǎn)換 mediaQuery: false, // 是否在媒體查詢的css代碼中也進(jìn)行轉(zhuǎn)換,默認(rèn)false replace: true, // 是否直接更換屬性值,而不添加備用屬性 exclude: [/node_modules/], // 忽略某些文件夾下的文件或特定文件,用正則做目錄名匹配,例如 'node_modules' 下的文件 landscape: false, // 是否處理橫屏情況 landscapeUnit: "vw", // 橫屏?xí)r使用的視窗單位,默認(rèn)vw landscapeWidth: 2048 // 橫屏?xí)r使用的視口寬度 } } };
Recommended learning: "vue. js video tutorial》
The above is the detailed content of Can vue be adaptive?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

You can add a function to the Vue button by binding the button in the HTML template to a method. Define the method and write function logic in the Vue instance.

Netflixusesacustomframeworkcalled"Gibbon"builtonReact,notReactorVuedirectly.1)TeamExperience:Choosebasedonfamiliarity.2)ProjectComplexity:Vueforsimplerprojects,Reactforcomplexones.3)CustomizationNeeds:Reactoffersmoreflexibility.4)Ecosystema

Netflix uses React as its front-end framework. 1) React's componentized development model and strong ecosystem are the main reasons why Netflix chose it. 2) Through componentization, Netflix splits complex interfaces into manageable chunks such as video players, recommendation lists and user comments. 3) React's virtual DOM and component life cycle optimizes rendering efficiency and user interaction management.

There are two ways to jump div elements in Vue: use Vue Router and add router-link component. Add the @click event listener and call this.$router.push() method to jump.

Netflix mainly uses React as the front-end framework, supplemented by Vue for specific functions. 1) React's componentization and virtual DOM improve the performance and development efficiency of Netflix applications. 2) Vue is used in Netflix's internal tools and small projects, and its flexibility and ease of use are key.

The methods to implement the jump of a tag in Vue include: using the a tag in the HTML template to specify the href attribute. Use the router-link component of Vue routing. Use this.$router.push() method in JavaScript. Parameters can be passed through the query parameter and routes are configured in the router options for dynamic jumps.

There are the following methods to implement component jump in Vue: use router-link and <router-view> components to perform hyperlink jump, and specify the :to attribute as the target path. Use the <router-view> component directly to display the currently routed rendered components. Use the router.push() and router.replace() methods for programmatic navigation. The former saves history and the latter replaces the current route without leaving records.

Pagination is a technology that splits large data sets into small pages to improve performance and user experience. In Vue, you can use the following built-in method to paging: Calculate the total number of pages: totalPages() traversal page number: v-for directive to set the current page: currentPage Get the current page data: currentPageData()
