VUE如何使得大屏自适应的几种方法?

VUE学习大屏自适应的几种方法

1.自适屏幕,始终保持16:9的比例

复制代码
<!-- 大屏固定比例16:9自适应   -->
<template>
    <div class="container">
      <div class="content" :style="getAspectRatioStyle">
        <!-- 数据展示内容 -->
      </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 {
    /* 根据计算得到的宽高样式设置 */
  }
  </style>

2.使用CSS scale属性对大屏幕做自适应处理

复制代码
<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;

// 根据浏览器大小推断缩放比例
// 首先要确定设计稿尺寸,默认是 1920 x 1080
// 分别计算浏览器和设计图宽高比
// 如果浏览器的宽高比大于设计稿的宽高比,就取浏览器高度和设计稿高度之比
// 如果浏览器的宽高比小于设计稿的宽高比,就取浏览器宽度和设计稿宽度之比
const getScale = (w = width, h = height) => {
  let ww = window.innerWidth / w;
  let wh = window.innerHeight / h;
  return ww < wh ? ww : wh;
};
/* 浏览器监听 resize 事件 */
const resize = () => {
  if (dataScreenRef.value) {
    dataScreenRef.value.style.transform = `scale(${getScale()}) translate(-50%, -50%)`;
  }
};

onMounted(() => {
  // 初始化时为外层盒子加上缩放属性,防止刷新界面时就已经缩放
  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单位转换成rem单位
复制代码
npm install postcss-px2rem --save
(2)在根目录src中新建util目录下新建rem.js等比适配文件
复制代码
// rem等比适配配置文件
// 基准大小
const baseSize = 14
// 设置 rem 函数
function setRem () {
  // 当前页面宽度相对于 1920宽的缩放比例,可根据自己需要修改。
  const scale = document.documentElement.clientWidth / 1920
  // 设置页面根节点字体大小("Math.min(scale, 2)" 指最高放大比例为2,可根据实际业务需求调整)
  document.documentElement.style.fontSize = baseSize * Math.min(scale, 2) + 'px'
}
// 初始化
setRem()
// 改变窗口大小时重新设置 `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值时,它会自动转成 (14px/14)rem
  remUnit: 14
})
 
// 使用等比适配插件
module.exports = {
  lintOnSave: true,
  css: {
    loaderOptions: {
      less: {
        javascriptEnabled: true,
      },
      postcss: {
        plugins: [
          postcss,
        ],
      },
    },
  },
}
相关推荐
程序视点10 分钟前
Umi-OCR完全指南:开源离线OCR识别软件下载安装使用教程|支持批量PDF/二维码识别
前端·后端·github
瘦瘦的小芝芝12 分钟前
es改动总结
前端
想要学好前端的阿毛14 分钟前
React状态管理库 -- Redux篇
前端
拾光拾趣录14 分钟前
前端宏(微)任务 | 从“为什么我的代码不按顺序执行”说起
前端·javascript
林太白18 分钟前
NestJS-菜单模块
前端·后端·nestjs
程序员ys20 分钟前
微前端(What)
前端·架构
狗都不学爬虫_21 分钟前
JS逆向 - (国外)SHEIN站 - 请求头(armorToken、Anti-in)
javascript·python·ajax·网络爬虫·wasm
用户7974761127322 分钟前
Mysql RR事务隔离级别引发的生产Bug,你中招了吗?
前端
Mintopia24 分钟前
🧠 三分视界:Three.js 离屏渲染与多重视角的艺术
前端·javascript·计算机图形学
JarvanMo33 分钟前
Dart & Flutter DevTools 扩展
前端