位置: IT常识 - 正文

vue 项目的屏幕自适应方案(vue display)

编辑:rootadmin
vue 项目的屏幕自适应方案 方案一:使用 scale-box 组件

推荐整理分享vue 项目的屏幕自适应方案(vue display),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:vue display,vue做大屏,vue大屏展示项目实例,vue全屏显示,vue全屏显示,vue大屏展示项目实例,vue大屏展示项目实例,vue 大屏显示,内容如对您有帮助,希望把文章链接给更多的朋友!

属性:

width 宽度 默认 1920height 高度 默认 1080bgc 背景颜色 默认 "transparent"delay自适应缩放防抖延迟时间(ms) 默认 100

vue2版本:vue2大屏适配缩放组件(vue2-scale-box - npm)

npm install vue2-scale-box

或者

yarn add vue2-scale-box

使用方法:

<template> <div> <scale-box :width="1920" :height="1080" bgc="transparent" :delay="100"> <router-view /> </scale-box> </div></template><script>import ScaleBox from "vue2-scale-box";export default { components: { ScaleBox },};</script><style lang="scss">body { margin: 0; padding: 0; background: url("@/assets/bg.jpg");}</style>

vue3版本:vue3大屏适配缩放组件(vue3-scale-box - npm)

npm install vue3-scale-box

或者

yarn add vue3-scale-boxvue 项目的屏幕自适应方案(vue display)

使用方法:

<template> <ScaleBox :width="1920" :height="1080" bgc="transparent" :delay="100"> <router-view /> </ScaleBox></template><script>import ScaleBox from "vue3-scale-box";</script><style lang="scss">body { margin: 0; padding: 0; background: url("@/assets/bg.jpg");}</style>方案二:设置设备像素比例(设备像素比)

在项目的 utils 下新建 devicePixelRatio.js 文件

