jQuery.nav.js插件代码:

/*
* jQuery One Page Nav Plugin
* http://github.com/davist11/jQuery-One-Page-Nav
*
* Copyright (c) 2010 Trevor Davis (http://trevordavis.net)
* Dual licensed under the MIT and GPL licenses.
* Uses the same license as jQuery, see:
* http://jquery.org/license
*
* @version 3.0.0
*
* Example usage:
* $('#nav').onePageNav({
* currentClass: 'current',
* changeHash: false,
* scrollSpeed: 750
* });
*/
;(function($, window, document, undefined){ // our plugin constructor
var OnePageNav = function(elem, options){
this.elem = elem;
this.$elem = $(elem);
this.options = options;
this.metadata = this.$elem.data('plugin-options');
this.$win = $(window);
this.sections = {};
this.didScroll = false;
this.$doc = $(document);
this.docHeight = this.$doc.height();
}; // the plugin prototype
OnePageNav.prototype = {
defaults: {
navItems: 'a',
currentClass: 'current',
changeHash: false,
easing: 'swing',
filter: '',
scrollSpeed: 750,
scrollThreshold: 0.5,
begin: false,
end: false,
scrollChange: false
}, init: function() {
// Introduce defaults that can be extended either
// globally or using an object literal.
this.config = $.extend({}, this.defaults, this.options, this.metadata); this.$nav = this.$elem.find(this.config.navItems); //Filter any links out of the nav
if(this.config.filter !== '') {
this.$nav = this.$nav.filter(this.config.filter);
} //Handle clicks on the nav
this.$nav.on('click.onePageNav', $.proxy(this.handleClick, this)); //Get the section positions
this.getPositions(); //Handle scroll changes
this.bindInterval(); //Update the positions on resize too
this.$win.on('resize.onePageNav', $.proxy(this.getPositions, this)); return this;
}, adjustNav: function(self, $parent) {
self.$elem.find('.' + self.config.currentClass).removeClass(self.config.currentClass);
$parent.addClass(self.config.currentClass);
}, bindInterval: function() {
var self = this;
var docHeight; self.$win.on('scroll.onePageNav', function() {
self.didScroll = true;
}); self.t = setInterval(function() {
docHeight = self.$doc.height(); //If it was scrolled
if(self.didScroll) {
self.didScroll = false;
self.scrollChange();
} //If the document height changes
if(docHeight !== self.docHeight) {
self.docHeight = docHeight;
self.getPositions();
}
}, 250);
}, getHash: function($link) {
return $link.attr('href').split('#')[1];
}, getPositions: function() {
var self = this;
var linkHref;
var topPos;
var $target; self.$nav.each(function() {
linkHref = self.getHash($(this));
$target = $('#' + linkHref); if($target.length) {
topPos = $target.offset().top;
self.sections[linkHref] = Math.round(topPos);
}
});
}, getSection: function(windowPos) {
var returnValue = null;
var windowHeight = Math.round(this.$win.height() * this.config.scrollThreshold); for(var section in this.sections) {
if((this.sections[section] - windowHeight) < windowPos) {
returnValue = section;
}
} return returnValue;
}, handleClick: function(e) {
var self = this;
var $link = $(e.currentTarget);
var $parent = $link.parent();
var newLoc = '#' + self.getHash($link); if(!$parent.hasClass(self.config.currentClass)) {
//Start callback
if(self.config.begin) {
self.config.begin();
} //Change the highlighted nav item
self.adjustNav(self, $parent); //Removing the auto-adjust on scroll
self.unbindInterval(); //Scroll to the correct position
self.scrollTo(newLoc, function() {
//Do we need to change the hash?
if(self.config.changeHash) {
window.location.hash = newLoc;
} //Add the auto-adjust on scroll back in
self.bindInterval(); //End callback
if(self.config.end) {
self.config.end();
}
});
} e.preventDefault();
}, scrollChange: function() {
var windowTop = this.$win.scrollTop();
var position = this.getSection(windowTop);
var $parent; //If the position is set
if(position !== null) {
$parent = this.$elem.find('a[href$="#' + position + '"]').parent(); //If it's not already the current section
if(!$parent.hasClass(this.config.currentClass)) {
//Change the highlighted nav item
this.adjustNav(this, $parent); //If there is a scrollChange callback
if(this.config.scrollChange) {
this.config.scrollChange($parent);
}
}
}
}, scrollTo: function(target, callback) {
var offset = $(target).offset().top; $('html, body').animate({
scrollTop: offset
}, this.config.scrollSpeed, this.config.easing, callback);
}, unbindInterval: function() {
clearInterval(this.t);
this.$win.unbind('scroll.onePageNav');
}
}; OnePageNav.defaults = OnePageNav.prototype.defaults; $.fn.onePageNav = function(options) {
return this.each(function() {
new OnePageNav(this, options).init();
});
}; })( jQuery, window , document );
/* 代码整理:懒人之家 www.lanrenzhijia.com */

html代码:

