根据屏幕尺寸缩放代码
Posted 小企鹅推雪球!
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了根据屏幕尺寸缩放代码相关的知识,希望对你有一定的参考价值。
function refreshScale() {
let docWidth = document.documentElement.clientWidth;
let docHeight = document.documentElement.clientHeight;
var designWidth = 1920,
designHeight = 1080,
widthRatio = docWidth / designWidth,
heightRatio = docHeight / designHeight;
document.body.style = "transform:scale(" + widthRatio + "," + heightRatio + ");transform-origin:left top;"
// 应对浏览器全屏切换前后窗口因短暂滚动条问题出现未占满情况
setTimeout(function () {
var lateWidth = document.documentElement.clientWidth,
lateHeight = document.documentElement.clientHeight;
if (lateWidth === docWidth) return;
widthRatio = lateWidth / designWidth
heightRatio = lateHeight / designHeight
document.body.style = "transform:scale(" + widthRatio + "," + heightRatio + ");transform-origin:left top;"
}, 0)
};
function mhs_onresize() {
var bodyStyle = document.createElement('style')
bodyStyle.innerhtml = `body{width:1920px; height:1080px!important;overflow: hidden}`
document.documentElement.firstElementChild.appendChild(bodyStyle)
this.refreshScale()
window.addEventListener("pageshow", function (e) {
if (e.persisted) { // 浏览器后退的时候重新计算
this.refreshScale()
}
}, false);
window.addEventListener("resize", this.refreshScale, false);
};
mhs_onresize()
以上是关于根据屏幕尺寸缩放代码的主要内容,如果未能解决你的问题,请参考以下文章