swiper2.0和3.0的區別: 3.0支持部分PC端瀏覽器 3.0合併js,新增動畫功能animate.css 3.0繼續更新,2.7.6停止更新 html結構: <div class="swiper-container"> <div class="swiper-wrap"> <div clas ...
swiper2.0和3.0的區別:
3.0支持部分PC端瀏覽器
3.0合併js,新增動畫功能animate.css
3.0繼續更新,2.7.6停止更新
html結構:
<div class="swiper-container"> <div class="swiper-wrap"> <div class="swiper-slide"> </div> </div> </div>
插件及參數:
json格式數據配置
參數值為數字、字元串、函數
支持各種狀態的輪播回調,參數只有一個,就是當前輪播的對象
swiper下載:https://www.swiper.com.cn/download/index.html
(我下的最新版已經到swiper5了)
swiper水平滾動基本示例:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>index</title> <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no"> <link rel="stylesheet" href="lib/swiper.min.css"> <style> *{margin:0;padding:0;} html,body{width:100%;height:100%;} .swiper-container,.swiper-wrapper,.swiper-slide img{width:100%;} </style> </head> <body> <div class="swiper-container"> <div class="swiper-wrapper"> <div class="swiper-slide"><img src="image/003.png"></div> <div class="swiper-slide"><img src="image/002.png"></div> <div class="swiper-slide"><img src="image/001.png"></div> <div class="swiper-slide"><img src="image/004.png"></div> </div> <div class="swiper-pagination"></div> <div class="swiper-button-prev"></div> <div class="swiper-button-next"></div> </div> <script src="lib/swiper.min.js"></script> <script> var swiper=new Swiper(".swiper-container", { //direction: 'vertical', // 垂直切換選項 loop: true, // 迴圈模式選項 // 如果需要分頁器 pagination: { el: '.swiper-pagination', }, // 如果需要前進後退按鈕 navigation: { nextEl: '.swiper-button-next', prevEl: '.swiper-button-prev', }, // 如果需要滾動條 // scrollbar: { // el: '.swiper-scrollbar', // }, autoplay: { delay: 2500, disableOnInteraction: false, } }); </script> </body> </html>
swiper垂直滾動基本示例:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>index</title> <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no"> <link rel="stylesheet" href="lib/swiper.min.css"> <style> *{margin:0;padding:0;} html,body{width:100%;height:100%;} .swiper-container,.swiper-wrapper,.swiper-slide img{width:100%;} </style> </head> <body> <div class="swiper-container"> <div class="swiper-wrapper"> <div class="swiper-slide"><img src="image/003.png"></div> <div class="swiper-slide"><img src="image/002.png"></div> <div class="swiper-slide"><img src="image/001.png"></div> <div class="swiper-slide"><img src="image/004.png"></div> </div> </div> <script src="lib/swiper.min.js"></script> <script> var swiper=new Swiper(".swiper-container", { direction: 'vertical', // 垂直切換選項 loop: true, // 迴圈模式選項 }); </script> </body> </html>
3D效果翻轉
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>index</title> <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no"> <link rel="stylesheet" href="lib/swiper.min.css"> <style> *{margin:0;padding:0;} html,body{width:100%;height:100%;} .swiper-container,.swiper-wrapper,.swiper-slide img{width:100%;} </style> </head> <body> <div class="swiper-container"> <div class="swiper-wrapper"> <div class="swiper-slide"><img src="image/003.png"></div> <div class="swiper-slide"><img src="image/002.png"></div> <div class="swiper-slide"><img src="image/001.png"></div> <div class="swiper-slide"><img src="image/004.png"></div> </div> <div class="swiper-pagination"></div> </div> <script src="lib/swiper.min.js"></script> <script> var swiper = new Swiper('.swiper-container', { effect: 'coverflow', grabCursor: true, centeredSlides: true, slidesPerView: 'auto', coverflowEffect: { rotate: 50, stretch: 0, depth: 100, modifier: 1, slideShadows : true, }, pagination: { el: '.swiper-pagination', }, }); </script> </body> </html>
進度條效果 scrollbar
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>index</title> <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no"> <link rel="stylesheet" href="lib/swiper.min.css"> <style> *{margin:0;padding:0;} html,body{width:100%;height:100%;} .swiper-container,.swiper-wrapper,.swiper-slide img{width:100%;} </style> </head> <body> <div class="swiper-container"> <div class="swiper-wrapper"> <div class="swiper-slide"><img src="image/003.png"></div> <div class="swiper-slide"><img src="image/002.png"></div> <div class="swiper-slide"><img src="image/001.png"></div> <div class="swiper-slide"><img src="image/004.png"></div> </div> <div class="swiper-scrollbar"></div> </div> <script src="lib/swiper.min.js"></script> <script> var swiper = new Swiper('.swiper-container', { scrollbar: { el: '.swiper-scrollbar', hide: true, }, }); </script> </body> </html>
使用swiper完成tab效果
查看官方API文檔--events事件--sliderMove
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>index</title> <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no"> <link rel="stylesheet" href="lib/swiper.min.css"> <style> *{margin:0;padding:0;box-sizing:border-box;} html,body{width:100%;height:100%;} a{text-decoration:none;} .swiper-container,.swiper-wrapper,.swiper-slide img{width:100%;} .tabs{position: fixed;left:0;bottom:0;height:45px;line-height:45px;background:#fff;color:#333;z-index:999;width:100%;border-top:1px solid #ccc;font-size:0;} .tabs a{display: inline-block;width:25%;text-align: center;color:#333;font-size:14px;} .tabs a.active{background:#eee;color:red;} a{-webkit-tap-highlight-color: rgba(255, 255, 255, 0);-webkit-user-select: none;-moz-user-focus: none;-moz-user-select: none;} </style> </head> <body> <div class="swiper-container"> <div class="swiper-wrapper"> <div class="swiper-slide"><img src="image/003.png"></div> <div class="swiper-slide"><img src="image/002.png"></div> <div class="swiper-slide"><img src="image/001.png"></div> <div class="swiper-slide"><img src="image/004.png"></div> </div> <div class="tabs"> <a href="#" class="active">課程</a> <a href="#">下載</a> <a href="#">發現</a> <a href="#">我的</a> </div> </div> <script src="lib/jquery-1.11.3.min.js"></script> <script src="lib/swiper.min.js"></script> <script> var swiper = new Swiper('.swiper-container', { on:{ //動畫結束時獲取當前索引 slideChangeTransitionStart: function(){ var index=this.activeIndex; tabActive(index);// 切換時更新tab樣式 }, }, }); //點擊tab切換輪播圖 $(".tabs a").on("click",function(e){ e.preventDefault();//阻止預設事件 var index=$(this).index();//獲取當前點擊的索引 tabActive(index);// 切換時更新tab樣式 swiper.slideTo(index);//切換對應的輪播圖 }); //更新tab樣式 function tabActive(index){ $(".tabs a