广告位联系
返回顶部
分享到

swiper4实现移动端导航切换的方法

JavaScript 来源:互联网搜集 作者:秩名 发布时间:2019-09-08 15:15:12 人浏览
摘要

具体效果如下: 首先导入 link rel=stylesheet href=css/swiper.min.css script src=js/jquery-1.10.1.min.js/scriptscript src=js/swiper.min.js/script(这里用的是Swiper 4.0.7版本) 在写入html内容 div id=header div class=head-top h3商品分类/h3 /div

具体效果如下:


 
首先导入



<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-top">
 <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:#ccc;overflow:hidden;}
 img{width:100%;border:0;}
 li{list-style:none;}
 
 .head-top{height:36px;position:fixed;top:0;left:0;text-align:center;width:100%;line-height:36px;}
 .swiper-containee{max-width:640px;}
 #nav{margin-top: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-top">
  <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>



版权声明 : 本文内容来源于互联网或用户自行发布贡献,该文观点仅代表原作者本人。本站仅提供信息存储空间服务和不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权, 违法违规的内容, 请发送邮件至2530232025#qq.cn(#换@)举报,一经查实,本站将立刻删除。
原文链接 : https://blog.csdn.net/weixin_42861240/article/details/81874275
相关文章
  • 本站所有内容来源于互联网或用户自行发布,本站仅提供信息存储空间服务,不拥有版权,不承担法律责任。如有侵犯您的权益,请您联系站长处理!
  • Copyright © 2017-2022 F11.CN All Rights Reserved. F11站长开发者网 版权所有 | 苏ICP备2022031554号-1 | 51LA统计