jquery仿百度百科底部浮动导航特效


Posted in Javascript onAugust 08, 2015

jquery类似百度百科底部导航预览,代码很简单,这里就不多废话了。

效果图:

jquery仿百度百科底部浮动导航特效

CSS 

* {
      margin: 0;
      padding: 0
    }

    .wrap {
      width: 1000px;
      overflow: hidden;
      margin: 0 auto;
    }

    .content {
      width: 780px;
      float: left;
    }

    .slide {
      width: 200px;
      float: right;
    }

HTML

<div class="wrap">
<div class="content">
  <h2 class="headline-1">
    <a class="anchor-1" name="1"></a>
    <span class="headline-1-index">1</span>
    <span class="headline-content">part1</span>
  </h2>

  <div class="para">
    content1,content1
    <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
  </div>
  <h2 class="headline-1">
    <a class="anchor-1" name="2"></a>
    <span class="headline-2-index">2</span>
    <span class="headline-content">part2</span>
  </h2>

  <div class="para">
    content2,content2
    <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
  </div>
  <h2 class="headline-2">
    <a class="anchor-2" name="2-1"></a>
    <!--<span class="headline-2-index">2-1</span>-->
    <span class="headline-content">part2-1</span>
  </h2>

  <div class="para">
    content2-1,content2-1
    <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
  </div>
  <h2 class="headline-2">
    <a class="anchor-2" name="2-2"></a>
    <!--<span class="headline-1-index">2-2</span>-->
    <span class="headline-content">part2-2</span>
  </h2>

  <div class="para">
    content2-2,content2-2
    <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
  </div>
  <h2 class="headline-1">
    <a class="anchor-1" name="3"></a>
    <span class="headline-1-index">3</span>
    <span class="headline-content">part3</span>
  </h2>

  <div class="para3">
    content,content
    <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
  </div>
  <h2 class="headline-1">
    <a class="anchor-1" name="4"></a>
    <span class="headline-1-index">4</span>
    <span class="headline-content">part4</span>
  </h2>

  <div class="para4">
    content,content
    <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
  </div>
  <h2 class="headline-1">
    <a class="anchor-1" name="5"></a>
    <span class="headline-1-index">5</span>
    <span class="headline-content">part5</span>
  </h2>

  <div class="para">
    content5,content5
    <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
  </div>
  <h2 class="headline-1">
    <a class="anchor-1" name="6"></a>
    <span class="headline-1-index">6</span>
    <span class="headline-content">part6</span>
  </h2>

  <div class="para">
    content6,content6
    <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
  </div>
  <h2 class="headline-1">
    <a class="anchor-1" name="7"></a>
    <span class="headline-1-index">7</span>
    <span class="headline-content">part7</span>
  </h2>

  <div class="para">
    content7,content7
    <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
  </div>
  <h2 class="headline-1">
    <a class="anchor-1" name="8"></a>
    <span class="headline-1-index">8</span>
    <span class="headline-content">part8</span>
  </h2>

  <div class="para">
    content8,content8
    <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
  </div>
  <h2 class="headline-1">
    <a class="anchor-1" name="9"></a>
    <span class="headline-1-index">9</span>
    <span class="headline-content">part9</span>
  </h2>

  <div class="para">
    content9,content9
    <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
  </div>
  <h2 class="headline-1">
    <a class="anchor-1" name="10"></a>
    <span class="headline-1-index">10</span>
    <span class="headline-content">part10</span>
  </h2>

  <div class="para">
    content10,content10
    <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
  </div>
  <h2 class="headline-1">
    <a class="anchor-1" name="11"></a>
    <span class="headline-1-index">11</span>
    <span class="headline-content">part11</span>
  </h2>

  <div class="para">
    content11,content11
    <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
  </div>
  <h2 class="headline-1">
    <a class="anchor-1" name="12"></a>
    <span class="headline-1-index">12</span>
    <span class="headline-content">part12</span>
  </h2>

  <div class="para">
    content12,content12
    <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
  </div>
  <h2 class="headline-1">
    <a class="anchor-1" name="13"></a>
    <span class="headline-1-index">13</span>
    <span class="headline-content">part13</span>
  </h2>

  <div class="para">
    content13,content13
    <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
  </div>
  <h2 class="headline-1">
    <a class="anchor-1" name="14"></a>
    <span class="headline-1-index">14</span>
    <span class="headline-content">part14</span>
  </h2>

  <div class="para">
    content14,content14
    <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
  </div>
  <h2 class="headline-1">
    <a class="anchor-1" name="15"></a>
    <span class="headline-1-index">15</span>
    <span class="headline-content">part15</span>
  </h2>

  <div class="para">
    content15,content15
    <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
    <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
    bottom
  </div>
