jQuery 導(dǎo)航自動跟隨滾動的實(shí)現(xiàn)代碼
具體代碼如下所示:
<%@ 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>
總結(jié)
以上所述是小編給大家介紹的jQuery 導(dǎo)航自動跟隨滾動的實(shí)現(xiàn)代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
jquery實(shí)現(xiàn)選項(xiàng)卡切換代碼實(shí)例
這篇文章主要介紹了jquery實(shí)現(xiàn)選項(xiàng)卡切換,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
jQuery學(xué)習(xí)筆記之jQuery的DOM操作
jQuery學(xué)習(xí)筆記之jQuery的DOM操作實(shí)現(xiàn)方法, 學(xué)習(xí)jquery與dom操作的朋友可以參考下。2010-12-12
使用jquery hover事件實(shí)現(xiàn)表格的隔行換色功能示例
hover(over,out)一個(gè)模仿懸停事件的方法,下面一個(gè)示例為大家詳細(xì)介紹下使用jquery實(shí)現(xiàn)表格的隔行換色功能,感興趣的朋友可以參考下2013-09-09
使用jquery DataTable和ajax向頁面顯示數(shù)據(jù)列表的方法
今天小編就為大家分享一篇使用jquery DataTable和ajax向頁面顯示數(shù)據(jù)列表的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08
jQuery實(shí)現(xiàn)布局高寬自適應(yīng)的簡單實(shí)例
下面小編就為大家?guī)硪黄猨Query實(shí)現(xiàn)布局高寬自適應(yīng)的簡單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-05-05
jQuery Easyui 驗(yàn)證兩次密碼輸入是否相等
easyui是一種基于jQuery的用戶界面插件集合。接下來通過本文給大家介紹jQuery Easyui 驗(yàn)證兩次密碼輸入是否相等的相關(guān)知識,感興趣的朋友一起學(xué)習(xí)吧2016-05-05
jQuery實(shí)現(xiàn)滾動切換的tab選項(xiàng)卡效果代碼
這篇文章主要介紹了jQuery實(shí)現(xiàn)滾動切換的tab選項(xiàng)卡效果代碼,涉及jquery鏈?zhǔn)讲僮骷皹邮絼討B(tài)操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08

