swiper4实现移动端导航切换
网络编程 2021-07-04 15:01www.168986.cn编程入门
这篇文章主要为大家详细介绍了swiper4实现移动端导航切换,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了swiper4实现移动端导航切换的具体代码,供大家参考,具体内容如下
导入
<link rel="stylesheet" href="css/swiper.min.css" > <script src="js/jquery-1.10.1.min.js"></script> <script src="js/swiper.min.js"></script>(这里用的是Swiper 4.0.7版本)
在写入html内容
<div id=header> <div class="head-"> <h3>商品分类</h3> </div> <div id="nav" class="swiper-container"> <ul class="swiper-wrapper"> <li class="swiper-slide active-nav"><a href="javascript:;" >手机</a></li> <li class="swiper-slide"><a href="javascript:;">厨具</a></li> <li class="swiper-slide"><a href="javascript:;">数码</a></li> <li class="swiper-slide"><a href="javascript:;">家纺</a></li> <li class="swiper-slide"><a href="javascript:;">生鲜</a></li> <li class="swiper-slide"><a href="javascript:;">家用电器</a></li> <li class="swiper-slide"><a href="javascript:;">食品饮料</a></li> <li class="swiper-slide"><a href="javascript:;">电脑/办公</a></li> <li class="swiper-slide"><a href="javascript:;">家用日用</a></li> </ul> </div> </div> <div id="page" class="swiper-container" id="page"> <div class="swiper-wrapper"> <div class="swiper-slide slidepage">手机</div> <div class="swiper-slide slidepage">厨具</div> <div class="swiper-slide slidepage">数码</div> <div class="swiper-slide slidepage">家纺</div> <div class="swiper-slide slidepage">生鲜</div> <div class="swiper-slide slidepage">家用电器</div> <div class="swiper-slide slidepage">食品饮料</div> <div class="swiper-slide slidepage">电脑/办公</div> <div class="swiper-slide slidepage">家用日用</div> </div> </div>
调用swiper
<script> var myNav = new Swiper('#nav', { spaceBetween: 10, slidesPerView : 3, watchSlidesProgress : true, watchSlidesVisibility : true, on:{ tap: function(){ myPage.slideTo( myNav.clickedIndex) } } }) var myPage = new Swiper('#page',{ on:{ slideChangeTransitionStart: function(){ updateNavPosition() }} }) function updateNavPosition(){ $('#nav .active-nav').removeClass('active-nav'); var activeNav = $('#nav .swiper-slide').eq(myPage.activeIndex).addClass('active-nav'); if (!activeNav.hasClass('swiper-slide-visible')) { console.log(1); if (activeNav.index()>myNav.activeIndex) { console.log(2); var thumbsPerNav = Math.floor(myNav.width/activeNav.width())-1 myNav.slideTo(activeNav.index()-thumbsPerNav) } else { console.log(3); myNav.slideTo(activeNav.index()) } } } </script>
全部代码如下
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>商品分类</title> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" /> <link rel="stylesheet" href="css/swiper.min.css" > <script src="js/jquery-1.10.1.min.js"></script> <script src="js/swiper.min.js"></script> <style> {padding:0;margin:0;font-size:20px;color:#333;} html{background:#fff;} a{text-decoration:none;} body{max-width:640px;margin:0 auto;position:relative;background:#c;overflow:hidden;} img{width:100%;border:0;} li{list-style:none;} .head-{height:36px;position:fixed;:0;left:0;text-align:center;width:100%;line-height:36px;} .swiper-containee{max-width:640px;} #nav{margin-:36px;border-bottom:1px solid #999;padding:10px;} #nav li{text-align:center;} .active-nav{ color:#fff; background:#ddd !important; } .active-nav a{ color:#fff; background:#ddd !important; } #page .swiper-slide{height:6rem;} </style> </head> <body> <div id=header> <div class="head-"> <h3>商品分类</h3> </div> <div id="nav" class="swiper-container"> <ul class="swiper-wrapper"> <li class="swiper-slide"><a href="javascript:;" >手机</a></li> <li class="swiper-slide"><a href="javascript:;" >厨具</a></li> <li class="swiper-slide"><a href="javascript:;" >数码</a></li> <li class="swiper-slide"><a href="javascript:;" >家纺</a></li> <li class="swiper-slide"><a href="javascript:;" >生鲜</a></li> <li class="swiper-slide"><a href="javascript:;" >家用电器</a></li> <li class="swiper-slide"><a href="javascript:;" >食品饮料</a></li> <li class="swiper-slide"><a href="javascript:;" >电脑/办公</a></li> <li class="swiper-slide"><a href="javascript:;" >家用日用</a></li> </ul> </div> </div> <div id="page" class="swiper-container" id="page"> <div class="swiper-wrapper"> <div class="swiper-slide slidepage">手机</div> <div class="swiper-slide slidepage">厨具</div> <div class="swiper-slide slidepage">数码</div> <div class="swiper-slide slidepage">家纺</div> <div class="swiper-slide slidepage">生鲜</div> <div class="swiper-slide slidepage">家用电器</div> <div class="swiper-slide slidepage">食品饮料</div> <div class="swiper-slide slidepage">电脑/办公</div> <div class="swiper-slide slidepage">家用日用</div> </div> </div> <script> var myNav = new Swiper('#nav', { spaceBetween: 10, slidesPerView : 3, watchSlidesProgress : true, watchSlidesVisibility : true, on:{ tap: function(){ myPage.slideTo( myNav.clickedIndex) } } }) var myPage = new Swiper('#page',{ on:{ slideChangeTransitionStart: function(){ updateNavPosition() }} }) function updateNavPosition(){ $('#nav .active-nav').removeClass('active-nav'); var activeNav = $('#nav .swiper-slide').eq(myPage.activeIndex).addClass('active-nav'); if (!activeNav.hasClass('swiper-slide-visible')) { console.log(1); if (activeNav.index()>myNav.activeIndex) { console.log(2); var thumbsPerNav = Math.floor(myNav.width/activeNav.width())-1 myNav.slideTo(activeNav.index()-thumbsPerNav) } else { console.log(3); myNav.slideTo(activeNav.index()) } } } </script> </body> </html>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持狼蚁SEO。
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程