<!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
* {
margin: 0;
padding: 0
} li {
list-style: none
}
a{text-decoration: none;} #nav {
position: fixed;
*left: 90px;
top:100px;
} #nav li {
margin-bottom: 2px;
width: 130px;
height: 41px;
line-height: 41px;
} #nav a {
background: #B18282;
color: #fff;
display: block;
text-transform: uppercase;
-ms-text-overflow: ellipsis;
padding: 0 5px;
text-overflow: ellipsis;
overflow: hidden;
white-space: nowrap;
} #nav a:hover {
background: #00d420;
color: #fff;
} #nav .current a {
background: #00d420;
color: #fff;
}
.head{margin: 0 auto; width: 100%; height: 100px; line-height: 100px; background: #D49B6D; text-align: center; font-size: 30px;}
.main{ width: 800px; margin: 0 auto; }
.box{ width: 600px; border:1px solid #ddd; margin-left: 140px; }
</style>
</head> <body>
<div class="head">导航</div>
<div class="main">
<ul id="nav">
<li><a href="#a0">花卉详情</a></li>
<li><a href="#a1">介绍</a></li>
<li><a href="#a2">形态特征</a></li>
<li><a href="#a3">生长习性</a></li>
<li><a href="#a4">栽培技术</a></li>
<li><a href="#a5">主要价值</a></li>
</ul>
<div class="box">
<div id="a0" style="height: 300px;border:1px solid #ddd;">花卉详情</div>
<div id="a1" style="height: 300px;border:1px solid #ddd;">介绍</div>
<div id="a2" style="height: 300px;border:1px solid #ddd;">形态特征</div>
<div id="a3" style="height: 300px;border:1px solid #ddd;">生长习性</div>
<div id="a4" style="height: 300px;border:1px solid #ddd;">栽培技术</div>
<div id="a5" style="height: 300px;border:1px solid #ddd;">主要价值</div>
</div>
</div>
<script src="http://apps.bdimg.com/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="js/jquery.nav.js"></script>
<script>
$(function() {
$('#nav').onePageNav();
});
</script>
</body> </html>

效果图:

jquery.nav.js定位导航滚动插件的更多相关文章

  1. jquery.fullPage.js全屏滚动插件教程演示

    css部分(此处需要导入jquery.fullPage.css) <style> .section { text-align: center; font: 50px "Micro ...

  2. jquery.fullPage.js全屏滚动插件

    注:本文内容复制于http://www.51xuediannao.com/js/jquery/jquery.fullPage.html 和 http://www.360doc.com/content/ ...

  3. Jquery.validate.js表单验证插件的使用

    作为一个网站web开发人员,以前居然不知道还有表单验证这样好呀的插件,还在一行行写表单验证,真是后悔没能早点知道他们的存在. 最近公司不忙,自己学习一些东西的时候,发现了validation的一个实例 ...

  4. jQuery.validate.js表单验证插件

    jQuery.validate.js表单验证插件的使用 效果: 代码: <!DOCTYPE html> <html lang="en"> <head& ...

  5. jQuery定位导航滚动3

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. FullPage.js全屏滚动插件学习总结

    如今我们经常能见到全屏网站,尤其是国外网站.这些网站用几幅很大的图片或色块做背景,再添加一些简单的内容,显得格外的高端大气上档次.比如 iPhone 5C 的介绍页面(查看),QQ浏览器的官网站.如果 ...

  7. FullPage.js全屏滚动插件

    一.介绍 fullPage.js是一个基于jQuery的插件,他能够很方便.很轻松的制作出全屏网站,主要功能有: 1.支持鼠标滚动 2.多个回调函数 3.支持手机.平板触摸事件 4.支持CSS3动画 ...

  8. fullpage.js全屏滚动插件使用小结

    刚做好公司网站,通过全屏滚动,显著提高了官网的浏览体验.遂总结一下使用fullpage.js的方法.欢迎指正 一. fullpage.js简介 fullpage.js是一套实现浏览器全屏滚动的js插件 ...

  9. fullPage.js全屏滚动插件API

    API sectionsColor:['green','orange','red','lime']; //设置背景颜色 可以为每一个section设置background-color属性 contro ...

随机推荐

  1. 删除链表之two star programming

    最近偶尔看到一篇关于Linus Torvalds的访问,大神说大部分人都不理解指针.假设被要求写一段链表删除节点的程序,大多数的做法都是跟踪两个指针,当前指针cur和其父节点pre,这种实现很容易理解 ...

  2. python 数值计算库

    pip install numpy pip install matplotlib pip install sklearn yum -y install tkinter pip install scip ...

  3. Linux实战教学笔记26:http协议原理

    目录 第二十六节 http协议原理 第1章 Web服务基础 1.1 http服务重要基础 1.2 HTTP协议 1.3 HTTP资源 1.4 网站流量度量术语 1.5 www服务软件介绍 1.6 本章 ...

  4. Python基础:面向对象基础 (一) 类及其属性和魔法方法

    定义类,添加和获取对象属性 # 定义类 格式如下 # class 类名: # 方法列表 # 新式类定义形式 # info 是一个实例方法,第一个参数一般是self,表示实例对象本身 class Her ...

  5. O(n)求中位数和第k大数

    解题关键:模板与思路.面试题 #include<cstdio> #include<cstring> #include<algorithm> #include< ...

  6. Mysql 使用触发器,把插入的数据在插入到宁一张表里

    CREATE TRIGGER tgr_tablea_insert AFTER //触发器名字 动作在插入数据之后 ON alertinfo //监听哪个表之后触发 FOR INSERT //监听的表的 ...

  7. 226. Invert Binary Tree 翻转二叉树

    [抄题]: Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 [暴力解法]: 时间分析: 空间分 ...

  8. maven filter插件只替换了部分变量问题

    maven filter简介 maven的resources插件,有一个filter的作用,能够在打包的时候,从特定文件里读取key-value对,替换配置文件中的占位符变量.很多线上线下有不同环境的 ...

  9. Windows环境下搭建MosQuitto服务器

    Windows环境下搭建MosQuitto服务器 2018年04月16日 22:00:01 wistronpj 阅读数:1185  摘自:https://blog.csdn.net/pjlxm/art ...

  10. 白盒测试实践-任务进度-Day02

    所使用静态代码检查工具 阿里巴巴Java开发代码检测IDE插件 小组成员 华同学.郭同学.覃同学.刘同学.穆同学.沈同学 任务进度 在经过任务分配阶段后,大家都投入到了各自的任务中,以下是大家今天任务 ...