vue pc端-移动端-ipad端适配

全局使用transform:scale来进行适配

在utils下创建js文件,比如命名为:autofit.js

let currRenderDom = null;
let currelRectification = "";
let currelRectificationLevel = "";
let resizeListener = null;
let timer = null;
let currScale = 1;
let isAutofitRunnig = false;
let isElRectification = false;
let keyboardHeight = 0;const autofit = {init(options = {}, isShowInitTip = true) {if (isShowInitTip) {console.log(`%c` + `autofit.js` + ` is running`,`font-weight: bold; color: #ffb712; background:linear-gradient(-45deg, #bd34fe 50%, #47caff 50% );background: -webkit-linear-gradient( 120deg, #bd34fe 30%, #41d1ff );background-clip: text;-webkit-background-clip: text; -webkit-text-fill-color:linear-gradient( -45deg, #bd34fe 50%, #47caff 50% ); padding: 8px 12px; border-radius: 4px;`);console.log(options.dw);}const {dw = 1920,dh = 929,el = typeof options === "string" ? options : "#app",resize = true,ignore = [],transition = "none",delay = 0,} = options;currRenderDom = el;let dom = document.querySelector(el);if (!dom) {console.error(`autofit: '${el}' is not exist`);return;}defaultHeight = document.documentElement.clientHeight;defaultWidth = document.documentElement.clientWidth;const style = document.createElement("style");const ignoreStyle = document.createElement("style");style.lang = "text/css";ignoreStyle.lang = "text/css";style.id = "autofit-style";ignoreStyle.id = "ignoreStyle";style.innerHTML = `body {overflow: hidden;}`;dom.appendChild(style);dom.appendChild(ignoreStyle);dom.style.height = `${dh}px`;dom.style.width = `${dw}px`;dom.style.transformOrigin = `0 0`;keepFit(dw, dh, dom, ignore);resizeListener = () => {clearTimeout(timer);if (delay != 0)timer = setTimeout(() => {keepFit(dw, dh, dom, ignore);isElRectification &&elRectification(currelRectification, currelRectificationLevel);}, delay);else {keepFit(dw, dh, dom, ignore);isElRectification &&elRectification(currelRectification, currelRectificationLevel);}};resize && window.addEventListener("resize", resizeListener);isAutofitRunnig = true;setTimeout(() => {dom.style.transition = `${transition}s`;});},off(el = "#app") {try {isElRectification = false;window.removeEventListener("resize", resizeListener);document.querySelector("#autofit-style").remove();document.querySelector(currRenderDom ? currRenderDom : el).style = "";isElRectification && offelRectification();} catch (error) {console.error(`autofit: Failed to remove normally`, error);isAutofitRunnig = false;}isAutofitRunnig &&console.log(`%c` + `autofit.js` + ` is off`,`font-weight: bold;color: #707070; background: #c9c9c9; padding: 8px 12px; border-radius: 4px;`);},
};function elRectification(el, level = 1) {if (!isAutofitRunnig) {console.error("autofit.js:autofit has not been initialized yet");}!el && console.error(`autofit.js:bad selector: ${el}`);currelRectification = el;currelRectificationLevel = level;const currEl = document.querySelectorAll(el);if (currEl.length == 0) {console.error("autofit.js:elRectification found no element");return;}for (let item of currEl) {if (!isElRectification) {item.originalWidth = item.clientWidth;item.originalHeight = item.clientHeight;}let rectification = currScale == 1 ? 1 : currScale * level;item.style.width = `${item.originalWidth * rectification}px`;item.style.height = `${item.originalHeight * rectification}px`;item.style.transform = `scale(${1 / currScale})`;item.style.transformOrigin = `0 0`;}isElRectification = true;
}
function offelRectification() {if (!currelRectification) return;for (let item of document.querySelectorAll(currelRectification)) {item.style.width = ``;item.style.height = ``;item.style.transform = ``;}
}
function keepFit(dw, dh, dom, ignore) {let clientHeight = document.documentElement.clientHeight;let clientWidth = document.documentElement.clientWidth;currScale =clientWidth / clientHeight < dw / dh ? clientWidth / dw : clientHeight / dh;dom.style.height = `${clientHeight / currScale}px`;dom.style.width = `${clientWidth / currScale}px`;dom.style.transform = `scale(${currScale})`;for (let item of ignore) {let itemEl = item.el || item.dom;typeof item == "string" && (itemEl = item);if (!itemEl) {console.error(`autofit: bad selector: ${itemEl}`);continue;}let realScale = item.scale ? item.scale : 1 / currScale;let realFontSize = realScale != currScale ? item.fontSize : "autofit";let realWidth = realScale != currScale ? item.width : "autofit";let realHeight = realScale != currScale ? item.height : "autofit";let regex = new RegExp(`${itemEl}(\x20|{)`, "gm");let isIgnored = regex.test(document.querySelector("#ignoreStyle").innerHTML);if (isIgnored) {continue;}document.querySelector("#ignoreStyle").innerHTML += `\n${itemEl} { transform: scale(${realScale})!important;transform-origin: 0 0;width: ${realWidth}!important;height: ${realHeight}!important;}`;document.querySelector("#ignoreStyle").innerHTML += `\n${itemEl} div ,${itemEl} span,${itemEl} a,${itemEl} * {font-size: ${realFontSize}px;}`;}
}
export { elRectification };
export default autofit;

