欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-01-23 21:24:42
具体代码如下所示: <%@ page language="java" contenttype="text/html; charset=utf-8" page...

具体代码如下所示:

<%@ 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> 

总结

以上所述是小编给大家介绍的jquery 导航自动跟随滚动的实现代码,希望对大家有所帮助