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, ], }, }, }, }
VUE如何使得大屏自适应的几种方法?
幕飒前端程序员2023-10-11 20:48
相关推荐
Zha0Zhun38 分钟前
一个使用ViewBinding封装的Dialog兆子龙39 分钟前
从微信小程序 data-id 到 React 列表性能优化:少用闭包,多用 data-*滕青山39 分钟前
文本行过滤/筛选 在线工具核心JS实现时光不负努力41 分钟前
编程常用模式集合时光不负努力42 分钟前
ts+vue3开发规范大雨还洅下1 小时前
前端JS: 跨域解决恋猫de小郭1 小时前
Apple 的 ANE 被挖掘,AI 硬件公开,宣传的 38 TOPS 居然是"数字游戏"?小岛前端1 小时前
Node.js 宣布重大调整,运行十年的规则要改了!OpenTiny社区1 小时前
OpenTiny NEXT-SDK 重磅发布:四步把你的前端应用变成智能应用梦想CAD控件1 小时前
在线CAD开发包结构与功能说明