pc端实现 网页居中显示 且自适应

通过给最外层添加绝对定位和translate(-50%,-50%) scaleratio)来实现居中且适度缩放。

<!DOCTYPE html >
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <title>pc端实现 网页居中显示 且自适应</title>
    <style>
       *{margin: 0; padding: 0;}
        .evt-content{ width: 800px; height: 1000px; border: #ccc 2px solid; margin: 0 auto;}
    </style>
</head>
<body>
    
<div class="evt-content">
    <img src="https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png" alt="">
    <img src="https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png" alt="">
    <img src="https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png" alt="">
    
</div>




<script src="https://code.jquery.com/jquery-3.2.1.js" integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE=" crossorigin="anonymous"></script>
        
<script type="text/javascript">
    (function(){
         function createResponsiveStyle() {
            var style = document.createElement("style");
            style.type = "text/css";
            document.head.appendChild(style);

            return {
              setStyle: function (text) {
                style.innerHTML = "";
                if( style.styleSheet ){
                  style.styleSheet.cssText = text;
                } else {
                  style.innerHTML = text;
                }
              },
              getRespStyle: function(){
                var ratio = Math.max(0.65, Math.min(1, window.innerHeight/960));
                return ".evt-content{" +
                    "position:absolute;height:800px;left:50%;top:50%;" +
                    "-webkit-transform:translate(-50%,-50%) scale("+ratio+");" +
                    "transform:translate(-50%,-50%) scale("+ratio+");" +
                    "-webkit-transform-origin:50% 50%;" +
                    "transform-origin:50% 50%;" +
                  "}";
              }
            }
          }


          var respStyle = createResponsiveStyle();
          respStyle.setStyle(respStyle.getRespStyle());
          $(window).resize(function () {
            respStyle.setStyle(respStyle.getRespStyle());
          });

    })();
</script>




</body>
</html>

 

 

 

 

转载于:https://www.cnblogs.com/shimily/articles/6902135.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值