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,
        ],
      },
    },
  },
}
相关推荐
开开心心就好3 小时前
批量提取PDF中的图片,直接导出原图
前端·javascript·支持向量机·智能手机·pdf·html·启发式算法
Setsuna_F_Seiei5 小时前
前端转型 Agent 开发 05 之 Agent Hooks 与 Checkpointer(让 Agent 从全自动转变人为可掌控)
前端·agent·ai编程
百万蹄蹄向前冲7 小时前
风扇转了一晚上MVP专家团翻车事故
前端·人工智能
默_笙7 小时前
🏛 给 AI 配一间办公室:Harness Engineering 六大模块与它的实现
前端·javascript
linux_cfan8 小时前
videojs v10 源代码系列解读:14 · 谓词守卫:在运行时安全地调用能力
前端·javascript·音视频
kyriewen9 小时前
我扒了 10,221 条 JD:腾讯技术岗 75% 在要 AI
前端·人工智能·ai编程
郑州光合科技余经理9 小时前
同城外卖小程序开发:下单成功后,后台导出能不能对上用户端状态
开发语言·前端·git·后端·uni-app·php·ai编程
+VX:Fegn089510 小时前
计算机毕业设计|基于springboot + vue旅游管理系统(源码+数据库+文档)
java·开发语言·vue.js·spring boot·课程设计
雪芽蓝域zzs10 小时前
第 7 章:双 Y 轴组合图(柱状 + 折线,看板高频场景)
vue.js·echars
IT_陈寒10 小时前
Vue的响应式让我熬到凌晨三点,原来漏了这个小细节
前端·人工智能·后端