I am currently designing an HTML page that includes a small navigation bar located halfway down the page. I am aiming to have this navigation bar stick to the top of the page (become fixed) once it reaches the top. Here is the code I have tried:
The script:
<script>
$(function() {
var header = $(".clearHeader");
$(window).scroll(function() {
var scroll = $(window).scrollTop();
if (scroll >= 100) {
header.removeClass('scrollHeader').addClass("fixedHeader");
} else {
header.removeClass("fixedHeader").addClass('scrollHeader');
}
});
});
</script>
The HTML:
<div class="scrollHeader">
<div class="row" style="background:#444;">
<div class="container">
<a href="#"><div class="col-sm-3 smallNav">
Perks
</div></a>
<a href="#"><div class="col-sm-3 smallNav">
Growth
</div></a>
<a href="#"><div class="col-sm-3 smallNav">
Technology
</div></a>
<a href="#"><div class="col-sm-3 smallNav">
Apply
</div></a>
</div>
</div>
</div>
The CSS:
.scrollHeader { position: relative !important;}
.fixedHeader {position: fixed !important; }
Unfortunately, my current code is not functioning as expected. Any advice or suggestions? I am using jQuery 1.9.1. Fiddle