最近公司之前的数据大屏 进行一个UI的调整 但是我看之前的数据大屏在自适应方面做得并不好
所以在网上冲浪过后 选择了使用scale来解决数据大屏的适配

一 dom结构

<div id="container">
    <div id="box"></div>
</div>

二 dom样式

#container{
    width: 100vw;
    height:100vh;
    position: relative;
}
#box{
    width: 1632px;//设计图宽度
    height: 840px;//设计图高度
    left: 50%;
    top: 50%;
    margin-right: -50%;
    transform-origin: left top;
    position: absolute;
    display: flex;
    flex-direction: column;
}

三 通过scale控制大屏的放大与缩小

let box = document.getElementById('box')//数据大屏的父盒子
let container  = document.getElementById('container')//数据大屏的盒子
function getScale(w=1632,h=840){
    //w 设计图的宽  h 设计图的高
    //ww 数据大屏可以占有的宽与设计图的宽之比
    //wh 数据大屏可以占有的高与设计图的高之比
    const ww = container.getBoundingClientRect().width / w
    const wh = container.getBoundingClientRect().height / h
    console.log(ww,wh)
    return ww < wh ? ww : wh
}
console.log('scale',getScale())
box.style.transform = 'scale(' + getScale() + ') translate(-50%,-50%)'
window.addEventListener('resize',function (){
    box.style.transform = 'scale(' + getScale() + ') translate(-50%,-50%)'
})

有需要可以试试

Logo

永洪科技,致力于打造全球领先的数据技术厂商,具备从数据应用方案咨询、BI、AIGC智能分析、数字孪生、数据资产、数据治理、数据实施的端到端大数据价值服务能力。

更多推荐