This code works as I intended, but I am definitely a JS noob (C++ background), so I'm not sure if this is the best way to do it (for example using .each
instead of some kind of a for
loop was completely new to me).
https://github.com/TaylorHuston/menuBreak/blob/master/menuBreak.js
function menuBreak() {
var windowWidth = $(window).width();
var menuLength = 0;
$("#navUl > li").each( function (index, element) {
menuLength += $(this).width();
});
if( menuLength > windowWidth*.9 ) {
var newWidth= menuLength*.7;
$('#navUl').css({"maxWidth":newWidth});
} else {
$('#navUl').css({"maxWidth":windowWidth});
}
}
And in my HTML, not sure if this is the 'best' way to call it
<script>
$(document).ready(function() {
menuBreak();
});
$(window).resize(function() {
menuBreak();
});
</script>