gpt4 book ai didi

javascript - 导航栏不固定

转载 作者:行者123 更新时间:2023-11-30 15:19:11 25 4
gpt4 key购买 nike

大家好,我正在尝试关注这个Example并为我的网站做完全相同的事情,但是我无法让我的导航栏工作并沿着屏幕向下移动。我已经尝试了很多,但不确定发生了什么。

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<title>Bootstrap 101 Template</title>

<!-- Bootstrap -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/custom.css" rel="stylesheet">

</head>
<body>
<div class="parallax">
<div class="avater" style="background-image: url('Images/Pic1.jpg');background-size: cover;"></div>
<div class="info1">
<h1>Hero</h1>
<h2>Play play play</h2>
</div>
</div>
<nav class="navbar navbar-default">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="#">Home</a></li>
<li><a href="#about">Section 1</a></li>
<li><a href="#contact">Section 2</a></li>
<li><a href="#contact">Section 3</a></li>
</ul>
</div><!--/.nav-collapse -->
</div>
</nav>

<div class="section">
<h1> Section 1 <h1>
</div>

<div class="section2">
<h1> Section 2 <h1>
</div>

<div class="section3">
<h1> Section 3 <h1>
</div>










<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="js/bootstrap.min.js"></script>
<script>
$(document).ready(function(){
$(window).bind('scroll', function() {
var navHeight = $( window ).height() - 70;
if ($(window).scrollTop() > navHeight) {
$('navbar').addClass('fixed');
}
else {
$('navbar').removeClass('fixed');
}
});
});
</script>
</body>
</html>

CSS:

    body {
font-family: 'Open Sans', sans-serif, Arial;
font-size: 16px;
color: #6D6D6D;
background-color: #F5F5F5;
font-weight: 300;
}

h1,h2,h3,h4,h5,h6 {
font-family: 'Montserrat', sans-serif, Arial;
padding-bottom: 5px;
color: #373432;
line-height: 1em;
font-weight: normal;
}

a {
text-decoration: none;
outline: none;
}

/* Header */
.parallax {
background-image: url("../Images/back1.jpg");
min-height: 700px;
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
padding: 100px 20px;
}

.avater {
width: 260px;
height: 260px;
border-radius: 50%;
position: relative;
cursor: default;
box-shadow: inset 0 0 0 0 rgba(200, 95, 66, 0.4), inset 0 0 0 16px rgba(255, 255, 255, 0.16), 0 1px 2px rgba(0, 0, 0, 0.1);
margin: 0 auto;

}

.info1 {
text-align: center;
margin-top: 40px;
}

.info1 > h1 {
font-size: 60px;
color: #FFFFFF;
}

.info1 > h2 {
color: #FFFFFF;
font-size: 30px;
}


/* Navbar */

.fixed {
position: fixed;
top: 0;
height: 70px;
z-index: 1;
}
.navbar {
margin-bottom: 0px;
left: 0;
top: 0;
width: 100%;
list-style: none;
height: 70px;
}
.navbar-nav {
float: none;
margin: 0;
text-align: center;
}
.navbar-nav > li {
float: none;
display: inline-block;
}
.navbar-nav > li > a {
line-height: 38px;
}
.navbar-nav > li > a.active {
background-color: #E7E7E7;
}
.navbar-default .navbar-nav > li > a:hover,
.navbar-default .navbar-nav > li > a:focus {
color: #333333;
background-color: #E7E7E7;
}

.navbar-toggle {
background-color: #000000;
background-image: none;
border-radius: 4px;
float: right;
margin-bottom: 8px;
margin-right: 15px;
margin-top: 18px;
padding: 9px 10px;
position: relative;

}
.navbar-inverse .navbar-toggle .icon-bar {
background-color: #2DCC70;
}
.navbar-default .navbar-toggle:hover,
.navbar-default .navbar-toggle:focus {
background-color: #000000;
}
.navbar-inverse .navbar-collapse,
.navbar-inverse .navbar-form {
border-color: transparent;
}

.section{
background-color:Red;
height: 1000px;
margin: 0;
}

.section h1 {
margin: 0;
}

.section2{
background-color:blue;
height: 1000px;
margin: 0;
}

.section3{
background-color:Black;
height: 1000px;
margin: 0;
}

如果我将 var navHeight = $( window ).height() - 70; 更改为 277 它工作正常但是如果我将屏幕变小它会直接向上没有等待所以真的很奇怪。

在这件事上的任何帮助都会很棒。

谢谢

最佳答案

抱歉。我试图修复你的代码而不是给你一个不同的解决方案。这无需跳跃即可工作。

var $window = $(window),
$el = $('nav'),
elTop = $el.offset().top;

$window.scroll(function() {
$el.toggleClass('navbar-fixed-top', $window.scrollTop() > elTop);
});

关于javascript - 导航栏不固定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44013287/

25 4 0