适配不同分辨率的另一种思路:页面缩放transform:scale(a,b) translate(x,y)

效果

这种方法的思路是把整个页面等比例缩放偏移,上下或者左右多出来的部分留白,所以不会在所有分辨率都适配的很完美,故而,有条件的还是用其他更完美的方法吧.

 

效果图-左右留白

效果图-上下留白

 

这种方法是页面的整体缩放,里面的所有内容都会缩放,包括字体,图片等,所以排版布局不会变形.

代码

html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link rel="shortcut icon" href="./img/logo.ico" type="image/x-icon" />
  <link rel="stylesheet" href="./css/index.css">
  <title>数据资产展示页面</title>
</head>

<body>
  <div id="container">
...
  </div>
</body>
</html>

css

html {
  padding: 0;
  margin: 0;
}
body{
  overflow: hidden;
  background: #ccc;
  height: 100vh;
}
#container {
  margin: 0 auto;
  box-sizing: border-box;
  background: url(../img/bj.jpg) no-repeat;
  background-size: 100% 100%;
  overflow: hidden;
  transform-origin: left top;
  transition: transform 0.2s
}

js

  screenScale($('#container'));
  function screenScale(element) {
    let width = '1920';
    let height = '1080';
    let offsetWidth = window.innerWidth;
    let offsetHeight = window.innerHeight;
    let top = 0;
    let left = 0;
    let scaleX = offsetWidth / width;
    let scaleY = offsetHeight / height;
    let scale = Math.min(scaleX, scaleY);
    top = (offsetHeight - height * scale) / 2;
    left = (offsetWidth - width * scale) / 2;
    //核心代码
    const transform = `translate(${left}px, ${top}px) scale(${scale})`;
    element.width(width);
    element.height(height);
    element.css({'transform': transform});
  }
  window.onresize = function () {
    screenScale($('#container'));
  }



作者:请叫我吉先生
链接:https://www.jianshu.com/p/616d6e42da23
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值