gpt4 book ai didi

javascript - 如何让导航栏随页面滚动?

转载 作者:行者123 更新时间:2023-11-30 16:23:40 26 4
gpt4 key购买 nike

如何使导航栏与页面一起向下滚动?我在这里找到了一些答案,但我遇到了困难,因为我的导航栏代码在另一个文件中,我刚刚使用了 <?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>

这是我的页面的样子

enter image description here

最佳答案

也许您正在寻找这个。全屏查看

您应该添加 $('.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>

关于javascript - 如何让导航栏随页面滚动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34395003/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com