Swiper滑動Html5手機瀏覽器自適應
阿新 • • 發佈:2019-02-10
手機網頁能通過window.screen.height, width獲取螢幕解析度,於是可以通過解析度比率來計算高度。
- window.onload=function(){
- var swiper = document.getElementById("swiper");
- var scale = window.screen.height / window.screen.width;
- swiper.style.height = document.body.clientWidth * scale + "px";
- }
結合swiper來做個手機全屏適配的滑動,全部程式碼如下
-
<!DOCTYPE html
- <htmllang="en">
- <head>
- <metacharset="utf-8">
- <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"/>
- <title>swiper demo</title>
-
<
- <styletype="text/css">
- body {
- font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
- font-size: 14px;
- color:#000;
- margin: 0;
- padding: 0;
- }
- .swiper-container {
-
margin: 0 auto;
- }
- .swiper-slide {
- text-align: center;
- font-size: 18px;
- background: #fff;
- }
- </style>
- </head>
- <body>
- <divclass="swiper-container"id="swiper">
- <divclass="swiper-wrapper">
- <divclass="swiper-slide"style="background:green;">Slide 1</div>
- <divclass="swiper-slide"style="background:yellow;">Slide 2</div>
- <divclass="swiper-slide"style="background:orange;">Slide 3</div>
- </div>
- <divclass="swiper-pagination"></div>
- </div>
- <scriptsrc="swiper.min.js"></script>
- <script>
- window.onload=function(){
- var swiper = document.getElementById("swiper");
- var scale = window.screen.height / window.screen.width;
- swiper.style.height = document.body.clientWidth * scale + "px";
- }
- var mySwiper = new Swiper('.swiper-container',{
- direction: 'horizontal',
- loop: false,
- pagination: '.swiper-pagination'
- });
- </script>
- </body>
- </html>
轉載自:http://blog.csdn.net/dyyaries/article/details/46442187