Swiper滑动Html5手机浏览器自适应

来源:互联网 发布:java客户端 api接口 编辑:程序博客网 时间:2024/04/29 03:13
 

Swiper滑动Html5手机浏览器自适应

分类: 代码片段 281人阅读 评论(0) 收藏 举报

手机网页能通过window.screen.height, width获取屏幕分辨率,于是可以通过分辨率比率来计算高度。

[html] view plaincopy
  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来做个手机全屏适配的滑动,全部代码如下

[html] view plaincopy
  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3. <head>  
  4.     <meta charset="utf-8">  
  5.     <meta name="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.     <link rel="stylesheet" href="swiper.min.css"/>  
  8.     <style type="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.     <div class="swiper-container" id="swiper">  
  28.         <div class="swiper-wrapper">  
  29.             <div class="swiper-slide" style="background:green;">Slide 1</div>  
  30.             <div class="swiper-slide" style="background:yellow;">Slide 2</div>  
  31.             <div class="swiper-slide" style="background:orange;">Slide 3</div>  
  32.         </div>  
  33.           
  34.         <div class="swiper-pagination"></div>  
  35.           
  36.     </div>  
  37.       
  38.     <script src="swiper.min.js"></script>  
  39.     <script>  
  40.         window.onload=function(){  
  41.             var swiper = document.getElementById("swiper");  
  42.             var scale = window.screen.height / window.screen.width;  
  43.             swiper.style.height = document.body.clientWidth * scale + "px";  
  44.         }  
  45.         var mySwiper = new Swiper('.swiper-container',{  
  46.             direction: 'horizontal',  
  47.             loop: false,  
  48.             pagination: '.swiper-pagination'  
  49.         });      
  50.     </script>  
  51. </body>  
  52. </html>  

转载自:http://blog.csdn.net/dyyaries/article/details/46442187

0 0