如何让导航栏与页面一起向下滚动?我在这里找到了一些答案,但我很困难,因为导航栏的代码在另一个文件中,我只是使用<?php include ("includes/navbar.php"); ?>
将它合并到我的其他页面。以下是我的代码到目前为止:我打算做的是如果我向下滚动页面导航栏将位于页面顶部
导航
<div class="navbar navbar-inverse">
<div class="container">
<div class="navbar-header">
<!-- Button for smallest screens -->
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span></button>
<!--<a class="navbar-brand" href="index.html">
<img src="assets/images/logo.png" alt="Techro HTML5 template"></a>-->
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav pull-right mainNav">
<li class="c1 active"><a href="index.php">Home</a></li>
<li class="c1"><a href="profile.php">Profile</a></li>
<li class="c1"><a href="games.php">Games</a></li>
<li class="c1"><a href="#">Top Players</a></li>
<li class="c1"><a href="about.php">About</a></li>
<li class="c1 dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Settings <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a data-toggle="modal" data-target='#change'>Change Password</a></li>
<li><a href="../logout.php">Logout</a></li>
</ul>
</li>
</ul>
</div>
<!--/.nav-collapse -->
</div>
<!--MODAL-->
<?php include("includes/changepass.php");?>
<!--END MODAL-->
</div>
CSS
.navbar-inverse .navbar-nav > li.c1 a{
color: #00A8FF;
}
.navbar-inverse .navbar-nav > li.c1 a:hover, .navbar-inverse .navbar-nav > li.c1.active a{
color:#fff;
background: #00A8FF;
}
JS
<script type="text/javascript">
$(document).ready(function() {
$(window).scroll(function () {
console.log($(window).scrollTop())
if ($(window).scrollTop() > 280) {
$('.navbar').addClass('navbar-fixed');
}
if ($(window).scrollTop() < 281) {
$('.navbar').removeClass('navbar-fixed');
}
});
});
</script>
以下是我的网页外观
答案 0 :(得分:2)
可能你正在寻找这个。全屏查看
您应该添加$('.navbar').addClass('navbar-fixed-top');
$(document).ready(function() {
$(window).scroll(function() {
console.log($(window).scrollTop())
if ($(window).scrollTop() > 280) {
$('.navbar').addClass('navbar-fixed-top');
}
if ($(window).scrollTop() < 281) {
$('.navbar').removeClass('navbar-fixed-top');
}
});
});
.navbar-inverse .navbar-nav > li.c1 a {
color: #00A8FF;
}
.navbar-inverse .navbar-nav > li.c1 a:hover,
.navbar-inverse .navbar-nav > li.c1.active a {
color: #fff;
background: #00A8FF;
}
.dummy-div {
background: black;
height: 200px;
}
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="dummy-div">
</div>
<div class="navbar navbar-inverse">
<div class="container">
<div class="navbar-header">
<!-- Button for smallest screens -->
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span>
</button>
<!--<a class="navbar-brand" href="index.html">
<img src="assets/images/logo.png" alt="Techro HTML5 template"></a>-->
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav pull-right mainNav">
<li class="c1 active"><a href="index.php">Home</a>
</li>
<li class="c1"><a href="profile.php">Profile</a>
</li>
<li class="c1"><a href="games.php">Games</a>
</li>
<li class="c1"><a href="#">Top Players</a>
</li>
<li class="c1"><a href="about.php">About</a>
</li>
<li class="c1 dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Settings <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a data-toggle="modal" data-target='#change'>Change Password</a>
</li>
<li><a href="../logout.php">Logout</a>
</li>
</ul>
</li>
</ul>
</div>
<!--/.nav-collapse -->
</div>
<!--MODAL-->
<?php include( "includes/changepass.php");?>
<!--END MODAL-->
</div>
<div style="height: 2000px;">
</div>
</div>
答案 1 :(得分:0)
由于您使用的是BootStrap,请将以下类添加到<nav>
元素中:
navbar-fixed-top
例如:
<nav class="navbar navbar-default navbar-fixed-top">
答案 2 :(得分:0)
您是否尝试将position:fixed
添加到.navbar.navbar-inverse
的CSS中?