</div>
<div class="slide" style="">
  <div id="sideToolbar" style="position: fixed; bottom: 0">
    <div id="sideCatalog" class="sideCatalogBg" style="visibility: visible;">
      <div id="sideCatalog-sidebar">
        <div class="sideCatalog-sidebar-top"></div>
        <div class="sideCatalog-sidebar-bottom"></div>
      </div>
      <div id="sideCatalog-updown" style="visibility: visible;">
        <div id="sideCatalog-up" class="sideCatalog-up-disable" title="向上翻页"></div>
        <div id="sideCatalog-down" class="sideCatalog-down-enable" title="向下翻页"></div>
      </div>
      <div id="sideCatalog-catalog">
        <dl style="width:175px;zoom:1">
          <!--
          <dd id="sideToolbar-item-0-1" class="sideCatalog-item2 heightlight">
            <span class="sideCatalog-index1">1</span>
            <a class="nslog:1026" onclick="return false;" title="part1" href="#1">part1</a>
            <span class="sideCatalog-dot"></span>
          </dd>
          <dd id="sideToolbar-item-0-2" class="sideCatalog-item1">
            <span class="sideCatalog-index1">2</span>
            <a class="nslog:1026" onclick="return false;" title="part2" href="#2">part2</a>
            <span class="sideCatalog-dot"></span>
          </dd>
          <dd id="sideToolbar-item-0-2-1" class="sideCatalog-item2">
            <span class="sideCatalog-index1">2-1</span>
            <a class="nslog:1026" onclick="return false;" title="part2" href="#2-1">part2-1</a>
            <span class="sideCatalog-dot"></span>
          </dd>
          -->
        </dl>
      </div>
    </div>
    <a id="sideCatalogBtn" class="" style="visibility: visible;" href="javascript:void(0);"></a>
    <a id="sideToolbar-up" style="visibility: visible;" title="返回顶部" href="javascript:void(0)"></a>
  </div>
</div>
</div>

JS

