Thank you for considering this question.
The code can be found on GitHub, here.
There are a few things going on here, so before we get to the code I want explain a bit about it.
I have a function makeNavigation that takes three parameters to make the navigation bar: an array of items for the navigation bar, the element id of where the navigation bar should go, and a size.
This works pretty well when the margins are not included. However as soon as the two lines for the margins are uncommented, then the drop down content is much larger than it should be. Thoughts?
In addition, when the window is collapse as small as possible, rather than having just one drop-down element, "Home" and the drop-down bars are stacked. Why / how could I correct this?
NOTE: I source W3 CSS and hover-master
So for variables we have pages and the "sizes".
var pages = ["HOME","ABOUT","PAGE3","PAGE4","PAGE5","PAGE6","PAGE7","PAGE8"];
var extraSmall, small, medium, large;
extraSmall = 610;
small = 700;
medium = 800;
large = 1250;
For functions:
function getSizeInText(size) {
if (size > large) {
return("large")
};
if (extraSmall < size && size < medium) {
return("small")
};
if (medium <= size && size <= large) {
return("medium")
};
if (size <= extraSmall) {
return("extraSmall")
}
}
function makeNavigation(navArray, navID, size) {
var theID = document.getElementById(navID);
var mar = 8;
var pad = 2;
theID.innerHTML = null;
// theID.style.marginRight = mar + "%";
// theID.style.marginLeft = mar + "%";
theID.style.marginTop = mar/4 + "%";
if (size == "extraSmall") {
var numNav = navArray.length;
theID.innerHTML += '<li class="w3-dropdown-hover w3-centered" '+
'style="width:' + spaceAllocated + '%" >' +
'<a class="hvr-reveal navFont">' +
'<i class="fa fa-bars"></i></a>' +
'<ul id="dropDownContent" class="w3-dropdown-content" style="width:'+ spaceAllocated +'%">' +
'</ul>' + '</li>';
for (var i = 0; i < numNav; i++ ) {
document.getElementById('dropDownContent').innerHTML+=
'<li style="width:' +
(100 - 2*pad) + '%">'+
'<a class="hvr-reveal navFont" href="' + navArray[i].toLowerCase() + '.html">' +
navArray[i] + '</a></li>';
}
}
if (size == "small") {
var numNav = navArray.length;
var spaceAllocated = (100 ) / 2;
for (var i = 0; i < 1; i++) {
theID.innerHTML +=
'<li style = "width:' +
spaceAllocated +
'%"><a class="hvr-reveal navFont"' +
' href="' +
navArray[i].toLowerCase() +
'.html">' +
navArray[i] + '</a></li>';
};
theID.innerHTML += '<li class="w3-dropdown-hover w3-centered" '+
'style="width:' + spaceAllocated + '%" >' +
'<a class="hvr-reveal navFont">' +
'<i class="fa fa-bars"></i></a>' +
'<ul id="dropDownContent" class="w3-dropdown-content" style="width:'+ spaceAllocated +'%">' +
'</ul>' + '</li>';
for (var i = 1; i < numNav; i++ ) {
document.getElementById('dropDownContent').innerHTML+=
'<li style="width:' +
(100 - 2*pad) + '%">'+
'<a class="hvr-reveal navFont" href="' + navArray[i].toLowerCase() + '.html">' +
navArray[i] + '</a></li>';
}
}
if (size == "medium") {
var numNav = navArray.length;
var half = Math.floor(numNav/2);
var spaceAllocated = (100 ) / (half+1);
for (var i = 0; i < half; i++) {
theID.innerHTML +=
'<li style = "width:' +
spaceAllocated +
'%"><a class="hvr-reveal navFont"' +
' href="' +
navArray[i].toLowerCase() +
'.html">' +
navArray[i] + '</a></li>';
};
theID.innerHTML += '<li class="w3-dropdown-hover w3-centered" '+
'style="width:' + spaceAllocated + '%" >' +
'<a class="hvr-reveal navFont">' +
'<i class="fa fa-bars"></i></a>' +
'<ul id="dropDownContent" class="w3-dropdown-content" style="width:'+ spaceAllocated +'%">' +
'</ul>' + '</li>';
for (var i = half; i < numNav; i++ ) {
document.getElementById('dropDownContent').innerHTML+=
'<li style="width:' +
(100 - 2*pad) + '%">'+
'<a class="hvr-reveal navFont" href="' + navArray[i].toLowerCase() + '.html">' +
navArray[i] + '</a></li>';
}
};
if (size == "large") {
var numNav = navArray.length;
var spaceAllocated = (100 ) / numNav;
for (var i = 0; i < numNav; i++) {
theID.innerHTML +=
'<li style = "width:' +
spaceAllocated +
'%"><a class="hvr-reveal navFont"' +
' href="' +
navArray[i].toLowerCase() +
'.html">' +
navArray[i] + '</a></li>';
};
};
}
and then in the HTML:
<div class="w3-container w3-section"><ul id="navBar" class="w3-navbar w3-center"></ul></div>
<script type="text/javascript">
var windowWidth;
var size;
jQuery(document).ready(function(){
windowWidth = jQuery(window).width();
size = getSizeInText(windowWidth);
if (windowWidth > large) {}
if (windowWidth < medium) {}
if (medium <= windowWidth && windowWidth <= large) {}
});
jQuery(window).resize(function () {
windowWidth = jQuery(window).width();
size = getSizeInText(windowWidth);
if (windowWidth > large) {
makeNavigation(pages, "navBar", size);
}
if (windowWidth < medium) {
makeNavigation(pages, "navBar", size);
}
if (medium <= windowWidth && windowWidth <= large) {
makeNavigation(pages, "navBar", size);
}
});
</script>
Update
programmer5000 gave a solution to this particular problem. However the same solution does not work when not using w3-css. How come?
/* Drop down content */
li a, .dropbtn {
display: inline-block;
text-align: center;
text-decoration: none;
}
li.dropdown {
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
text-align: center;
width: inherit;
z-index: 1;
}
.dropdown-content a {
text-decoration: none;
display: block;
}
.dropdown:hover .dropdown-content {
display: block;
}
<nav id ='navigation-bar'>
<ul>
<li><a href="#"> HOME </a></li>
<li class="dropdown">
<a class="dropbtn">TEST</a>
<div class="dropdown-content">
<a>1</a>
<a>2</a>
<a>3</a>
</div>
</li>
</ul>
</nav>