在App.vue中引入使用

import autofit from "@/utils/autofit";
autofit.init({dh: 1080,dw: 1920,el: "#app",resize: true,
});
如果想要某个不在上述设置的el下的dom也使用响应式,添加transform
  1. 在上述代码的基础上添加下述代码
// 定义一个需要额外添加transform的属性默认为false
let hasFlag = false
// 在init方法中的options额外添加一个参数needScaleClass
const {dw = 1920,dh = 929,el = typeof options === 'string' ? options : '#app',resize = true,ignore = [],transition = 'none',delay = 0,needScaleClass = ''} = options// 然后获取当前所有类名为needScaleClass的dom(init方法中)
let scaleDom = document.querySelectorAll(needScaleClass)if (scaleDom) {hasFlag = true}if (hasFlag) {needScaleDom(dw, dh, scaleDom)}// resize监听中调用方法resizeListener = () => {clearTimeout(timer)if (delay != 0)timer = setTimeout(() => {keepFit(dw, dh, dom, ignore)if (hasFlag) {needScaleDom(dw, dh, scaleDom)}isElRectification &&elRectification(currelRectification, currelRectificationLevel)}, delay)else {keepFit(dw, dh, dom, ignore)if (hasFlag) {needScaleDom(dw, dh, scaleDom)}isElRectification &&elRectification(currelRectification, currelRectificationLevel)}}setTimeout(() => {dom.style.transition = `${transition}s`if (hasFlag) {for(let m = 0; m<scaleDom.length;m++) {scaleDom[m].style.transition = `${transition}s`}}})//添加方法给dom赋值style
function needScaleDom (dw, dh, dom) {let clientHeight = document.documentElement.clientHeightlet clientWidth = document.documentElement.clientWidthcurrScale =clientWidth / clientHeight < dw / dh ? clientWidth / dw : clientHeight / dhfor(let k = 0; k<dom.length;k++) {dom[k].style.transform = `scale(${currScale})`}}
  1. App.vue中使用
import autofit from "@/utils/autofit";
autofit.init({dh: 1080,dw: 1920,el: "#app",resize: true,needScaleClass: '.message-toolip-box',
});
在移动端的使用

在移动端的时候,如果又要输入内容的时候键盘弹起会导致可视页面变小,就会触发resize监听从而改变scale的值,但是如果我们在输入内容时不想改变页面大小想保持原状,上述代码需要添加键盘弹起判断了

