jquery.nav.js定位导航滚动插件
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定位导航滚动插件的更多相关文章
- jquery.fullPage.js全屏滚动插件教程演示
css部分(此处需要导入jquery.fullPage.css) <style> .section { text-align: center; font: 50px "Micro ...
- jquery.fullPage.js全屏滚动插件
注:本文内容复制于http://www.51xuediannao.com/js/jquery/jquery.fullPage.html 和 http://www.360doc.com/content/ ...
- Jquery.validate.js表单验证插件的使用
作为一个网站web开发人员,以前居然不知道还有表单验证这样好呀的插件,还在一行行写表单验证,真是后悔没能早点知道他们的存在. 最近公司不忙,自己学习一些东西的时候,发现了validation的一个实例 ...
- jQuery.validate.js表单验证插件
jQuery.validate.js表单验证插件的使用 效果: 代码: <!DOCTYPE html> <html lang="en"> <head& ...
- jQuery定位导航滚动3
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- FullPage.js全屏滚动插件学习总结
如今我们经常能见到全屏网站,尤其是国外网站.这些网站用几幅很大的图片或色块做背景,再添加一些简单的内容,显得格外的高端大气上档次.比如 iPhone 5C 的介绍页面(查看),QQ浏览器的官网站.如果 ...
- FullPage.js全屏滚动插件
一.介绍 fullPage.js是一个基于jQuery的插件,他能够很方便.很轻松的制作出全屏网站,主要功能有: 1.支持鼠标滚动 2.多个回调函数 3.支持手机.平板触摸事件 4.支持CSS3动画 ...
- fullpage.js全屏滚动插件使用小结
刚做好公司网站,通过全屏滚动,显著提高了官网的浏览体验.遂总结一下使用fullpage.js的方法.欢迎指正 一. fullpage.js简介 fullpage.js是一套实现浏览器全屏滚动的js插件 ...
- fullPage.js全屏滚动插件API
API sectionsColor:['green','orange','red','lime']; //设置背景颜色 可以为每一个section设置background-color属性 contro ...
随机推荐
- C#自省
[C#自省] 1.根据string,获取type.Type.GetType 方法,获取具有指定名称的 Type,执行区分大小写的搜索. 2.根据obj,获取type.Object.GetType 方法 ...
- JAVA heap space 内存溢出
3月28日开始使用JAVA调用SAP RFC后提示内存溢出 尝试增加set java_OPTS.....但仍然提示内存溢出 最终设置方法
- 83. Remove Duplicates from Sorted List (List)
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- unity与android交互总结
http://www.jianshu.com/p/4739ce2f4cd1 http://www.cnblogs.com/suoluo/p/5443889.html http://www.th7.cn ...
- HQL多表查询
------------------siwuxie095 HQL 多表查询 以客户和联系人为例(一对多) 1.内连接 (1)hql 语句写法 from Customer c inner join c. ...
- java-tip-各种Map的区别及如何选择
这里主要讨论这三种:HashMap.LinkedHashMap.TreeMap 1. HashMap是常规的哈希表,查询以及插入的性能最好,如果没有特殊要求,应该使用这个 2. LinkedHashM ...
- linux系统chkconfig使用方法及服务开机启动
一.基础知识 有关linux系统开机过程.运行等级,执行权限请看另一篇:linux系统启动过程及运行等级详解. 本篇文章实践的系统:centos6.5 二.创建服务 通过之前的说明,我们知道了如果需要 ...
- tftp-hpa客户端使用说明
1.板子 sudo apt-get install tftp-hpa 2.主机chmod 777 tftp—dir 3.tftp -4 192.168.1.122 -c put lib2.tar.gz ...
- loadrunner录制时web时,ie报安全证书问题
解决方法:在Recording_Options下Port Mapping>Capture level设置为 WinNet level data Capture Level的设置说明:1.Sock ...
- CentOS7安装redis,并设置开机自启动
卸载redis 停止并删除所有已的rendis目录即可. rm -rf /home/wls/soft/redis-4.0.2 rm -rf /etc/redis* rm -rf /var/log/re ...