大屏1920*1080缩放适应页面

1.设置需要缩放的div的宽高为1920*1080

#transMain{
  width:1920px;
  height:1010px;
  transform-origin:left top;
}

2.缩放

let scalX = Number(window.innerWidth)/1920
let scalY = Number(window.innerHeight)/1080
document.getElementById("transMain").style.transform = "scale("+scalX+","+scalY+")"

原理其实很简单,就是获取页面的宽高,与1920*1080的比例差距,然后进行宽高缩放就行了

如果高度不是1080就该成需要的宽高就行

3.监听

window.addEventListener("resize",this.myResize) //添加
window.removeEventListener("resize",this.myResize) //删除
myResize(){
    let scalX = Number(window.innerWidth)/1920
	let scalY = Number(window.innerHeight)/1080
	document.getElementById("transMain").style.transform = "scale("+scalX+","+scalY+")"
}

你可能感兴趣的:(css,js,html,前端,javascript,html)