首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >修复了div上的导航效果

修复了div上的导航效果
EN

Stack Overflow用户
提问于 2016-03-09 06:10:43
回答 2查看 181关注 0票数 0

我找到了这段代码。

代码语言:javascript
复制
   $(document).ready(function(){
	   $(window).bind('scroll', function() {
	   var navHeight = $( window ).height() - 70;
			 if ($(window).scrollTop() > navHeight) {
				 $('nav').addClass('fixed');
			 }
			 else {
				 $('nav').removeClass('fixed');
			 }
		});
	});
代码语言:javascript
复制
/*
Tutorial Name: Scroll To Top Then Fixed Navigation Effect With JQuery and CSS
Description: Create a sticky navigation bar that remains fixed to the top after scroll
Author: Stanislav Kostadinov
Author URI: http://stanislav.it
Version: 1.0.0 - 11.01.2014
*/

* {margin: 0; padding: 0;}

a {text-decoration: none;}

/* This class is added on scroll */
.fixed {
	position: fixed; 
	top: 0; 
	height: 70px; 
	z-index: 1;
}

body {
	color: #fff;
	font-family: 'open-sans-bold'; 
	font-size: 18px;
	text-align: center;
}

/* Font Face Settings */
@font-face {
    font-family: 'open-sans-bold';
	src: url('../fonts/open-sans/OpenSans-Bold.eot');
    src: url('../fonts/open-sans/OpenSans-Bold.eot?iefix') format('embedded-opentype'),
		 url('../fonts/open-sans/OpenSans-Bold.ttf');
    font-weight: normal;
}	

/* Navigation Settings */
nav {
	position: absolute;
	bottom: 0;
	width: 100%;
	height: 70px;
	background: #fff;
}

nav li {
	display: inline-block;
	padding: 24px 10px;
}

nav li a {
	color: #757575;
	text-transform: uppercase;
}

section {
	height: 100vh;
}

/* Screens Settings */
#screen1 {	
	background: #43b29d;
}

#screen1 p {
	padding-top: 200px;
}

#screen2 {
	background: #efc94d;
}

#screen3 {
	background: #e1793d;
}

@media only screen and (max-width: 520px) {

	nav li {
		padding: 24px 4px;
	}

	nav li a {
		font-size: 14px;
	}

}
代码语言:javascript
复制
<section id="screen1">

	<p>Scroll down</p>

	<nav>
		<ul>
			<li><a href="#">Home</a></li>
			<li><a href="#">About</a></li>
			<li><a href="#">Services</a></li>
			<li><a href="#">Team</a></li>
			<li><a href="#">Contact</a></li>
		</ul>
	</nav>

</section>
	
<section id="screen2"></section>
<section id="screen3"></section>

下面是一个有效的示例。http://stanhub.com/tutorials/sticky-navigation/

这是一个粘性的菜单,效果很好,但问题是只有当页面全部向下滚动时,它才会粘滞。是否有可能在导航div向下滚动40px后修复它?假设导航框和顶部浏览器之间的距离是40像素。

EN

回答 2

Stack Overflow用户

发布于 2016-03-09 06:14:21

是,更改代码中的变量var navHeight = $( window ).height() - 70;

navHeight = $(窗口).height() - 40;

这应该可以解决指定高度的问题

票数 0
EN

Stack Overflow用户

发布于 2016-03-10 02:47:57

没有你发布你的代码,这是我能做的最好的基于你的描述。

如下所示更改Javascript:

代码语言:javascript
复制
$(document).ready(function(){
   $(window).bind('scroll', function() {
        var navHeight = 40; //Match the distance of your nav bar from the top of the window
        if ($(window).scrollTop() > navHeight) {
            $('nav').addClass('fixed');
        }
        else {
            $('nav').removeClass('fixed');
        }
    });
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35879028

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档