swiper4实现移动端导航切换

本文实例为大家分享了swiper4实现移动端导航切换的具体代码,供大家参考,具体内容如下

首先导入

<link rel="stylesheet" href="css/swiper.min.css" rel="external nofollow" rel="external nofollow" >
<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:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >手机</a></li>
      <li class="swiper-slide"><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >厨具</a></li>
      <li class="swiper-slide"><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >数码</a></li>
      <li class="swiper-slide"><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >家纺</a></li>
      <li class="swiper-slide"><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >生鲜</a></li>
      <li class="swiper-slide"><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >家用电器</a></li>
      <li class="swiper-slide"><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >食品饮料</a></li>
      <li class="swiper-slide"><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >电脑/办公</a></li>
      <li class="swiper-slide"><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >家用日用</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" rel="external nofollow" rel="external nofollow" >
  <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 active-nav"><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >手机</a></li>
        <li class="swiper-slide"><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >厨具</a></li>
        <li class="swiper-slide"><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >数码</a></li>
        <li class="swiper-slide"><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >家纺</a></li>
        <li class="swiper-slide"><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >生鲜</a></li>
        <li class="swiper-slide"><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >家用电器</a></li>
        <li class="swiper-slide"><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >食品饮料</a></li>
        <li class="swiper-slide"><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >电脑/办公</a></li>
        <li class="swiper-slide"><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >家用日用</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>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • 移动端触摸滑动插件swiper使用方法详解

    Swiper是移动端的一款非常强大的触摸滑动插件,下面代码只展示一些常用的配置,具体可以查看官网api <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <link rel="stylesheet" href="swiper.min.cs

  • 移动端滑动切换组件封装 vue-swiper-router实例详解

    具体代码如下所述: <strong>组件部分</strong> <template> <div class="main"> <div class="page-tab"> <div :class="nowPath == item.path ? 'tab-item tab-item_active' : 'tab-item'" v-for='(item, index) in tabLis

  • Swiper.js实现移动端元素左右滑动

    swiper.js也是一个很好用的移动端的js库. 需要引入它的css文件和js文件.用法很简单,加入相应的类即可,看一下Demo,很容易看懂. 我的例子如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Swiper demo</title> <meta name="viewport

  • Swiper 4.x 使用方法(移动端网站的内容触摸滑动)

    Swiper常用于移动端网站的内容触摸滑动 Swiper是纯javascript打造的滑动特效插件,面向手机.平板电脑等移动终端. Swiper能实现触屏焦点图.触屏Tab切换.触屏多图切换等常用效果. Swiper开源.免费.稳定.使用简单.功能强大,是架构移动终端网站的重要选择! 1.首先加载插件,需要用到的文件有swiper.min.js和swiper.min.css文件.可下载Swiper文件或使用CDN. <!DOCTYPE html> <html> <head&g

  • swiper4实现移动端导航切换

    本文实例为大家分享了swiper4实现移动端导航切换的具体代码,供大家参考,具体内容如下 首先导入 <link rel="stylesheet" href="css/swiper.min.css" rel="external nofollow" rel="external nofollow" > <script src="js/jquery-1.10.1.min.js"></s

  • swiper4实现移动端导航栏tab滑动切换

    swiper4移动端导航栏tab滑动切换效果,供大家参考,具体内容如下 效果如图: 首先引入swiper的css和js文件 官网下载地址 <link href="swiper.min.css" rel="stylesheet"> <script src="swiper.min.js"></script> html结构部分 <body> <div class="tab">

  • Bootstrap的Carousel配合dropload.js实现移动端滑动切换图片

    轮播图可以通过点击按钮来跳到上一张或者下一张,在移动端我们常用的就是通过滑动来实现一些时间,那么如何实现呢. 以下是通过bootstrap的Carousel和dropload.js(移动端上下拉动刷新插件)的实现方法. HTML部分 <div id="goods_lunbo" style="width:200%;overflow:hidden;margin-left:-50%;"> <div id="goods_lunbo2"

  • vue+Vue Router多级侧导航切换路由(页面)的实现代码

    当当当当当~我又来了. 在项目里经常会遇到侧导航切换页面的功能. 如果我们将侧导航做成公共组件,来调用的话,就会在每一个页面都引用该组件,在后期维护的时候比较麻烦,比如改参数. 所以此文将侧导航做成父页面组件,将切换的页面做成子页面,这样只需调用一次即可.大大减少了后期维护的麻烦 涉及功能点 侧导航支持多级 Vue Router的使用方法( 官方文档 ) 子父组件的写法 样式:elementUI 效果图 实现 --- 目录结构 --- Vue Router的使用方法 首先安装 npm insta

  • vue使用vuex实现首页导航切换不同路由的方法

    vue实现首页导航切换不同路由的方式(二)[使用vuex实现的],具体代码如下所示: <nav> <!-- 导航栏 --> <div class="indexNavOut"> <div class="indexNav"> <ul class="navLi"> <li @click="checkNav()" style="width: 130px;&qu

  • 使用JS实现导航切换时高亮显示的示例讲解

    index.html代码内容 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>导航高亮显示</title> <style type="text/css"> body{ font-size:20px; } .nav{ list-style-type: none; margin:0; padding:0; } .cl

  • Vue实现移动端页面切换效果【推荐】

    在子页面把整个页面做绝对定位,覆盖整个屏幕,子父页面将 router-view 用  transition 套起来,并加上过渡动画就可以啦. 代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, init

  • vue.js实现左边导航切换右边内容

    本文实例为大家分享了vue.js左边导航切换右边内容的具体代码,供大家参考,具体内容如下 <template> <div class="layout-container"> <y-header> <div slot="nav"></div> </y-header> <div class="w"> <div class="content"&

  • vue-router之实现导航切换过渡动画效果

    过渡动效 提供了transition的封装组件,添加过渡动画,通过添加或删除css类名来实现. 过渡的css类名: v-enter 进入过渡的开始状态 v-enter-active 进入活动状态 v-enter-to 进入的结束状态 v-leave 离开过渡的开始状态 v-leave-active 离开活动状态 v-leave-to 离开结束状态 过渡模式: in-out 先进后出 out-in 先出后进 用法: 做一个淡隐淡出效果 把想要运动的元素放到<transition></tra

随机推荐