1. 程式人生 > >關於適配所有移動端螢幕且到固定寬度時居中的寫法

關於適配所有移動端螢幕且到固定寬度時居中的寫法

<!DOCTYPE html>
<html>
 <head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1">
  <title>圖解風雲</title>
  <style>
   *{
    margin: 0;
    padding: 0;
   }
   html,body{
    position: relative;
    background: #EEE;
   }
   div{
    position: relative;
   }
   img{
    border: none;
    width: 100%;
    max-width: 750px;
   }
   .img1{ 
    position: absolute;
    left: 50%;
    margin-left: -375px;
   }
  </style>
 </head>
 <body>
  <div id='box'>
   <img src='img/tjdj.png' id='img'>
  </div>
  <script>      
        function getInsideArr(){   
   var w=window.innerWidth || document.documentElement.clientWidth ;
   document.getElementById("box").style.width=w+"px";
   if(w>750){
    document.getElementById("box").style.width=750+"px";
    var div = document.getElementById('box');
                div.setAttribute("class", "img1");
   };
   };   
   getInsideArr();
  </script>
 </body>
</html>