jQuery页面定位导航滚动js代码

版权:原创 更新时间:1年以上
[该文章底部包含文件资源,可根据自己情况,决定是否下载资源使用,时间>金钱,如有需要,立即查看资源]

以下是 jQuery页面定位导航滚动js代码 的示例演示效果:

当前平台(PC电脑)
  • 平台:

部分效果截图1:

jQuery页面定位导航滚动js代码

部分效果截图2:

jQuery页面定位导航滚动js代码

HTML代码(index.html):

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>jQuery页面定位导航滚动插件jquery.nav</title>
<link href="css/zzsc.css" rel="stylesheet" media="all">
<style>
.section-wrapper {margin-top: -50px;padding-top: 50px;}
.section-wrapper:first-child .section {padding-top: 100px;}
#nav {background: #000;left: 0;list-style: none;overflow: hidden;padding-top: 9px;position: fixed;right: 0;text-align: center;top: 0;}
#nav li {display: inline-block;	margin-bottom: 0;}
#nav a {background: #000;color: #fff;display: block;padding: 10px;}
#nav a:hover {background: none;	color: #dedede;}
#nav .current a {background: #fff;color: #000;}
</style>
</head>
<body>
<ul id="nav">
  <li class="current"><a href="#examples">examples</a></li>
  <li><a href="#section-1">Section 1</a></li>
  <li><a href="#section-2">Section 2</a></li>
  <li><a href="#section-3">Section 3</a></li>
</ul>
<div id="container">
  <div class="section-wrapper" id="examples">
    <div class="section">
      <h2>Examples</h2>
      <p><a href="index2.html">垂直位置导航</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="index.html">水平位置导航</a></p>
    </div>
  </div>
  <div class="section-wrapper" id="section-1">
    <div class="section">
      <h1>Horizontal Nav</h1>
      <p>To account for the offset of the horizontal nav, add top negative margin and top padding. For example, the navigation on this page is 50px tall. So to each section, add a -50px top margin and 50px to offset the height of the nav. That means when you go to each section, it will scroll to the exact top of the section while accounting for the height of the nav.</p>
    </div>
  </div>
  <div class="section-wrapper" id="section-2">
    <div class="section">
      <h2>Section 2</h2>
      <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
      <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
    </div>
  </div>
  <div class="section-wrapper" id="section-3">
    <div class="section">
      <h2>Section 3</h2>
      <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
      <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
    </div>
  </div>
</div>
<script src="js/jquery.min.js"></script>
<script src="js/jquery.nav.js"></script>
<script>
$(function(){
	$('#nav').onePageNav();
});
</script>
</body>
</html>

HTML代码(index2.html):

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>jQuery页面定位导航滚动插件jquery.nav</title>
<link href="css/zzsc.css" rel="stylesheet" media="all">
</head>
<body>
<ul id="nav">
  <li class="current"><a href="#examples">Examples</a></li>
  <li><a href="#intro">Intro</a></li>
  <li><a href="#usage">Usage</a></li>
  <li><a href="#options">Options</a></li>
</ul>
<div id="container">
  <div class="section" id="examples">
    <h2>Examples</h2>
    <ul>
      <li><a href="index2.html">垂直位置导航</a></li>
      <li><a href="index.html">水平位置导航</a></li>
    </ul>
  </div>
  <div class="section" id="intro">
    <h1>jQuery One Page Nav</h1>
    <p>A lightweight jQuery plugin for the navigation on one-page sites. Adds smooth scrolling when clicking on the navigation and automatically selects the correct navigation items as you are scrolling through the different sections.</p>
  </div>
  <div class="section" id="usage">
    <h2>Usage</h2>
    <pre>$(document).ready(function() {
	$('#nav').onePageNav();
});</pre>
    <h3>Markup</h3>
    <pre>&lt;ul id="nav">
	&lt;li class="current">&lt;a href="#intro">Intro&lt;/a>&lt;/li>
	&lt;li>&lt;a href="#usage">Usage&lt;/a>&lt;/li>
	&lt;li>&lt;a href="#examples">Examples&lt;/a>&lt;/li>
&lt;/ul></pre>
  </div>
  <div class="section" id="options">
    <h2>Options</h2>
    <ul>
      <li><strong>currentClass</strong>: Class to add to the nav to indicate the current item. Defaults to <em>current</em>.</li>
      <li><strong>changeHash</strong>: Whether you want to change the hash when a user clicks on the nav. Defaults to <em>false</em>.</li>
      <li><strong>scrollSpeed</strong>: Speed at which the page will scroll upon clicking on the nav. Defaults to <em>750</em>.</li>
      <li><strong>scrollThreshold</strong>: Percentage of screen at which the next section should become current. Defaults to <em>0.5</em>.</li>
      <li><strong>filter</strong>: Selector of links to filter out of one page nav functionality.</li>
      <li><strong>easing</strong>: Easing method. Defaults to <em>swing</em>.</li>
      <li><strong>begin</strong>: Function to call when the scrolling starts.</li>
      <li><strong>end</strong>: Function to call when the scrolling ends.</li>
      <li><strong>scrollChange</strong>: Function to call when you enter a section. The current nav item gets passed in.</li>
    </ul>
  </div>
