h5使用swiper外掛實現滑動元件和切換標籤的同步
阿新 • • 發佈:2019-01-04
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no"> <title>投資計劃</title> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.0.2/css/swiper.min.css"> <style type="text/css"> /*body預設鋪滿整個螢幕*/ body{ position: absolute; bottom: 0; top: 0; left: 0; right: 0; } #nav{ height: 2.8125rem; background-color: white; color: gray; display: flex; /* @include flex(center, center);*/ } #nav .sub-item{ text-align: center; -webkit-flex: 1; flex: 1; line-height: 2.8125rem; width: 100%; height: 100%; border-bottom: none; } .selected{ border-bottom: 0.0625rem solid red!important; color: red!important; } #main{ min-height: 100%; } </style> </head> <body> <nav class="text-title1" id="nav"> <div class="sub-item selected" data-type="new-peo" data-num="0">回款中</div> <div class="sub-item" data-type="earning" data-num="1">已回款</div> <div class="sub-item" data-type="earning" data-num="2">笑嘻嘻</div> </nav> <div class="swiper-container" id="main"> <div class="swiper-wrapper"> <div class="swiper-slide"> 1 </div> <div class="swiper-slide"> 2 </div> <div class="swiper-slide"> 3 </div> </div> </div> <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.0.2/js/swiper.min.js"></script> <script type="text/javascript"> $(function(){ /*滑動切換*/ var swiper = new Swiper('.swiper-container', { on:{ slideNextTransitionEnd: function(swiper){ $('.sub-item')[this.previousIndex].classList.remove('selected') $('.sub-item')[this.activeIndex].classList.add('selected') }, slidePrevTransitionEnd: function(swiper){ $('.sub-item')[this.activeIndex+1].classList.remove('selected') $('.sub-item')[this.activeIndex].classList.add('selected') } } }); /*點選選單切換*/ $('#nav .sub-item').click(function(){ $('.selected').removeClass('selected'); $(this).addClass('selected'); var num = $(this).data('num'); swiper.slideTo(num, 500, false); }); }); </script>