<script>
  var allEle = $(':header[class*="headline"]');
  var headLen = allEle.length;
  var ddArr = [];
  //根据页面内容生成slide导航;
  allEle.each(function(i){
    var sideIndex,
      sideName,
      ddId,
      highlight='',
      ddClass,
      sideAnchor;
    sideName = $(this).find('.headline-content').text();
    if($(this).hasClass('headline-1')){
      sideAnchor = sideIndex = $(this).find('.anchor-1').eq(0).attr('name');
      ddClass = 'sideCatalog-item1';
    }else{
      sideAnchor = $(this).find('.anchor-2').eq(0).attr('name');
      sideIndex = sideAnchor.replace('-','.');
      ddClass = 'sideCatalog-item2';
    }
    ddId = 'sideToolbar-item-0-'+ sideAnchor;
    if(i==0){
      highlight = 'highlight';
    }
    var ddHtml = '<dd id="'+ ddId +'" class="'+ddClass + ' ' + highlight +'">'
        +    '<span class="sideCatalog-index1">'+ sideIndex +'</span>'
        +    '<a class="nslog:1026" onclick="return false;" title="part'+sideAnchor+'" href="#'+sideAnchor+'">'+ sideName +'</a>'
        +    '<span class="sideCatalog-dot"></span>'
        +  '</dd>';
    ddArr.push(ddHtml);
  });
  $('#sideCatalog-catalog dl').html(ddArr.join(''));

  //设置导航的位置
  var slideTop = $(window).height() - $('.slide').height();
  $('.slide').css('top',slideTop);

  var slideInnerHeight = $('#sideCatalog-catalog dl').height();
  var slideOutHeight = $('#sideCatalog-catalog').height();
  var enableTop = slideInnerHeight - slideOutHeight;
  var step = 50;
  //点击向上的按钮
  $('#sideCatalog-down').bind('click', function () {
    if ($(this).hasClass('sideCatalog-down-enable')) {
      if ((enableTop - Math.abs(parseInt($('#sideCatalog-catalog dl').css('top')))) > step) {
        $('#sideCatalog-catalog dl').stop().animate({'top': '-=' + step}, 'fast');
        $('#sideCatalog-up').removeClass('sideCatalog-up-disable').addClass('sideCatalog-up-enable');
      } else {
        $('#sideCatalog-catalog dl').stop().animate({'top': -enableTop}, 'fast');
        $(this).removeClass('sideCatalog-down-enable').addClass('sideCatalog-down-disable');
      }
    } else {
      return false;
    }
  })
  //点击向下的按钮
  $('#sideCatalog-up').bind('click', function () {
    if ($(this).hasClass('sideCatalog-up-enable')) {
      if (Math.abs(parseInt($('#sideCatalog-catalog dl').css('top'))) > step) {
        $('#sideCatalog-catalog dl').stop().animate({'top': '+=' + step}, 'fast');
        $('#sideCatalog-down').removeClass('sideCatalog-down-disable').addClass('sideCatalog-down-enable');
      } else {
        $('#sideCatalog-catalog dl').stop().animate({'top': '0'}, 'fast');
        $(this).removeClass('sideCatalog-up-enable').addClass('sideCatalog-up-disable');
      }
    } else {
      return false;
    }
  })

  //点击导航中的各个目录
  $('#sideCatalog-catalog dl').delegate('dd', 'click', function () {
    var index = $('#sideCatalog-catalog dl dd').index($(this));
    scrollSlide($(this), index);
    var ddIndex = $(this).find('a').stop().attr('href').lastIndexOf('#');
    var ddId = $(this).find('a').stop().attr('href').substring(ddIndex+1);
    var windowTop = $('a[name="' + ddId + '"]').offset().top;
    $('body,html').animate({scrollTop: windowTop}, 'fast');
  })

  //滚动页面,即滚动条滚动
  $(window).scroll(function () {
    if($(this).scrollTop()>$(this).height()){
      $('.slide').show();
    }else{
      $('.slide').hide();
    }
    for (var i=headLen-1; i>=0; i--) {
      if ($(this).scrollTop() >=allEle.eq(i).offset().top - allEle.eq(i).height()) {
        var index = i;
        $('#sideCatalog-catalog dl dd').eq(index).addClass('highlight').siblings('dd').removeClass('highlight');
        scrollSlide($('#sideCatalog-catalog dl dd').eq(index), index);
        return false;
      } else {
        $('#sideCatalog-catalog dl dd').eq(0).addClass('highlight').siblings('dd').removeClass('highlight');
      }
    }
  })

  //导航的滚动,以及向上,向下按钮的显示隐藏
  function scrollSlide(that, index){
    if (index < 5) {
      $('#sideCatalog-catalog dl').stop().animate({'top': '0'}, 'fast');
      $('#sideCatalog-down').removeClass('sideCatalog-down-disable').addClass('sideCatalog-down-enable');
      $('#sideCatalog-up').removeClass('sideCatalog-up-enable').addClass('sideCatalog-up-disable');
    } else if (index > 11) {
      $('#sideCatalog-catalog dl').stop().animate({'top': -enableTop}, 'fast');
      $('#sideCatalog-down').removeClass('sideCatalog-down-enable').addClass('sideCatalog-down-disable');
      $('#sideCatalog-up').removeClass('sideCatalog-up-disable').addClass('sideCatalog-up-enable');
    } else {
      var dlTop = parseInt($('#sideCatalog-catalog dl').css('top')) + slideOutHeight / 2 - (that.offset().top - $('#sideCatalog-catalog').offset().top);
      $('#sideCatalog-catalog dl').stop().animate({'top': dlTop}, 'fast');
      $('#sideCatalog-down').removeClass('sideCatalog-down-disable').addClass('sideCatalog-down-enable');
      $('#sideCatalog-up').removeClass('sideCatalog-up-disable').addClass('sideCatalog-up-enable');
    }
  }

  //置顶
  $('#sideToolbar-up').bind('click', function(){
    $('body,html').animate({scrollTop: 0}, 'fast');
  })

  //slide内容的显示与隐藏
  $('#sideCatalogBtn').bind('click', function(){
    if($(this).hasClass('sideCatalogBtnDisable')){
      $(this).removeClass('sideCatalogBtnDisable');
      $('#sideCatalog').css('visibility','visible');
    }else{
      $(this).addClass('sideCatalogBtnDisable');
      $('#sideCatalog').css('visibility','hidden');
    }
  })

