最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2023年10月12日 09:21:26   作者:幕颯前端程序員  
這篇文章主要介紹了Vue使得大屏自適應(yīng)的多種方法,自適屏幕,始終保持16:9的比例,還一種是使用CSS scale屬性對(duì)大屏幕做自適應(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) {
          // 以高度為基準(zhǔn),按比例計(jì)算寬度
          contentHeight.value = containerHeight;
          contentWidth.value = Math.floor(containerHeight * aspectRatio);
        } else {
          // 以寬度為基準(zhǔn),按比例計(jì)算高度
          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ù)計(jì)算得到的寬高樣式設(shè)置 */
  }
  </style>

2.使用CSS scale屬性對(duì)大屏幕做自適應(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è)計(jì)稿尺寸,默認(rèn)是 1920 x 1080
// 分別計(jì)算瀏覽器和設(shè)計(jì)圖寬高比
// 如果瀏覽器的寬高比大于設(shè)計(jì)稿的寬高比,就取瀏覽器高度和設(shè)計(jì)稿高度之比
// 如果瀏覽器的寬高比小于設(shè)計(jì)稿的寬高比,就取瀏覽器寬度和設(shè)計(jì)稿寬度之比
const getScale = (w = width, h = height) => {
  let ww = window.innerWidth / w;
  let wh = window.innerHeight / h;
  return ww < wh ? ww : wh;
};
/* 瀏覽器監(jiān)聽(tīng) resize 事件 */
const resize = () => {
  if (dataScreenRef.value) {
    dataScreenRef.value.style.transform = `scale(${getScale()}) translate(-50%, -50%)`;
  }
};
onMounted(() => {
  // 初始化時(shí)為外層盒子加上縮放屬性,防止刷新界面時(shí)就已經(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下載插件,自動(dòng)將px單位轉(zhuǎn)換成rem單位

npm install postcss-px2rem --save

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

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

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

import './util/rem'

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

// 引入等比適配插件
const px2rem = require('postcss-px2rem')
// 配置基本大小
const postcss = px2rem({
  // 基準(zhǔn)大小 baseSize,需要和rem.js中相同
  // remUnit: 14 代表 1rem = 14px; 所以當(dāng)你一個(gè)14px值時(shí),它會(huì)自動(dòng)轉(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Vue制作Todo List網(wǎng)頁(yè)

    Vue制作Todo List網(wǎng)頁(yè)

    這篇文章主要為大家詳細(xì)介紹了Vue制作Todo List網(wǎng)頁(yè)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • VUE 動(dòng)態(tài)組件的應(yīng)用案例分析

    VUE 動(dòng)態(tài)組件的應(yīng)用案例分析

    這篇文章主要介紹了VUE 動(dòng)態(tài)組件的應(yīng)用,結(jié)合具體案例形式分析了vue.js動(dòng)態(tài)組件的應(yīng)用場(chǎng)景、解決方案及相關(guān)操作技巧,需要的朋友可以參考下
    2019-12-12
  • 在Vue mounted方法中使用data變量詳解

    在Vue mounted方法中使用data變量詳解

    今天小編就為大家分享一篇在Vue mounted方法中使用data變量詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-11-11
  • vue yaml代碼編輯器組件問(wèn)題

    vue yaml代碼編輯器組件問(wèn)題

    這篇文章主要介紹了vue yaml代碼編輯器組件問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • Vue之Watcher源碼解析(1)

    Vue之Watcher源碼解析(1)

    這篇文章主要為大家詳細(xì)介紹了Vue源碼之Watcher的基礎(chǔ)知識(shí),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • vue運(yùn)行環(huán)境搭建全過(guò)程

    vue運(yùn)行環(huán)境搭建全過(guò)程

    這篇文章主要介紹了vue運(yùn)行環(huán)境搭建全過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • vue中關(guān)于_ob_:observer的處理方式

    vue中關(guān)于_ob_:observer的處理方式

    這篇文章主要介紹了vue中關(guān)于_ob_:observer的處理方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • vuex+axios+element-ui實(shí)現(xiàn)頁(yè)面請(qǐng)求loading操作示例

    vuex+axios+element-ui實(shí)現(xiàn)頁(yè)面請(qǐng)求loading操作示例

    這篇文章主要介紹了vuex+axios+element-ui實(shí)現(xiàn)頁(yè)面請(qǐng)求loading操作,結(jié)合實(shí)例形式分析了vuex+axios+element-ui實(shí)現(xiàn)頁(yè)面請(qǐng)求過(guò)程中l(wèi)oading遮罩層相關(guān)操作技巧與使用注意事項(xiàng),需要的朋友可以參考下
    2020-02-02
  • vue2 mint-ui loadmore實(shí)現(xiàn)下拉刷新,上拉更多功能

    vue2 mint-ui loadmore實(shí)現(xiàn)下拉刷新,上拉更多功能

    這篇文章主要介紹了vue2 mint-ui loadmore實(shí)現(xiàn)下拉刷新,上拉更多功能,需要的朋友可以參考下
    2018-03-03
  • 關(guān)于vue中@click.native.prevent的說(shuō)明

    關(guān)于vue中@click.native.prevent的說(shuō)明

    這篇文章主要介紹了關(guān)于vue中@click.native.prevent的說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03

最新評(píng)論

疏附县| 呼图壁县| 繁昌县| 湘潭县| 遂溪县| 大安市| 剑河县| 内江市| 张家口市| 海南省| 天台县| 金堂县| 五常市| 安平县| 乌苏市| 石门县| 平武县| 赣榆县| 临澧县| 保山市| 彩票| 伊川县| 安化县| 沧州市| 长汀县| 太原市| 克拉玛依市| 渭源县| 涟源市| 沁源县| 遵化市| 崇阳县| 双鸭山市| 曲周县| 阿城市| 乐亭县| 宁晋县| 玉林市| 南城县| 柞水县| 衡水市|