class devicePixelRatio { /* 获取系统类型 */ getSystem() { const agent = navigator.userAgent.toLowerCase(); const isMac = /macintosh|mac os x/i.test(navigator.userAgent); if (isMac) return false; // 目前只针对 win 处理,其它系统暂无该情况,需要则继续在此添加即可 if (agent.indexOf("windows") >= 0) return true; } /* 监听方法兼容写法 */ addHandler(element, type, handler) { if (element.addEventListener) { element.addEventListener(type, handler, false); } else if (element.attachEvent) { element.attachEvent("on" + type, handler); } else { element["on" + type] = handler; } } /* 校正浏览器缩放比例 */ correct() { // 页面devicePixelRatio(设备像素比例)变化后,计算页面body标签zoom修改其大小,来抵消devicePixelRatio带来的变化 document.getElementsByTagName("body")[0].style.zoom = 1 / window.devicePixelRatio; } /* 监听页面缩放 */ watch() { const that = this; // 注意: 这个方法是解决全局有两个window.resize that.addHandler(window, "resize", function () { that.correct(); // 重新校正浏览器缩放比例 }); } /* 初始化页面比例 */ init() { const that = this; // 判断设备,只在 win 系统下校正浏览器缩放比例 if (that.getSystem()) { that.correct(); // 校正浏览器缩放比例 that.watch(); // 监听页面缩放 } }}export default devicePixelRatio;

在 App.vue 中引入并使用即可

<template> <div> <router-view /> </div></template><script>import devPixelRatio from "@/utils/devicePixelRatio.js";export default { created() { new devPixelRatio().init(); // 初始化页面比例 },};</script><style lang="scss">body { margin: 0; padding: 0;}</style>方案三:通过 JS 设置 zoom 属性调整缩放比例

在项目的 utils 下新建 monitorZoom.js 文件

export const monitorZoom = () => { let ratio = 0, screen = window.screen, ua = navigator.userAgent.toLowerCase(); if (window.devicePixelRatio !== undefined) { ratio = window.devicePixelRatio; } else if (~ua.indexOf("msie")) { if (screen.deviceXDPI && screen.logicalXDPI) { ratio = screen.deviceXDPI / screen.logicalXDPI; } } else if ( window.outerWidth !== undefined && window.innerWidth !== undefined ) { ratio = window.outerWidth / window.innerWidth; } if (ratio) { ratio = Math.round(ratio * 100); } return ratio;};

在 main.js 中引入并使用即可

import { monitorZoom } from "@/utils/monitorZoom.js";const m = monitorZoom();if (window.screen.width * window.devicePixelRatio >= 3840) { document.body.style.zoom = 100 / (Number(m) / 2); // 屏幕为 4k 时} else { document.body.style.zoom = 100 / Number(m);}

完整代码

import Vue from "vue";import App from "./App.vue";import router from "./router";/* 调整缩放比例 start */import { monitorZoom } from "@/utils/monitorZoom.js";const m = monitorZoom();if (window.screen.width * window.devicePixelRatio >= 3840) { document.body.style.zoom = 100 / (Number(m) / 2); // 屏幕为 4k 时} else { document.body.style.zoom = 100 / Number(m);}/* 调整缩放比例 end */Vue.config.productionTip = false;new Vue({ router, render: (h) => h(App),}).$mount("#app");获取屏幕的分辨率

获取屏幕的宽:

window.screen.width * window.devicePixelRatio

获取屏幕的高:

window.screen.height * window.devicePixelRatio移动端适配(使用 postcss-px-to-viewport 插件)

官网:https://github.com/evrone/postcss-px-to-viewport

npm install postcss-px-to-viewport --save-dev

或者

yarn add -D postcss-px-to-viewport

配置适配插件的参数(在项目根目录创建 .postcssrc.js 文件[与 src 目录平级])粘贴以下代码即可

module.exports = { plugins: { autoprefixer: {}, // 用来给不同的浏览器自动添加相应前缀,如-webkit-,-moz-等等 "postcss-px-to-viewport": { unitToConvert: "px", // 需要转换的单位,默认为"px" viewportWidth: 390, // UI设计稿的宽度 unitPrecision: 6, // 转换后的精度,即小数点位数 propList: ["*"], // 指定转换的css属性的单位,*代表全部css属性的单位都进行转换 viewportUnit: "vw", // 指定需要转换成的视窗单位,默认vw fontViewportUnit: "vw", // 指定字体需要转换成的视窗单位,默认vw selectorBlackList: ["wrap"], // 需要忽略的CSS选择器,不会转为视口单位,使用原有的px等单位 minPixelValue: 1, // 默认值1,小于或等于1px则不进行转换 mediaQuery: false, // 是否在媒体查询的css代码中也进行转换,默认false replace: true, // 是否直接更换属性值,而不添加备用属性 exclude: [/node_modules/], // 忽略某些文件夹下的文件或特定文件,用正则做目录名匹配,例如 'node_modules' 下的文件 landscape: false, // 是否处理横屏情况 landscapeUnit: "vw", // 横屏时使用的视窗单位,默认vw landscapeWidth: 2048 // 横屏时使用的视口宽度 } }};
本文链接地址:https://www.jiuchutong.com/zhishi/298547.html 转载请保留说明!

上一篇:Vue3父子组件间传参通信(vue父组件子组件)

下一篇:Vue3 中 axios 的安装及使用(vue3+antd)

  • 工资表个税多扣了账务处理递减
  • 消费税和所得税的关系
  • 房产税会计分录怎么写
  • 税收保全措施有金银首饰吗
  • 信息采集需要填两个家庭成员,但只能有一个监护人
  • 其他应收款减值测试注意什么
  • 个人社保缴费多少钱一个月
  • 纳税申报表真伪验证
  • 个税缴款三方协议
  • 环保设备折旧年限和残值率
  • 发票没用完可以申请超限量吗
  • 股东个人为公司付的钱
  • 进销的单位不一样怎么办
  • 明细分类核算的方法分为
  • 社保费补缴有滞纳金吗
  • 小规模餐饮业会计核算
  • 应收账款预付账款属于什么科目
  • 道路运输业税率多少
  • 申请最高开票限额不超过10万元的无需事前实地查验
  • 增值税纳税表销售额的填写
  • 出售固定资产支付的相关费用计入
  • 分拆业务所涉及客户
  • 个人销售比例用什么函数
  • 固定资产清理税金如何处理
  • 单位登记注册类型指的是什么
  • 自己开电子发票要什么软件
  • 加工贸易企业如何财务管理账务流程
  • 税盘服务费抵税分录
  • 生产企业出口需要什么手续
  • 收到财政奖励扶持资金账务处理?
  • 代理进口增值税客户不抵扣,进出口公司可以抵扣吗
  • windows11怎么安装iis
  • thinkphp5控制器
  • 结转待抵扣
  • 长期挂账的其他应付账款怎么处理
  • 广度优先算法代码
  • 老生常谈PHP 文件写入和读取(必看篇)
  • 开票一定要对公户嘛
  • torch训练模型
  • java项目报错
  • php显示错误报告方式
  • 深度学习论文精读[6]:UNet++
  • php防止用户重度登录
  • 税费滞纳金计入增值税吗
  • 纳税证明和完税证明的金额为什么不一样
  • phpcms文档
  • dubbo dubbox
  • 以房租入股公司怎么交税
  • 预计净残值影响营业利润吗
  • 子公司注销合并报表少数股东权益的处理
  • 餐厅吃饭不小心把餐具打破需要赔吗?
  • 企业预缴增值税附加税率
  • 建设银行e信通介绍
  • 回购注销库存股的会计处理
  • 非营利组织注册资金可以用吗
  • 公司新装宽带怎么安装
  • 内部招待所管理规定
  • 销售回款率怎么计算,麻烦知道的告诉我,11
  • 支付宝扣手续费是怎么回事
  • 财政返还什么意思
  • 明细账的作用
  • 使用权资产
  • kvm虚拟机paused
  • win7系统怎么修复安装系统
  • 繁体系统安装简体软件
  • win2008如何安装telnet
  • ubuntu20.04设置
  • Win7系统打开文件夹没有自动排列选项
  • make:arm-linux- conmand not found错误处理探讨
  • win8装机教程
  • 硬件茶谈win10系统安装
  • win8开机自启动在哪里设置
  • android本地保存数据
  • unityxlua热更新方案
  • jquery foreach循环
  • 教育培训行业的发展前景
  • 增值税计税依据含运费吗
  • 税务社保费是什么意思
  • 问一下医院
  • 平板电脑购物
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设