gpt4 book ai didi

javascript - 如何创建一个滚动后固定在顶部的粘性导航栏

转载 作者:IT王子 更新时间:2023-10-29 03:12:09 29 4
gpt4 key购买 nike

我正在尝试制作一个导航栏,该导航栏会在网站首次加载时出现在可视页面的底部,然后当用户向下滚动时,导航栏会向上滚动,并最终固定在顶部。我正在使用 Bootstrap,就像这个网站一样,但我无法弄清楚这个网站是如何做到的。有帮助吗?

这是我试图模拟的带有导航栏的站点:http://www.blastprocessor.co.uk/

这是我的导航 html 和 css 代码:

HTML:

<div class="navbar navbar-fixed-top" id="navbar">
<div class="navbar-inner">
<div class="container">
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<div class="nav-collapse">
<ul class="nav nav-pills">
<li class="active"><a href="#home">Home</a></li>
<li><a href="#service-link">Services</a></li>
<li><a href="#contact-link">Contact</a></li>
</ul><!-- /.nav -->
</div><!--/.nav-collapse -->
</div><!-- /.container -->
</div><!-- /.navbar-inner -->
</div><!-- /.navbar -->

这是我的 CSS:

.navbar-fixed-top,.navbar-fixed-bottom{position:fixed; -webkit-box-shadow: none; -moz-box-shadow: none; box-shadow: none;}
.navbar .nav > li a{
color:white; background:rgba(0,0,0,0.2); text-shadow:none; font-size:1.7em; font-family: marvel, serif; padding:.5em 1.3em; margin:1em 2em;
}
.navbar .nav > .active a:hover, .navbar .nav > li a:hover, .navbar .nav > .active a {
color:white; ; background:#F90; text-shadow:none; font-size:1.7em; font-family: marvel, serif; padding:.5em 1.3em; margin:1em 2em;
}
.navbar .nav > li {padding:2em;}
.navbar.navbar-fixed-top .navbar-inner{background: rgba(255, 255, 255, 0);}
.navbar .nav, .navbar .nav > li {
float:none;
display:inline-block;
*display:inline; /* ie7 fix */
*zoom:1; /* hasLayout ie7 trigger */
vertical-align: top;
padding:0 2em;
}
.navbar-inner {text-align:center;}
.navbar .navbar-inner, .navbar .navbar-inner {border: none; box-shadow: none; filter: none;}

最佳答案

我一直在寻找同样的东西。我曾读到 Bootstrap 3.0 中提供了此功能,但我并没有真正实现它。这是我想出的,效果很好。非常简单的 jQuery 和 Javascript。

这是可以玩的 JSFiddle... http://jsfiddle.net/CriddleCraddle/Wj9dD/

该解决方案与网络和 StackOverflow 上的其他解决方案非常相似。如果您觉得这个没有用,请搜索您需要的内容。祝你好运!

这是 HTML...

<div id="banner">
<h2>put what you want here</h2>
<p>just adjust javascript size to match this window</p>
</div>

<nav id='nav_bar'>
<ul class='nav_links'>
<li><a href="url">Sign In</a></li>
<li><a href="url">Blog</a></li>
<li><a href="url">About</a></li>
</ul>
</nav>

<div id='body_div'>
<p style='margin: 0; padding-top: 50px;'>and more stuff to continue scrolling here</p>
</div>

这是 CSS...

html, body {
height: 4000px;
}

.navbar-fixed {
top: 0;
z-index: 100;
position: fixed;
width: 100%;
}

#body_div {
top: 0;
position: relative;
height: 200px;
background-color: green;
}

#banner {
width: 100%;
height: 273px;
background-color: gray;
overflow: hidden;
}

#nav_bar {
border: 0;
background-color: #202020;
border-radius: 0px;
margin-bottom: 0;
height: 30px;
}

//the below css are for the links, not needed for sticky nav
.nav_links {
margin: 0;
}

.nav_links li {
display: inline-block;
margin-top: 4px;
}

.nav_links li a {
padding: 0 15.5px;
color: #3498db;
text-decoration: none;
}

现在,只需添加 javacript 即可根据滚动位置添加和删除修复类。

$(document).ready(function() {
//change the integers below to match the height of your upper div, which I called
//banner. Just add a 1 to the last number. console.log($(window).scrollTop())
//to figure out what the scroll position is when exactly you want to fix the nav
//bar or div or whatever. I stuck in the console.log for you. Just remove when
//you know the position.
$(window).scroll(function () {

console.log($(window).scrollTop());

if ($(window).scrollTop() > 550) {
$('#nav_bar').addClass('navbar-fixed-top');
}

if ($(window).scrollTop() < 551) {
$('#nav_bar').removeClass('navbar-fixed-top');
}
});
});

关于javascript - 如何创建一个滚动后固定在顶部的粘性导航栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14667829/

29 4 0