If you want to alter the CSS of an element as you scroll down the page, the most effective method is to create a new CSS class with all the necessary rules. For instance:
.navbar-sm {
background-color: blue;
/* define all required rules here */
}
After defining the new CSS class, utilize JQUERY to apply it to the element dynamically based on scrolling behavior:
$(document).ready(function () {
$(window).scroll(function () {
if ($(window).scrollTop() > 100) {
$('.navbar').addClass('navbar-sm');
} else {
$('.navbar').removeClass('navbar-sm');
}
});
if ($(window).scrollTop() > 100) {
$('.navbar').addClass('navbar-sm');
}
});
You can see this concept in action through this live example: LIVE DEMO