// 全局定义默认宽高
let defaultHeight = 0;
let defaultWidth = 0;
// 在resize监听中
resizeListener = () => {const windowHeight = window.innerHeight;const windowWidth = window.innerWidth;// 如果当前窗口高度小于默认高度,且宽度和默认宽度相等则说明键盘是打开状态let keyboardHeight = defaultHeight - windowHeight;if (keyboardHeight > 0 && windowWidth == defaultWidth) {let transform = dom.style.transform;const result = transform.slice(0, transform.indexOf(")") + 1);let a = "translateY(-" + keyboardHeight + "px)";dom.style.transform = result + a;return;}// 宽度和默认宽度不相等,可能是切换了不同分辨率或者变为了横屏状态if (windowWidth != defaultWidth) {defaultHeight = document.documentElement.clientHeight;defaultWidth = document.documentElement.clientWidth;}clearTimeout(timer);if (delay != 0)timer = setTimeout(() => {keepFit(dw, dh, dom, ignore);isElRectification &&elRectification(currelRectification, currelRectificationLevel);}, delay);else {keepFit(dw, dh, dom, ignore);isElRectification &&elRectification(currelRectification, currelRectificationLevel);}};

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/584915.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【网络安全 | XCTF】simple_transfer

考察kali基本工具的使用 方法一 打开文件如图&#xff1a; 存在较多协议&#xff0c;将协议分级&#xff1a; 可以看到DLEP协议占比最大&#xff1a; 将其作为过滤器应用&#xff1a; 搜索DLEP&#xff1a; 并没有有利信息&#xff0c;但观察到多数数据包损坏&#xff1a; 执行…

Transformer(seq2seq、self-attention)学习笔记

在self-attention 基础上记录一篇Transformer学习笔记 Transformer的网络结构EncoderDecoder 模型训练与评估 Transformer的网络结构 Transformer是一种seq2seq 模型。输入一个序列&#xff0c;经过encoder、decoder输出结果也是一个序列&#xff0c;输出序列的长度由模型决定…

【超图】SuperMap iClient3D for WebGL/WebGPU —— 坐标系位置 —— Cartesian2

作者&#xff1a;taco 说到关于地理必然逃不开位置的关系。借用百度百科的内容来说地理学&#xff08;geography&#xff09;&#xff0c;是研究地球表层空间地理要素或者地理综合体空间分布规律、时间演变过程和区域特征的一门学科。所以位置&坐标系必然逃不掉了。那么在S…

自检服务器,无需服务器、不用编程。

自检服务器&#xff0c;无需服务器、不用编程。 大家好&#xff0c;我是JavaPub. 这几年自媒体原来热&#xff0c;很多人都知道了个人 IP 的重要性。连一个搞中医的朋友都要要做一个自己的网站&#xff0c;而且不想学编程、还不想花 RMB 租云服务。 老读者都知道&#xff0c…

SpringCloud(H版alibaba)框架开发教程之Ribbon负载均衡和Openfeign远程调用——附源码(3)

源码地址在这套教程的第一个里 我们打开order80的主类上的注释 Spring Cloud Ribbon是一个基于HTTP和TCP的客户端负载均衡工具&#xff0c;它基于Netflix Ribbon实现。通过Spring Cloud的封装&#xff0c;可以让我们轻松地将面向服务的REST模版请求自动转换成客户端负载均衡的…

[RoarCTF 2019]Easy Java(java web)

题目 页面如下 页面长得像sql注入 点击help看一下 这里需要了解java web目录结构 WEB INF:Java的web应用安全目录&#xff1b; 此外如果想在页面访问WEB-INF应用里面的文件&#xff0c;必须要通过web.xml进行相应的映射才能访问&#xff1b; WEB-INF是Java Web应用程序中的一…

Java EE 网络原理之HTTPS

文章目录 1. HTTPS 是什么&#xff1f;2. "加密" 是什么&#xff1f;3. HTTPS 的工作过程3.1 引入对称加密3.2 引入非对称加密3.3 中间人攻击3.4 引入证书 4. Tomecat4.1 tomcat 的作用 1. HTTPS 是什么&#xff1f; HTTPS也是⼀个应用层协议&#xff0c;是在 HTTP …

js实时监听input输入框值的变化

实习日记之通过调用common chemistry的api接口实现输入keyword查找cas号和mw。做了一个简单的html网页&#xff0c;用到了ajax技术。比较简单&#xff0c;适合刚入门的宝学习参考。代码如下&#xff1a; <!DOCTYPE html> <html lang"en"> <head>&l…

Ubuntu22.04 双系统安装、配置及常用设置

Ubuntu双系统安装、配置及常用设置 前期准备下载Ubuntu系统镜像下载镜像文件写入工具制作Ubuntu启动盘磁盘分区更改 BIOS 引导模式 安装系统更改回原来的 BIOS 参数配置 Ubuntu 系统配置源同步Windows和Ubuntu系统时间更改启动菜单的默认项添加右键新建文本文档在桌面上创建软件…

【HBuilder + IDEA + XFtp + XShell】打包部署上线

简述 前后端分离&#xff1a;需要将前后端的程序包打包发送至应用Linux服务器上Linux服务器 &#xff08;1&#xff09;需要启用SSHD服务&#xff0c;该服务会监听22号端口&#xff08;一般是开启的&#xff09; &#xff08;2&#xff09;搭建&#xff1a;MYSQL、Nginx、jdk、…

《HelloGitHub》第 93 期

兴趣是最好的老师&#xff0c;HelloGitHub 让你对编程感兴趣&#xff01; 简介 HelloGitHub 分享 GitHub 上有趣、入门级的开源项目。 这里有实战项目、入门教程、黑科技、开源书籍、大厂开源项目等&#xff0c;涵盖多种编程语言 Python、Java、Go、C/C、Swift...让你在短时间内…

[Angular] 笔记 18:Angular Router

Angular Router 视频 chatgpt&#xff1a; Angular 具有内置的大量工具、功能和库&#xff0c;功能强大且经过良好设计&#xff0c;如组件化架构、依赖注入、模块化系统、路由和HTTP客户端等。这些功能可以直接用于项目中&#xff0c;无需额外的设置或第三方库。这简化了开发流…

显示器与按键(LCD 1602 + button)

一、实验目的&#xff1a; &#xff08;1&#xff09;学习lcd 1602的编程与使用、 &#xff08;2&#xff09;机械式复位开关button软件消抖的方法。 二、实验内容&#xff1a; 1、必做&#xff1a;先显示开机画面&#xff0c;&#xff1a;在1602显示器上&#xff0c;分两行…

Windows自动化之UIautomation(一)

一、UIAutomation的工作原理&#xff1a; UIAutomation操作程序时会给程序发送WM_GETOBJECT消息&#xff0c;如果程序处理WM_GETOBJECT消息&#xff0c;实现UI Automation Provider,并调用函数 UiaReturnRawElementProvider(HWND hwnd,WPARAM wparam,LPARAM lparam,IRawElemen…

白话机器学习的数学-2-分类

1、设置问题 图片分类&#xff1a;只根据尺寸把它分类为 纵向图像和横向图像。 如果只用一条线将图中白色的点和黑色的点分开&#xff1a; 这次分类的目的就是找到这条线。 2、内积 找到一条线&#xff0c;这是否意味着我们要像学习回归时那样&#xff0c;求出一次函数的斜率…

关于 K8s 的一些基础概念整理

〇、前言 Kubernetes&#xff0c;将中间八个字母用数字 8 替换掉简称 k8s&#xff0c;是一个开源的容器集群管理系统&#xff0c;由谷歌开发并维护。它为跨主机的容器化应用提供资源调度、服务发现、高可用管理和弹性伸缩等功能。 下面简单列一下 k8s 的几个特性&#xff1a; 自…

学习笔记-MyBatis的工作原理。

Mybatis的工作原理 读取MyBatis配置文件&#xff1a;mybatis-config.xml为MyBatis的全局配置文件&#xff0c;配置了MyBatis的运行环境等信息&#xff0c;例如数据库连接信息。加载映射文件。映射文件即SQL映射文件&#xff0c;该文件中配置了操作数据库的SQL语句&#xff0c;…

如何使用人工智能算法解决实际业务问题?

使用人工智能算法解决实际业务问题的过程可以概括为以下几个步骤&#xff1a; 问题定义和数据采集&#xff1a;首先&#xff0c;需要明确业务问题&#xff0c;并收集相关的数据&#xff0c;包括结构化和非结构化数据。这些数据可以来自不同的来源&#xff0c;如数据库、文本、图…

clickhouse连接工具dbeaver

地址 地址&#xff1a; Download | DBeaver Community 安装 表引擎 表引擎之TinyLog 以列文件的形式保存在磁盘上&#xff0c;不支持索引&#xff0c;没有并发控制。一般保存少量数据的小表&#xff0c; 生产环境上作用有限&#xff0c;多用于平时练习测试用。 内存引擎&am…

k8s 架构

主要组件 k8s有如下的主要组件&#xff1a; Control plane(s) and worker node(s)OperatorsServicesPods of containersNamespaces and quotasNetwork and policiesStorage. 一个k8s集群是有一个或多个 cp&#xff08;控制平面&#xff09;节点和一组worker 节点组成的。这个…