Trying to use jQuery to set a specific pixel width for my Bootstrap navbar that spans the entire width of the browser window. However, encountering some bugs as the width needs to be recalculated whenever the browser is resized.
Currently, I have this code which sets the initial width of 'nav' as 'navsize', but it doesn't update properly upon window resize:
$(document).on('ready', function () {
$(window).on('resize', function () {
var navsize = $('nav').width();
$('nav').css('width', navsize);
}).trigger('resize');
});
I've also tried using $('nav').innerWidth()
without success.
The resize function is triggered correctly as confirmed by testing with console.log()
.
This customization is necessary because I'm using StickyJS to keep my navigation bar fixed while scrolling, but the percentage-based width causes issues when the navbar leaves its container upon scroll.