前端大屏适配方案(scale方式)

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

  <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>
<!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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
前端屏适的代码可以参考以下示例,其中使用了CSS3的媒体查询和百分比布局来实现适: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>前端屏适示例</title> <style> /* 默认样式 */ * { margin: 0; padding: 0; } /* 大屏幕样式 */ @media screen and (min-width: 1024px) { /* 设置页面宽度为100% */ body { width: 100%; } /* 设置页面主体部分宽度为80% */ .main { width: 80%; margin: 0 auto; } /* 设置页面元素的字体大小为2em */ h1, p { font-size: 2em; } /* 设置图片宽度为50% */ img { width: 50%; } } /* 小屏幕样式 */ @media screen and (max-width: 1023px) { /* 设置页面宽度为100% */ body { width: 100%; } /* 设置页面主体部分宽度为90% */ .main { width: 90%; margin: 0 auto; } /* 设置页面元素的字体大小为1em */ h1, p { font-size: 1em; } /* 设置图片宽度为100% */ img { width: 100%; } } </style> </head> <body> <div class="main"> <h1>前端屏适示例</h1> <p>这是一个前端屏适的示例,通过使用CSS3的媒体查询和百分比布局来实现。</p> <img src="example.jpg" alt="示例图片"> </div> </body> </html> ``` 在上述代码中,通过设置 @media 查询来实现不同屏幕尺寸下的样式设置。在大屏幕下,设置了页面宽度为100%、页面主体部分宽度为80%、页面元素字体大小为2em、图片宽度为50%等样式。在小屏幕下,设置了页面宽度为100%、页面主体部分宽度为90%、页面元素字体大小为1em、图片宽度为100%等样式。通过这种方式,可以实现前端屏适
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值