前端scale(缩放)

很多的大屏适配都是使用的这种方案。
这种方案的原理就是根据宽高比例进行缩放。
1、根据宽度比率进行缩放
(宽度比率=网页当前宽度/设计稿宽度)

<script>
    // 设计稿:1920 * 1080
    // 1.设计稿尺寸
    let targetWidth = 1920;
    // 2.拿到当前设备(浏览器)的宽度
    // document.documentElement  获取html的宽度
    let currentWidth =
      document.documentElement.clientWidth || document.body.clientWidth;
    // 3.计算缩放比率(屏幕过宽,根据高度计算缩放比例)
    let scaleRatio = currentWidth / targetWidth; 
    // 4.开始缩放网页
    document.body.style = `transform: scale(${scaleRatio})`;
  </script>

上面这种根据宽度比例进行缩放的,针对1920 * 1080,3840 * 2160(4k)是没有问题的,但是在超宽屏的情况下还是存在只显示一半的问题。
分析原因:

我们的设计稿:
1920 * 1080 => 要适配 (1920*2=3840, 1080*2=2160, 4k屏) 3840 * 2160
也要适配=> ( 1920*4 = 7680 : 1080 * 2 = 2160) 7680 * 2160 

我们当前是根据宽度比率进行缩放的:

先设配3840 * 2160

scaleRatio = 3840 / 1920  = 2

根据这个缩放比率

我们的设计稿宽高都会被缩放两倍

1920 * 2 = 3840

1080 * 2 = 2160



设配7680 * 2160

scaleRatio = 7680 / 1920  =  4

根据这个宽度比例我们的设置稿宽高都会被缩放4倍

1920 * 4 = 7680

1080 * 4  = 4240 
这个原先的比例是 4 : 2,现在变成了 4 :4 ,这也是为什么我们只看到一半高度的原因。

2、动态计算
动态计算网页宽高比,决定是按照宽度的比例还是高度的比例进行缩放。

<script>
    // 设计稿:1920 * 1080
    // 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.计算缩放比率(屏幕过宽,根据高度计算缩放比例)
		// 若currentWidth是4k屏宽度 3840 除于 我们设计稿的宽度 1920  3840/1920 = 2
		// 这样页面就行进行2倍缩放
    let scaleRatio = currentWidth / targetWidth; // 参照宽度进行缩放(默认情况下)
		
    // 当前页面宽高比例,当页面越宽currentRatio值就越大
    let currentRatio = currentWidth / currentHeight;
		
		// 判断是根据宽度进行缩放,还是根据高度进行缩放
    if (currentRatio > targetRatio) {
      // 根据高度进行网页的缩放
      scaleRatio = currentHeight / targetHeight; // 参照高度进行缩放(屏幕很宽的情况下)
      document.body.style = `transform: scale(${scaleRatio}) translateX(-50%)`;
    } else {
      // 根据宽度进行网页的缩放
      document.body.style = `transform: scale(${scaleRatio})`;
    }
  </script>

完整demo代码:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      body {
        position: relative;
        width: 1920px;
        height: 1080px;
        border: 3px solid red;
        /* 设置缩放原点 */
        transform-origin: left top;
        box-sizing: border-box;
      }
      ul {
        display: flex;
        flex-direction: row;
        flex-wrap: wrap;
        width: 100%;
        height: 100%;
      }

      li {
        width: 33.333%;
        height: 50%;
        font-size: 30px;
        list-style: none;
        border: 3px solid green;
        box-sizing: border-box;
      }
    </style>
  </head>
  <body>
    <ul>
      <li>1</li>
      <li>2</li>
      <li>3</li>
      <li>4</li>
      <li>5</li>
      <li>6</li>
    </ul>
  </body>
  <script>
    // 设计稿: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})`;
    }
  </script>
</html>
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值