I'm trying to add a drop shadow to my site's fixed navigation bar when it reaches a certain section. Can anyone explain why this isn't working?
In my .CSS,
.whiteDropShadow {
-moz-box-shadow: 0 0 10px #FFFFFF;
-webkit-box-shadow: 0 0 10px #FFFFFF;
-o-box-shadow: 0 0 10px #FFFFFF;
box-shadow: 0 0 10px #FFFFFF;
}
In my .JS,
$(function() {
// Initial top offset from ABOUT section
var topOffset = $('#about').offset().top;
// FUNCTION: adds class to #navLinks when vertical distance from the top is larger than the initial top offset.
var editNavBar = function(){
var verticalDistance = $(window).scrollTop(); // Current vertical distance from the top
if (verticalDistance > topOffset) {
$('#navLinks').addClass('.whiteDropShadow');
} else {
$('#navLinks').removeClass('.whiteDropShadow');
}
};
// Run upon scrolling
$(window).scroll(function() {
editNavBar();
});
});
I have it working here. I just logged out your values to make sure you could scroll far enough for the conditional to be hit. Also added #navLinks
to your whiteDropShadow
selector.
I'd double check your able to scroll long enough on your page to make your conditional truthy.
<div id="navLinks">
<a href="#">item1</a>
<a href="#">item 2</a>
<a href="#">item 3</a>
</div>
<div class="container">
<div id="spacer">
<p>Just for space</p>
</div>
<div id="about">
<p>about</p>
</div>
</div>
// Initial top offset from ABOUT section
var topOffset = $('#about').offset().top;
// FUNCTION: adds class to #navLinks when vertical distance from the top is larger than the initial top offset.
var editNavBar = function(){
var verticalDistance = $(window).scrollTop(); // Current vertical distance from the top
console.log( verticalDistance, topOffset );
if (verticalDistance > topOffset) {
$('#navLinks').addClass('whiteDropShadow');
} else {
$('#navLinks').removeClass('whiteDropShadow');
}
};
// Run upon scrolling
$(window).scroll(function() {
editNavBar();
});
#navLinks {
position: fixed;
top: 0;
width: 100%;
height: 20px;
background: #aeaeae;
}
#navLinks a { color: #000;}
#navLinks.whiteDropShadow {
-moz-box-shadow: 0 0 10px #FFFFFF;
-webkit-box-shadow: 0 0 10px #FFFFFF;
-o-box-shadow: 0 0 10px #FFFFFF;
box-shadow: 0 0 10px #FFFFFF;
}
.container { height: 1400px; }
#spacer {
display: block;
height: 500px;
background: green;
}
#about {
display: block;
height: 500px;
background: red;
}