当前位置: 首页 > news >正文

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)remremUnit: 14
})// 使用等比适配插件
module.exports = {lintOnSave: true,css: {loaderOptions: {less: {javascriptEnabled: true,},postcss: {plugins: [postcss,],},},},
}
http://www.lryc.cn/news/189621.html

相关文章:

  • API接口安全运营研究(内附官方开发平台api接口接入方式)
  • 信钰证券:股票交易费用计算方法?
  • 通过js获取用户网络ip地址
  • 微信小程序wxml使用过滤器
  • 内网渗透面试问题
  • Go语言函数进阶:值传递、引用传递、函数式编程
  • 数据结构 堆——详细动画图解,形象理解
  • 使用pymodbus进行modbus-TCP通信
  • 2. redis常见数据类型
  • 多测师肖sir_高级金牌讲师_python之结构语句005
  • 用3-8译码器实现全减器
  • 招投标系统简介 企业电子招投标采购系统源码之电子招投标系统 —降低企业采购成本
  • Linux Centos7 下使用yum安装的nginx平滑升级
  • C/S架构学习之多线程实现TCP并发服务器
  • iPhone手机记笔记工具选择用哪个
  • MyBatis动态SQL(if、choose、when和otherwise)标签
  • idea将jar包deploy到本地仓库
  • 麻省理工学院与Meta AI共同开发StreamingLLM框架,实现语言模型无限处理长度
  • 记录 K8S 挂了的解决经过
  • Flink---11、状态管理(按键分区状态(值状态、列表状态、Map状态、归约状态、聚合状态)算子状态(列表状态、广播状态))
  • Vue3中使用tinymce全功能演示,包括开源功能
  • There was an error committing your changes: File could not be edited
  • 10月9日,每日信息差
  • 【软考设计师】S01 数据结构 E01 线性结构 P01 线性表
  • nginx配置https 访问
  • 希亦CG声波清洗机:眼镜党福利,家庭必备清洗机
  • 2023年10月12日历史上的今天大事件早读
  • uCOSIII实时操作系统 五 任务API(时间片轮转API调度)
  • 微信小程序项目如何用Hbuild启动,先让对方在微信开发平台将你的微信号添加成开发成员。
  • 应对广告虚假流量,app广告变现该如何风控?