热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

jquery.nav.js定位导航滚动插件

jQuery.nav.js插件代码:**jQueryOnePageNavPlugin*http:github.comdavist11jQuery-One-Page-Na

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>Documenttitle>
<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>

效果图:

 


推荐阅读
  • 使用nodejs爬取b站番剧数据,计算最佳追番推荐
    本文介绍了如何使用nodejs爬取b站番剧数据,并通过计算得出最佳追番推荐。通过调用相关接口获取番剧数据和评分数据,以及使用相应的算法进行计算。该方法可以帮助用户找到适合自己的番剧进行观看。 ... [详细]
  • 本文介绍了Java工具类库Hutool,该工具包封装了对文件、流、加密解密、转码、正则、线程、XML等JDK方法的封装,并提供了各种Util工具类。同时,还介绍了Hutool的组件,包括动态代理、布隆过滤、缓存、定时任务等功能。该工具包可以简化Java代码,提高开发效率。 ... [详细]
  • Voicewo在线语音识别转换jQuery插件的特点和示例
    本文介绍了一款名为Voicewo的在线语音识别转换jQuery插件,该插件具有快速、架构、风格、扩展和兼容等特点,适合在互联网应用中使用。同时还提供了一个快速示例供开发人员参考。 ... [详细]
  • 本文介绍了高校天文共享平台的开发过程中的思考和规划。该平台旨在为高校学生提供天象预报、科普知识、观测活动、图片分享等功能。文章分析了项目的技术栈选择、网站前端布局、业务流程、数据库结构等方面,并总结了项目存在的问题,如前后端未分离、代码混乱等。作者表示希望通过记录和规划,能够理清思路,进一步完善该平台。 ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • 本文讨论了在手机移动端如何使用HTML5和JavaScript实现视频上传并压缩视频质量,或者降低手机摄像头拍摄质量的问题。作者指出HTML5和JavaScript无法直接压缩视频,只能通过将视频传送到服务器端由后端进行压缩。对于控制相机拍摄质量,只有使用JAVA编写Android客户端才能实现压缩。此外,作者还解释了在交作业时使用zip格式压缩包导致CSS文件和图片音乐丢失的原因,并提供了解决方法。最后,作者还介绍了一个用于处理图片的类,可以实现图片剪裁处理和生成缩略图的功能。 ... [详细]
  • 本文讨论了如何在codeigniter中识别来自angularjs的请求,并提供了两种方法的代码示例。作者尝试了$this->input->is_ajax_request()和自定义函数is_ajax(),但都没有成功。最后,作者展示了一个ajax请求的示例代码。 ... [详细]
  • springboot项目引入jquery浏览器报404错误的解决办法
    本文介绍了在springboot项目中引入jquery时,可能会出现浏览器报404错误的问题,并提供了解决办法。问题可能是由于将jquery.js文件复制粘贴到错误的目录导致的,解决办法是将文件复制粘贴到正确的目录下。如果问题仍然存在,可能是其他原因导致的。 ... [详细]
  • 海马s5近光灯能否直接更换为H7?
    本文主要介绍了海马s5车型的近光灯是否可以直接更换为H7灯泡,并提供了完整的教程下载地址。此外,还详细讲解了DSP功能函数中的数据拷贝、数据填充和浮点数转换为定点数的相关内容。 ... [详细]
  • C语言常量与变量的深入理解及其影响
    本文深入讲解了C语言中常量与变量的概念及其深入实质,强调了对常量和变量的理解对于学习指针等后续内容的重要性。详细介绍了常量的分类和特点,以及变量的定义和分类。同时指出了常量和变量在程序中的作用及其对内存空间的影响,类似于const关键字的只读属性。此外,还提及了常量和变量在实际应用中可能出现的问题,如段错误和野指针。 ... [详细]
  • Android实战——jsoup实现网络爬虫,糗事百科项目的起步
    本文介绍了Android实战中使用jsoup实现网络爬虫的方法,以糗事百科项目为例。对于初学者来说,数据源的缺乏是做项目的最大烦恼之一。本文讲述了如何使用网络爬虫获取数据,并以糗事百科作为练手项目。同时,提到了使用jsoup需要结合前端基础知识,以及如果学过JS的话可以更轻松地使用该框架。 ... [详细]
  • 单页面应用 VS 多页面应用的区别和适用场景
    本文主要介绍了单页面应用(SPA)和多页面应用(MPA)的区别和适用场景。单页面应用只有一个主页面,所有内容都包含在主页面中,页面切换快但需要做相关的调优;多页面应用有多个独立的页面,每个页面都要加载相关资源,页面切换慢但适用于对SEO要求较高的应用。文章还提到了两者在资源加载、过渡动画、路由模式和数据传递方面的差异。 ... [详细]
  • 本文介绍了Java后台Jsonp处理方法及其应用场景。首先解释了Jsonp是一个非官方的协议,它允许在服务器端通过Script tags返回至客户端,并通过javascript callback的形式实现跨域访问。然后介绍了JSON系统开发方法,它是一种面向数据结构的分析和设计方法,以活动为中心,将一连串的活动顺序组合成一个完整的工作进程。接着给出了一个客户端示例代码,使用了jQuery的ajax方法请求一个Jsonp数据。 ... [详细]
  • C#多线程解决界面卡死问题的完美解决方案
    当界面需要在程序运行中不断更新数据时,使用多线程可以解决界面卡死的问题。一个主线程创建界面,使用一个子线程执行程序并更新主界面,可以避免卡死现象。本文分享了一个例子,供大家参考。 ... [详细]
  • Jquery 跨域问题
    为什么80%的码农都做不了架构师?JQuery1.2后getJSON方法支持跨域读取json数据,原理是利用一个叫做jsonp的概念。当然 ... [详细]
author-avatar
大好
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有