swiper是一款免費以及輕量級輪播滑動的js框架,適用於PC端跟移動端,官方地址:(https://www.swiper.com.cn/)css
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <title>Swiper.js簡單快速實現輪播滑動(兼容PC端、移動端)</title>
5 <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0,minimum-scale=1.0,user-scalable=0" />
6
7 <!-- 下面加載Swiper的css跟js文件。能夠將文件下載到項目中加載,也能夠經過cdn遠程加載,這裏使用的是cdn遠程加載 -->
8 <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.4.2/css/swiper.css"><!-- 加載Swiper的css文件 -->
9 <script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.4.2/js/swiper.js"></script><!-- 加載Swiper的js文件 -->
10 </head>
11 <body>
12 <div style="height: 200px;" class="swiper-container">
13 <div class="swiper-wrapper">
14 <!-- 下面是4個不一樣顏色的輪播滑塊 -->
15 <div style="background-color: red;" class="swiper-slide">slider1</div>
16 <div style="background-color: green;" class="swiper-slide">slider2</div>
17 <div style="background-color: yellow;" class="swiper-slide">slider3</div>
18 <div style="background-color: blue;" class="swiper-slide">slider4</div>
19 </div>
20 <div class="swiper-button-prev"></div><!--左箭頭-->
21 <div class="swiper-button-next"></div><!--右箭頭-->
22 <div class="swiper-pagination"></div><!--分頁器(就是4個點點)-->
23 </div>
24 </body>
25 </html>
26 <script>
27 var mySwiper = new Swiper('.swiper-container', {//初始化Swiper
28 autoplay: {//自動切換
29 delay: 3000,
30 stopOnLastSlide: false,
31 disableOnInteraction: false,
32 },
33 navigation: {//前進後退
34 nextEl: '.swiper-button-next',
35 prevEl: '.swiper-button-prev',
36 },
37 pagination: {//分頁器
38 el: '.swiper-pagination',
39 clickable :true,
40 },
41 loop : true,//循環
42 })
43 </script>
複製代碼
<head>
<!-- 這裏使用的是cdn遠程加載Swiper4的版本 -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.4.2/css/swiper.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.4.2/js/swiper.js"></script>
</head>
複製代碼
swiper4文件下載地址:html
https://www.swiper.com.cn/download/index.htmlweb
swiper4遠程CDN加載地址:ajax
https://www.swiper.com.cn/cdn/index.htmlapi
<div class="swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide">slider1</div>
<div class="swiper-slide">slider2</div>
<div class="swiper-slide">slider3</div>
<div class="swiper-slide">slider4</div>
</div>
</div>
複製代碼
上面的HTML內容是固定模板,若是須要添加左右箭頭或者分頁器等其餘組件能夠參考swiper4的官方文檔:https://www.swiper.com.cn/api/index.html瀏覽器
<script>
var mySwiper = new Swiper('.swiper-container', {
autoplay:true,//等同於如下設置
/*autoplay: {
delay: 3000,
stopOnLastSlide: false,
disableOnInteraction: true,
},*/
});
</script>
複製代碼
在js中若是須要添加其餘配置選項能夠參考swiper4的官方文檔:bash
https://www.swiper.com.cn/api/index.htmlapp
在實際的web開發中,老是須要兼容PC跟移動端,若是使用原生js本身寫的話,不只耗時,可能兼容性、穩定性也很差,特別是移動端的touch觸控事件。使用swiper便不用考慮到兼容這些事,只要熟悉它的API,即可快速實現手機,電腦網頁大部分滑動,焦點圖、tab、觸摸導航等功能框架
以爲文章不錯的話,給我個關注哇,點個讚唄!iphone
若是對文章有疑問或想技術交流,可關注公衆號【GitWeb】與我一塊兒探索學習!