欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue使得大屏自適應(yīng)的多種方法

 更新時間:2023年10月12日 09:21:26   作者:幕颯前端程序員  
這篇文章主要介紹了Vue使得大屏自適應(yīng)的多種方法,自適屏幕,始終保持16:9的比例,還一種是使用CSS scale屬性對大屏幕做自適應(yīng)處理,需要的朋友可以參考下

VUE學(xué)習(xí)大屏自適應(yīng)的幾種方法

1.自適屏幕,始終保持16:9的比例

<!-- 大屏固定比例16:9自適應(yīng)   -->
<template>
    <div class="container">
      <div class="content" :style="getAspectRatioStyle">
        <!-- 數(shù)據(jù)展示內(nèi)容 -->
      </div>
    </div>
  </template>
  <script setup lang="ts">
  import { ref, onMounted, onBeforeUnmount, computed } from 'vue';
      const contentWidth = ref(0);
      const contentHeight = ref(0);
      const calculateAspectRatio = () => {
        const container = document.querySelector('.container');
        // const containerWidth = container.offsetWidth;
        const containerWidth: number = (<HTMLElement>container).offsetWidth;
        // const containerHeight = container.offsetHeight;
        const containerHeight: number = (<HTMLElement>container).offsetHeight;
        const aspectRatio = 16 / 9; // 16:9 比例
        const containerAspectRatio = containerWidth / containerHeight;
        if (containerAspectRatio > aspectRatio) {
          // 以高度為基準,按比例計算寬度
          contentHeight.value = containerHeight;
          contentWidth.value = Math.floor(containerHeight * aspectRatio);
        } else {
          // 以寬度為基準,按比例計算高度
          contentWidth.value = containerWidth;
          contentHeight.value = Math.floor(containerWidth / aspectRatio);
        }
        console.log('contentWidth',contentWidth.value)
        console.log('contentHeight',contentHeight.value)
      };
      onMounted(() => {
        calculateAspectRatio();
        window.addEventListener('resize', calculateAspectRatio);
      });
      onBeforeUnmount(() => {
        window.removeEventListener('resize', calculateAspectRatio);
      });
      const getAspectRatioStyle = computed(() => ({
        width: `${contentWidth.value}px`,
        height: `${contentHeight.value}px`,
        margin: 'auto',
        background: 'gray'
      }
    ));
  </script>
  <style>
  .container {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .content {
    /* 根據(jù)計算得到的寬高樣式設(shè)置 */
  }
  </style>

2.使用CSS scale屬性對大屏幕做自適應(yīng)處理

<template>
<div class="login-container">
  <div class="login-main" ref="dataScreenRef"></div>
</div>
</template>
<script setup>
const dataScreenRef = ref(null);
const width = 1920;
const height = 1080;
// 根據(jù)瀏覽器大小推斷縮放比例
// 首先要確定設(shè)計稿尺寸,默認是 1920 x 1080
// 分別計算瀏覽器和設(shè)計圖寬高比
// 如果瀏覽器的寬高比大于設(shè)計稿的寬高比,就取瀏覽器高度和設(shè)計稿高度之比
// 如果瀏覽器的寬高比小于設(shè)計稿的寬高比,就取瀏覽器寬度和設(shè)計稿寬度之比
const getScale = (w = width, h = height) => {
  let ww = window.innerWidth / w;
  let wh = window.innerHeight / h;
  return ww < wh ? ww : wh;
};
/* 瀏覽器監(jiān)聽 resize 事件 */
const resize = () => {
  if (dataScreenRef.value) {
    dataScreenRef.value.style.transform = `scale(${getScale()}) translate(-50%, -50%)`;
  }
};
onMounted(() => {
  // 初始化時為外層盒子加上縮放屬性,防止刷新界面時就已經(jīng)縮放
  if (dataScreenRef.value) {
    dataScreenRef.value.style.transform = `scale(${getScale()}) translate(-50%, -50%)`;
    dataScreenRef.value.style.width = `${width}px`;
    dataScreenRef.value.style.height = `${height}px`;
  }
  window.addEventListener("resize", resize);
});
</script>
<style scoped lang="scss">
.login-container {
  width: 100%;
  height: 100%;
  transform-origin: 0 0;
  position: relative;
}
.login-main {
  width: 100%;
  height: 100%;
  position: absolute;
}
</style>

3.使用rem

(1)npm下載插件,自動將px單位轉(zhuǎn)換成rem單位

npm install postcss-px2rem --save

(2)在根目錄src中新建util目錄下新建rem.js等比適配文件

// rem等比適配配置文件
// 基準大小
const baseSize = 14
// 設(shè)置 rem 函數(shù)
function setRem () {
  // 當前頁面寬度相對于 1920寬的縮放比例,可根據(jù)自己需要修改。
  const scale = document.documentElement.clientWidth / 1920
  // 設(shè)置頁面根節(jié)點字體大小(“Math.min(scale, 2)” 指最高放大比例為2,可根據(jù)實際業(yè)務(wù)需求調(diào)整)
  document.documentElement.style.fontSize = baseSize * Math.min(scale, 2) + 'px'
}
// 初始化
setRem()
// 改變窗口大小時重新設(shè)置 `rem`
window.onresize = function () {
  setRem()
}

(3)在main.js中引入適配文件

import './util/rem'

(4)到vue.config.js中配置插件

// 引入等比適配插件
const px2rem = require('postcss-px2rem')
// 配置基本大小
const postcss = px2rem({
  // 基準大小 baseSize,需要和rem.js中相同
  // remUnit: 14 代表 1rem = 14px; 所以當你一個14px值時,它會自動轉(zhuǎn)成 (14px/14)rem
  remUnit: 14
})
// 使用等比適配插件
module.exports = {
  lintOnSave: true,
  css: {
    loaderOptions: {
      less: {
        javascriptEnabled: true,
      },
      postcss: {
        plugins: [
          postcss,
        ],
      },
    },
  },
}

到此這篇關(guān)于Vue使得大屏自適應(yīng)的多種方法的文章就介紹到這了,更多相關(guān)vue大屏幕自適應(yīng)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue?動態(tài)style?拼接寬度問題

    vue?動態(tài)style?拼接寬度問題

    這篇文章主要介紹了vue?動態(tài)style?拼接寬度問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • Vue?插槽?Slots源碼解析與用法詳解

    Vue?插槽?Slots源碼解析與用法詳解

    這篇文章主要介紹了Vue?插槽?(Slots)?源碼解析與用法,通過實例,我們?nèi)媪私饬四J插槽、具名插槽和作用域插槽的用法,并深入理解了其在Vue源碼中的實現(xiàn)原理,需要的朋友可以參考下
    2024-01-01
  • vue實現(xiàn)登錄后頁面跳轉(zhuǎn)到之前頁面

    vue實現(xiàn)登錄后頁面跳轉(zhuǎn)到之前頁面

    本文給大家分享了vue實現(xiàn)登錄后頁面跳轉(zhuǎn)到之前頁面的一個功能,有這方便需要的朋友學(xué)習(xí)參考下吧。
    2018-01-01
  • 在Vue組件上動態(tài)添加和刪除屬性方法

    在Vue組件上動態(tài)添加和刪除屬性方法

    下面小編就為大家分享一篇在Vue組件上動態(tài)添加和刪除屬性方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-02-02
  • Vue.js中的組件系統(tǒng)

    Vue.js中的組件系統(tǒng)

    這篇文章主要介紹了Vue.js之組件系統(tǒng),本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-05-05
  • Ant Design Vue如何生成動態(tài)菜單a-menu

    Ant Design Vue如何生成動態(tài)菜單a-menu

    這篇文章主要介紹了Ant Design Vue如何生成動態(tài)菜單a-menu問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • 淺談vue2 單頁面如何設(shè)置網(wǎng)頁title

    淺談vue2 單頁面如何設(shè)置網(wǎng)頁title

    這篇文章主要介紹了淺談vue2 單頁面如何設(shè)置網(wǎng)頁title,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-11-11
  • 淺析Vue3如何實現(xiàn)頁面訪問攔截

    淺析Vue3如何實現(xiàn)頁面訪問攔截

    在Vue3中,頁面訪問攔截(Navigation?Guards)是一種常見的路由控制機制,那么Vue3具體是如何實現(xiàn)這一功能的呢,下面就跟隨小編一起來學(xué)習(xí)一下吧
    2024-03-03
  • vue實現(xiàn)購物車全部功能的簡單方法

    vue實現(xiàn)購物車全部功能的簡單方法

    vue是前端輕量級MVVM框架,入門門檻相對較低,今天用Vue做一個購物車實例,所以下面這篇文章主要給大家介紹了關(guān)于vue實現(xiàn)購物車全部功能的簡單方法,需要的朋友可以參考下
    2021-07-07
  • 解決axios發(fā)送post請求返回400狀態(tài)碼的問題

    解決axios發(fā)送post請求返回400狀態(tài)碼的問題

    今天小編就為大家分享一篇解決axios發(fā)送post請求返回400狀態(tài)碼的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-08-08

最新評論