之前发的PC端的适配我发现在火狐和IE等一些浏览器老版本内核的情况下没办法实现,原因是zoom属性内核过老无法识别到这个属性,那个我又想到了这个方案,原理是一样的,只是采用不一样的css属性进行封装
// 设计稿:1920 * 1080
// 设配目标:1920 * 1080 ( 1 : 1) | 3840* 2160 ( 2 : 2 ) | 7680 * 2160 ( 4 : 2)
// 1.设计稿尺寸
let targetWidth = 1920;
let targetHeight = 1080;
let targetRatio = 16 / 9; // 宽高比率 (宽 / 高)
// 2.拿到当前设备(浏览器)的宽度
let currentWidth =
document.documentElement.clientWidth || document.body.clientWidth;
let currentHeight =
document.documentElement.clientHeight || document.body.clientHeight;
// 3.计算缩放比率(屏幕过宽,根据高度计算缩放比例)
let scaleRatio = currentWidth / targetWidth; // 参照宽度进行缩放(默认情况下)
// 当前宽高比例
let currentRatio = currentWidth / currentHeight;
if (currentRatio > targetRatio) {
scaleRatio = currentHeight / targetHeight; // 参照高度进行缩放(屏幕很宽的情况下)
document.body.style = `transform: scale(${scaleRatio}) translateX(-50%); left: 50%;`;
} else {
// 4.开始缩放网页
document.body.style = `transform: scale(${scaleRatio})`;