Update Bootstrap 4
The documentation suggests using the sticky polyfill, but the ScrollPos-Styler recommended does not effectively manage scroll position (an offset can be easily defined).
In my opinion, it is simpler to utilize jQuery to monitor the window scroll and adjust the CSS accordingly to make it fixed...
var toggleAffix = function(affixElement, wrapper, scrollElement) {
var height = affixElement.outerHeight(),
top = wrapper.offset().top;
if (scrollElement.scrollTop() >= top){
wrapper.height(height);
affixElement.addClass("affix");
}
else {
affixElement.removeClass("affix");
wrapper.height('auto');
}
};
$('[data-toggle="affix"]').each(function() {
var ele = $(this),
wrapper = $('<div></div>');
ele.before(wrapper);
$(window).on('scroll resize', function() {
toggleAffix(ele, wrapper, $(this));
});
// init
toggleAffix(ele, wrapper, $(window));
});
Bootstrap 4 affix (sticky navbar)
EDIT: Another approach is to consider using this version of the 3.x Affix plugin as a substitute in Bootstrap 4.
Related: Animate/Shrink NavBar on scroll using Bootstrap 4