</div>
<script src="js/jquery.min.js"></script>
<script src="js/jquery.nav.js"></script>
<script>
$(function(){
	$('#nav').onePageNav();
});
</script>
</body>
</html>

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 constructorvar 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 prototypeOnePageNav.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 navif(this.config.filter !== ''){
	this.$nav = this.$nav.filter(this.config.filter);
}
//Handle clicks on the navthis.$nav.on('click.onePageNav',$.proxy(this.handleClick,this));
	//Get the section positionsthis.getPositions();
	//Handle scroll changesthis.bindInterval();
	//Update the positions on resize toothis.$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 scrolledif(self.didScroll){
	self.didScroll = false;
	self.scrollChange();
}
//If the document height changesif(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 callbackif(self.config.begin){
	self.config.begin();
}
//Change the highlighted nav itemself.adjustNav(self,$parent);
	//Removing the auto-adjust on scrollself.unbindInterval();
	//Scroll to the correct positionself.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 inself.bindInterval();
	//End callbackif(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 setif(position !== null){
	$parent = this.$elem.find('a[href$="#' + position + '"]').parent();
	//If it's not already the current sectionif(!$parent.hasClass(this.config.currentClass)){
	//Change the highlighted nav itemthis.adjustNav(this,$parent);
	//If there is a scrollChange callbackif(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 );
	

CSS代码(zzsc.css):

*{-moz-box-sizing:border-box;-webkit-box-sizing:border-box;box-sizing:border-box;}
html,body{margin:0;padding:0;}
body{color:#222;font:16px/1.5 Helvetica,Arial,sans-serif;}
h1,h2,h3,p,pre,ul{margin:0;padding:0;}
h1{font-size:48px;line-height:1;margin-bottom:1em;}
h2{font-size:24px;line-height:1;margin-bottom:1em;}
h3{font-size:16px;line-height:1;margin-bottom:1em;}
p,pre,ul{margin-bottom:2em;}
pre{background:#fefefe;border:1px solid #dedede;overflow-x:auto;padding:2em;}
ul{margin-left:1em;}
/* ��Ҫ���벿�� */
#nav{left:20px;list-style:none;margin:0;position:fixed;top:20px;}
#nav li{margin-bottom:2px;}
#nav a{background:#ededed;color:#666;display:block;font-size:14px;padding:5px 10px;text-decoration:none;text-transform:uppercase;}
#nav a:hover{background:#dedede;}
#nav .current a{background:#666;color:#ededed;}
#container{margin:0 auto;width:800px;}
.section{border-bottom:5px solid #ccc;min-height:60vh;padding:50px 20px;}
.section p:last-child{margin-bottom:0;}
附件:下载该文件资源,减少时间成本(增值服务)
留言
该资源可下载
File Source
.rar
36.36 KB
jquery特效8
最新结算
jquery虚拟键盘中文打字效果js代码
类型: .rar 金额: CNY 2.31¥ 状态: 待结算 详细>
jquery虚拟键盘中文打字效果js代码
类型: .rar 金额: CNY 0.29¥ 状态: 待结算 详细>
HTML5实现CSS滤镜图片切换特效代码
类型: .rar 金额: CNY 2.31¥ 状态: 待结算 详细>
jQuery头像裁剪插件cropbox js代码
类型: .rar 金额: CNY 0.29¥ 状态: 待结算 详细>
jQuery头像裁剪插件cropbox js代码
类型: .rar 金额: CNY 2.31¥ 状态: 待结算 详细>
CSS3制作3D图片立方体旋转特效
类型: .rar 金额: CNY 2.31¥ 状态: 待结算 详细>
CSS3制作3D图片立方体旋转特效
类型: .rar 金额: CNY 0.29¥ 状态: 待结算 详细>
CSS3制作3D图片立方体旋转特效
类型: .rar 金额: CNY 2.31¥ 状态: 待结算 详细>
CSS3制作3D图片立方体旋转特效
类型: .rar 金额: CNY 0.29¥ 状态: 待结算 详细>
jQuery+css3实现信封效果
类型: .rar 金额: CNY 0.29¥ 状态: 待结算 详细>
我们力求给您提供有用的文章,再此基础上,会附加营收资源,不做任何广告,让平台可以更好发展 若您发现您的权利被侵害,或使用了您的版权,请发邮件联系 sunlifel@foxmail.com ggbig觉得 : 不提供源码的文章不是好文章
合作伙伴
联系我们
  • QQ:21499807
  • 邮箱:sunlifel@foxmail.com
  • QQ扫一扫加QQ
    QQ扫一扫
Copyright 2023-2024 ggbig.com·皖ICP备2023004211号-1
打赏文章