1. 程式人生 > >Swiper滑動Html5手機瀏覽器自適應

Swiper滑動Html5手機瀏覽器自適應

手機網頁能通過window.screen.height, width獲取螢幕解析度,於是可以通過解析度比率來計算高度。

  1. window.onload=function(){  
  2.     var swiper = document.getElementById("swiper");  
  3.     var scale = window.screen.height / window.screen.width;  
  4.     swiper.style.height = document.body.clientWidth * scale + "px";  
  5. }  

結合swiper來做個手機全屏適配的滑動,全部程式碼如下
  1. <!DOCTYPE html
    >
  2. <htmllang="en">
  3. <head>
  4.     <metacharset="utf-8">
  5.     <metaname="viewport"content="width=device-width,height=device-height,target-densitydpi=high-dpi,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
  6.     <title>swiper demo</title>
  7.     <
    linkrel="stylesheet"href="swiper.min.css"/>
  8.     <styletype="text/css">
  9.     body {  
  10.         font-family: Helvetica Neue, Helvetica, Arial, sans-serif;  
  11.         font-size: 14px;  
  12.         color:#000;  
  13.         margin: 0;  
  14.         padding: 0;  
  15.     }  
  16.     .swiper-container {  
  17.         margin: 0 auto;  
  18.     }  
  19.     .swiper-slide {  
  20.         text-align: center;  
  21.         font-size: 18px;  
  22.         background: #fff;  
  23.     }  
  24.     </style>
  25. </head>
  26. <body>
  27.     <divclass="swiper-container"id="swiper">
  28.         <divclass="swiper-wrapper">
  29.             <divclass="swiper-slide"style="background:green;">Slide 1</div>
  30.             <divclass="swiper-slide"style="background:yellow;">Slide 2</div>
  31.             <divclass="swiper-slide"style="background:orange;">Slide 3</div>
  32.         </div>
  33.         <divclass="swiper-pagination"></div>
  34.     </div>
  35.     <scriptsrc="swiper.min.js"></script>
  36.     <script>
  37.         window.onload=function(){  
  38.             var swiper = document.getElementById("swiper");  
  39.             var scale = window.screen.height / window.screen.width;  
  40.             swiper.style.height = document.body.clientWidth * scale + "px";  
  41.         }  
  42.         var mySwiper = new Swiper('.swiper-container',{  
  43.             direction: 'horizontal',  
  44.             loop: false,  
  45.             pagination: '.swiper-pagination'  
  46.         });      
  47.     </script>
  48. </body>
  49. </html>

轉載自:http://blog.csdn.net/dyyaries/article/details/46442187