欢迎来到代码驿站!

jquery

当前位置:首页 > 网页前端 > jquery

jQuery 导航自动跟随滚动的实现代码

时间:2021-07-17 08:11:19|栏目:jquery|点击:

具体代码如下所示:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>   
<c:set var="front" value="${frontPath}${pageContext.request.contextPath}/f"/> 
<c:set var="ajaxUrl" value="${frontPath}${pageContext.request.contextPath}"/> 
<c:set var="ctxStatic" value="${pageContext.request.contextPath}/static"/> 
<!DOCTYPE html> 
<html lang="en"> 
<head> 
  <meta charset="UTF-8"/> 
  <meta name="format-detection" content="telephone=no,email=no,adress=no"/> 
  <meta name="viewport" 
     content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no"/> 
  <meta name="apple-mobile-web-app-capable" content="yes"/> 
  <meta name="apple-mobile-web-app-status-bar-style" content="black"/> 
  <title>simpleNavFollowing</title> 
  <style> 
    * { 
      margin: 0; 
      padding: 0; 
    } 
    html, body { 
      position: relative; 
      height: 100%; 
    } 
    #nav { 
      position: fixed; 
      top: 50%; 
      left: 20px; 
      margin-top: -140px; 
      display: inline-block; 
      vertical-align: middle; 
    } 
    #nav a{ 
      display: block; 
      position: relative; 
      width: 98px; 
      height: 28px; 
      line-height: 28px; 
      text-align: center; 
      background-color: #eee; 
      color: #333; 
      border-left: 2px solid transparent; 
    } 
    #nav a:hover{ 
      width: 98px; 
      border-left: 2px solid #4b59ff; 
    } 
    #nav a p{ 
      position: relative; 
      z-index: 3; 
    } 
    #nav a.active{ 
      background-color: #4b59ff; 
      color: #fff; 
    } 
    #main { 
      max-width: 1200px; 
      margin: 0 auto; 
    } 
    #main div { 
      height: 300px; 
      background-color: #efefef; 
      border-top: 1px solid #ccc; 
    } 
    #main div:nth-child(2n) { 
      background-color: #fafafa; 
    } 
  </style> 
  <script src="http://www.jq22.com/jquery/jquery-1.10.2.js"></script> 
  <script> 
    $(function () { 
      var nav = $("#nav"); 
      var mainPage = $(".mainPage"); 
      var mainTopArr = new Array(); 
      for(var i=0;i<mainPage.length;i++){ 
        var top = mainPage.eq(i).offset().top; 
        mainTopArr.push(top); 
      } 
      $(window).scroll(function(){ 
        var scrollTop = $(this).scrollTop(); 
        var k; 
        for(var i=0;i<mainTopArr.length;i++){ 
          if(scrollTop>=mainTopArr[i]){ 
            k=i; 
          } 
        } 
        nav.find("a").eq(k).addClass("active").siblings().removeClass("active"); 
      }); 
      nav.find("a[href^='#']").click(function(e){ 
        e.preventDefault(); 
        $('html, body').animate({scrollTop: $(this.hash).offset().top}, 400); 
      }); 
    }); 
  </script> 
</head> 
<body> 
<div id="nav"> 
  <a href="#page1" rel="external nofollow" class="active">page1</a> 
  <a href="#page2" rel="external nofollow" >page2</a> 
  <a href="#page3" rel="external nofollow" >page3</a> 
  <a href="#page4" rel="external nofollow" >page4</a> 
  <a href="#page5" rel="external nofollow" >page5</a> 
  <a href="#page6" rel="external nofollow" >page6</a> 
  <a href="#page7" rel="external nofollow" >page7</a> 
  <a href="#page8" rel="external nofollow" >page8</a> 
  <a href="#page9" rel="external nofollow" >page9</a> 
  <a href="#page10" rel="external nofollow" >page10</a> 
</div> 
<div id="main"> 
  <div class="mainPage" id="page1">page1</div> 
  <div class="mainPage" id="page2" style="height: 400px;">page2</div> 
  <div class="mainPage" id="page3">page3</div> 
  <div class="mainPage" id="page4">page4</div> 
  <div class="mainPage" id="page5" style="height: 300px;">page5</div> 
  <div class="mainPage" id="page6">page6</div> 
  <div class="mainPage" id="page7">page7</div> 
  <div class="mainPage" id="page8">page8</div> 
  <div class="mainPage" id="page9" style="height: 900px;">page9</div> 
  <div class="mainPage" id="page10">page10</div> 
  <div class="mainPage" id="page11" style="height: 900px;">page11</div> 
</div> 
</body> 
</html> 

总结

上一篇:解决checkbox的attr(checked)一直为undefined问题

栏    目:jquery

下一篇:jQuery对底部导航进行跳转并高亮显示的实例代码

本文标题:jQuery 导航自动跟随滚动的实现代码

本文地址:http://www.codeinn.net/misctech/158528.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有