亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Table of Contents
Using the scale-box component
Set the device pixel ratio (device pixel ratio)
Set zoom attribute adjustment through JS Scaling ratio
Home Web Front-end Front-end Q&A Can vue be adaptive?

Can vue be adaptive?

Dec 30, 2022 pm 03:25 PM
vue Adaptive

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.

Can vue be adaptive?

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
  • heightHeight default1080
  • bgcBackground color default "transparent"
  • delayAdaptive scaling anti-shake delay time (ms) Default 100

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/], // 忽略某些文件夾下的文件或特定文件,用正則做目錄名匹配,例如 &#39;node_modules&#39; 下的文件
      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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1488
72
How to develop a complete Python Web application? How to develop a complete Python Web application? May 23, 2025 pm 10:39 PM

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.

Laravel Vue.js single page application (SPA) tutorial Laravel Vue.js single page application (SPA) tutorial May 15, 2025 pm 09:54 PM

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.

How to separate the front and back end of wordpress How to separate the front and back end of wordpress Apr 20, 2025 am 08:39 AM

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 the video stream of Hikvision camera SDK to the front-end Vue project for real-time playback? How to push the video stream of Hikvision camera SDK to the front-end Vue project for real-time playback? Apr 19, 2025 pm 07:42 PM

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...

How to work and configuration of front-end routing (Vue Router, React Router)? How to work and configuration of front-end routing (Vue Router, React Router)? May 20, 2025 pm 07:18 PM

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.

What is the significance of Vue's reactivity transform (experimental, then removed) and its goals? What is the significance of Vue's reactivity transform (experimental, then removed) and its goals? Jun 20, 2025 am 01:01 AM

ReactivitytransforminVue3aimedtosimplifyhandlingreactivedatabyautomaticallytrackingandmanagingreactivitywithoutrequiringmanualref()or.valueusage.Itsoughttoreduceboilerplateandimprovecodereadabilitybytreatingvariableslikeletandconstasautomaticallyreac

What are the core differences between Vue.js and React in componentized development? What are the core differences between Vue.js and React in componentized development? May 21, 2025 pm 08:39 PM

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.

Solve the challenges of Laravel and Vue.js form building with Composer Solve the challenges of Laravel and Vue.js form building with Composer Apr 18, 2025 am 08:12 AM

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.

See all articles