当用户向下滚动网站时,我试图让我的导航栏显示白色背景。但出于某种原因,我不确定如何让白色背景出现在导航栏上,一旦我滚动,我的一直在消失
所以我的导航栏显示正常,页面顶部没有背景。然后我需要的是,一旦用户向下滚动页面,它应该只是粘在屏幕顶部,背面有白色背景。
像这样:Example
但显然我已经设置了导航栏,我只需要背景显示
<body>
<div class="parallax">
<div class ="logo">
<img src="Images/logo1.fw.png">
</div>
<nav class="navbar navbar-default">
<div class="container-fluid">
<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><a href="#">Home</a></li>
<li><a href="#">Timetable</a></li>
<li><a href="#">Prices</a></li>
<li><a href="#">About Us</a></li>
<li><a href="#">Contact</a></li>
<li><a href="#">Belt Rankings</a></li>
<li><a href="#">Blog</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Classes </a>
<ul class="dropdown-menu">
<li><a href="#">Brazilian Jiu Jitsu</a></li>
<li><a href="#">Wrestling</a></li>
<li><a href="#">Boxing</a></li>
<li><a href="#">Yoga</a></li>
<li><a href="#">Women Self-Defence</a></li>
<li><a href="#">Treatments</a></li>
<li><a href="#">Kids</a></li>
</ul>
</li>
</ul>
</div><!--/.nav-collapse -->
</div><!--/.container-fluid -->
</nav>
<div class="container">
</div>
</div>
<div class="parallax1">
</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.js"></script>
<script>
$(document).ready(function(){
// hide .navbar first
$(".navbar").show();
// fade in .navbar
$(function () {
$(window).scroll(function () {
$('.navbar-default').css({
backgroundColor: $(this).scrollTop() > 100 ?
'white':
'transparent'
})
});
});
});
</script>
</body>
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;
}
.parallax {
background-image: url("../Images/back1.jpg");
min-height: 1000px;
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
width: 100%;
}
.parallax1 {
min-height: 1000px;
}
.logo {
float: left;
padding-top: 50px;
padding-left: 40px;
}
.logo h1 {
text-transform: uppercase;
font-weight: 900;
font-size: 60px;
letter-spacing: -5px;
line-height: 50px;
color: white;
}
.navbar-nav>li>a {
line-height: 6px;
}
.navbar {
float: right;
padding-top: 50px;
padding-right: 60px;
font-size: 13px;
text-transform: uppercase;
}
.navbar-default {
background-color: transparent;
border-top: 0px solid rgba(0, 0, 0, 0.5);
border-bottom: 0px solid rgba(0, 0, 0, 0.5);
border-left: 0px solid rgba(0, 0, 0, 0.5);
border-right: 0px solid rgba(0, 0, 0, 0.5);
-webkit-transition: background-color 200ms linear;
-moz-transition: background-color 200ms linear;
-o-transition: background-color 200ms linear;
-ms-transition: background-color 200ms linear;
transition: background-color 200ms linear;
}
.navbar-default .navbar-nav>li>a {
color: #ffffff;
}
.navbar-default .navbar-nav > li > a:hover,
.navbar-default .navbar-nav > li > a:focus {
background-color: #F8BD23;
text-decoration: none;
color: #ffffff;
-webkit-transition: background-color 200ms linear;
-moz-transition: background-color 200ms linear;
-o-transition: background-color 200ms linear;
-ms-transition: background-color 200ms linear;
transition: background-color 200ms linear;
}
.navbar-nav > li{
padding-left:30px;
}
.container {
width:100%;
height:100%;
display:flex;
justify-content:center;
flex-direction: column;
align-items: center;
}
答案 0 :(得分:2)
原则上,您需要更改:
...
if ($(this).scrollTop() > 100) {
$('.navbar').fadeIn();
} else {
$('.navbar').fadeOut();
}
...
...到......
...
$('.navbar-default').css({
backgroundColor: $(this).scrollTop() > 100 ?
'white':
'transparent'
})
...
您可能还想在CSS中为background-color
.navbar-default
属性添加转换,就像您为链接所做的那样:
.navbar-default {
-webkit-transition: background-color 200ms linear;
-moz-transition: background-color 200ms linear;
-o-transition: background-color 200ms linear;
-ms-transition: background-color 200ms linear;
transition: background-color 200ms linear;
}
在实践中,您希望将其包含在一个不仅仅是scroll
事件调用的函数中。也就是load
和resize
事件,例如:
var checkScrollBar = function(){
$('.navbar-default').css({
backgroundColor: $(this).scrollTop() > 100 ?
'white' : 'transparent'
})
}
$(window).on('load resize scroll', checkScrollBar)
工作示例:
var checkScrollBar = function(){
$('.navbar-default').css({
backgroundColor: $(this).scrollTop() > 100 ?
'white' : 'transparent'
})
}
$(window).on('load resize scroll', checkScrollBar)
body {
margin:0
}
body > div {
background-color: red;
}
.parallax1 {
min-height: 200vh;
}
/*
* all CSS above is just for for SO, don't use in project
*/
nav.navbar-default {
background-color: transparent;
transition: background-color 200ms linear;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="parallax">
<div class="logo">
<img src="Images/logo1.fw.png">
</div>
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container-fluid">
<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><a href="#">Home</a></li>
<li><a href="#">Timetable</a></li>
<li><a href="#">Prices</a></li>
<li><a href="#">About Us</a></li>
<li><a href="#">Contact</a></li>
<li><a href="#">Belt Rankings</a></li>
<li><a href="#">Blog</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Classes </a>
<ul class="dropdown-menu">
<li><a href="#">Brazilian Jiu Jitsu</a></li>
<li><a href="#">Wrestling</a></li>
<li><a href="#">Boxing</a></li>
<li><a href="#">Yoga</a></li>
<li><a href="#">Women Self-Defence</a></li>
<li><a href="#">Treatments</a></li>
<li><a href="#">Kids</a></li>
</ul>
</li>
</ul>
</div>
<!--/.nav-collapse -->
</div>
<!--/.container-fluid -->
</nav>
<div class="container">
</div>
</div>
<div class="parallax1">
</div>