</script>
Javascript 相关文章推荐
JavaScript 图像动画的小demo
May 23 Javascript
jquery等宽输出文字插件使用介绍
Sep 18 Javascript
js实现单行文本向上滚动效果实例代码
Nov 28 Javascript
js+css实现文字散开重组动画特效代码分享
Aug 21 Javascript
JavaScript实现算术平方根算法-代码超简单
Sep 11 Javascript
vue2.x 父组件监听子组件事件并传回信息的方法
Jul 17 Javascript
webpack项目调试以及独立打包配置文件的方法
Feb 28 Javascript
webuploader分片上传的实现代码(前后端分离)
Sep 10 Javascript
angular6的响应式表单的实现
Oct 10 Javascript
Js Snowflake(雪花算法)生成随机ID的实现方法
Aug 26 Javascript
JavaScript实现点击出现子菜单效果
Feb 08 Javascript
html5调用摄像头截图功能
Jan 18 Javascript
jquery实现页面虚拟键盘特效
Aug 08 #Javascript
Jquery实现顶部弹出框特效
Aug 08 #Javascript
Jquery数字上下滚动动态切换插件
Aug 08 #Javascript
Jquery实现弹性滑块滑动选择数值插件
Aug 08 #Javascript
jQuery实现列表内容的动态载入特效
Aug 08 #Javascript
jQuery实现下拉框选择图片功能实例
Aug 08 #Javascript
jQuery实现指定内容滚动同时左侧或其它地方不滚动的方法
Aug 08 #Javascript
You might like
PHP的SQL注入实现(测试代码安全不错)
2011/02/27 PHP
关于查看MSSQL 数据库 用户每个表 占用的空间大小
2013/06/21 PHP
php除数取整示例
2014/04/24 PHP
php封装的单文件(图片)上传类完整实例
2016/10/18 PHP
PHP实现的折半查找算法示例
2017/12/19 PHP
JS实现将人民币金额转换为大写的示例代码
2014/02/13 Javascript
jquery基础教程之数组使用详解
2014/03/10 Javascript
JS实现动态移动层及拖动浮层关闭的方法
2015/04/30 Javascript
JQuery球队选择实例
2015/05/18 Javascript
javascript编写贪吃蛇游戏
2015/07/07 Javascript
每天一篇javascript学习小结(基础知识)
2015/11/10 Javascript
angular.extend方法的具体使用
2017/09/14 Javascript
jQuery阻止事件冒泡实例分析
2018/07/03 jQuery
node实现分片下载的示例代码
2018/10/17 Javascript
深入理解Puppeteer的入门教程和实践
2019/03/05 Javascript
详解Vue-cli3.X使用px2rem遇到的问题
2019/08/09 Javascript
JavaScript this指向相关原理及实例解析
2020/07/10 Javascript
[03:09]显微镜下的DOTA2第一期——带你走进华丽的DOTA2世界
2014/06/20 DOTA
Python中的__new__与__init__魔术方法理解笔记
2014/11/08 Python
python使用Queue在多个子进程间交换数据的方法
2015/04/18 Python
Python 获取当前所在目录的方法详解
2017/08/02 Python
详解Django之auth模块(用户认证)
2018/04/17 Python
Python 网络爬虫--关于简单的模拟登录实例讲解
2018/06/01 Python
python爬取微信公众号文章的方法
2019/02/26 Python
django的ORM模型的实现原理
2019/03/04 Python
使用pandas实现连续数据的离散化处理方式(分箱操作)
2019/11/22 Python
Python数据可视化:顶级绘图库plotly详解
2019/12/07 Python
python开发实例之Python的Twisted框架中Deferred对象的详细用法与实例
2020/03/19 Python
Django 解决阿里云部署同步数据库报错的问题
2020/05/14 Python
纯CSS绘制漂亮的圆形图案效果
2014/05/07 HTML / CSS
美国最大的袜子制造商和零售商:Renfro Socks
2017/09/03 全球购物
你的创业计划书怎样才能打动风投
2014/02/06 职场文书
企业愿景口号
2015/12/25 职场文书
HTML5中 rem适配方案与 viewport 适配问题详解
2021/04/27 HTML / CSS
使用canvas对video视频某一刻截图功能
2021/09/25 HTML / CSS
MySQL数据库查询进阶之多表查询详解
2022/04/08 MySQL