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; // 目前只針對(duì) 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://ipnx.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: {}, // 用來給不同的瀏覽器自動(dòng)添加相應(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選擇器,不會(huì)轉(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)

To develop a complete Python Web application, follow these steps: 1. Choose the appropriate framework, such as Django or Flask. 2. Integrate databases and use ORMs such as SQLAlchemy. 3. Design the front-end and use Vue or React. 4. Perform the test, use pytest or unittest. 5. Deploy applications, use Docker and platforms such as Heroku or AWS. Through these steps, powerful and efficient web applications can be built.

Single-page applications (SPAs) can be built using Laravel and Vue.js. 1) Define API routing and controller in Laravel to process data logic. 2) Create a componentized front-end in Vue.js to realize user interface and data interaction. 3) Configure CORS and use axios for data interaction. 4) Use VueRouter to implement routing management and improve user experience.

It is not recommended to directly modify the native code when separating WordPress front and back ends, and it is more suitable for "improved separation". Use the REST API to obtain data and build a user interface using the front-end framework. Identify which functions are called through the API, which are retained on the backend, and which can be cancelled. The Headless WordPress mode allows for a more thorough separation, but it is more cost-effective and difficult to develop. Pay attention to security and performance, optimize API response speed and cache, and optimize WordPress itself. Gradually migrate functions and use version control tools to manage code.

How to push video streams from Hikvision camera SDK to front-end Vue project? During the development process, you often encounter videos that need to be captured by the camera to be circulated...

The core of the front-end routing system is to map URLs to components. VueRouter and ReactRouter realize refresh-free page switching by listening for URL changes and loading corresponding components. The configuration methods include: 1. Nested routing, allowing the nested child components in the parent component; 2. Dynamic routing, loading different components according to URL parameters; 3. Route guard, performing logic such as permission checks before and after route switching.

ReactivitytransforminVue3aimedtosimplifyhandlingreactivedatabyautomaticallytrackingandmanagingreactivitywithoutrequiringmanualref()or.valueusage.Itsoughttoreduceboilerplateandimprovecodereadabilitybytreatingvariableslikeletandconstasautomaticallyreac

The core differences between Vue.js and React in component development are: 1) Vue.js uses template syntax and option API, while React uses JSX and functional components; 2) Vue.js uses responsive systems, React uses immutable data and virtual DOM; 3) Vue.js provides multiple life cycle hooks, while React uses more useEffect hooks.

I'm having a headache when developing a project based on Laravel and Vue.js: How to create and manage forms efficiently. Especially when it is necessary to define the form structure on the backend and generate dynamic forms on the frontend, traditional methods appear cumbersome and error-prone. I tried many methods, but the results were not satisfactory. Finally, I discovered the k-eggermont/lara-vue-builder library, which not only simplified my workflow, but also greatly improved the development efficiency.
