I have a navigation that allows me to say 12 elements, and when resolution reduces the number of elements in a new line. I need to do this when the item is no longer suitable for navigation, it should put the βMOREβ button on the right side of the navigator. and place this item that is not suitable for the dropdown menu. If you do not understand me, there is an image below.
But the problem is that the navigation elements do not always have the same width, since the navigation elements are generated from the REST api.
I tried to make a jQuery script to calculate the width of the elements and add them to the navigation. Here is the script created, I did it in a hurry, so this is really bad.
I need help on how to calculate witdh elements and navigation width correctly and calculate when to add elements to navigation or remove elements from navigation.
Here is the image if you havenβt received it: http://img.hr/aagV
function removeMany() { var i = $items.length - 1; if (itemsWidth > navWidth) { while (itemsWidth > navWidth) { $($items[i]).removeClass('first-level-item').addClass('second-level-item'); dropdownItems.push($items[i]); $($items[i]).removeClass('showed'); $items.pop(); i--; getItemsWidth(); } $nav.append($navMore); dropdownItems.reverse().forEach(function (element, index, array) { $('ul.second-level').append(element); }); getItems(); } }
body { margin: 0; padding: 0; border: 0; } ul, li { margin: 0; padding: 0; list-style: none; } ul.second-level li { display: block !important; } ul.second-level li > a { color: black; } a { color: #fff; text-decoration: none; text-transform: uppercase; } .second-level-item a { color: #333 !important; } .navigation { width: 960px; max-width: 100%; background: #211; color: #aaa; margin: 0 auto; } .first-level .first-level-item { display: inline-block; padding: 10px; } .first-level .item-more { display: inline-block; } .first-level .item-more .second-level-item { display: inline-block; } .second-level { position: absolute; top: 100%; right: 0; width: 200px; background: #fff; padding: 10px; box-shadow: 0 4px 10px rgba(0, 0, 0, 0.4); } .has-second-level { position: relative; } .has-second-level .second-level { display: none; } .has-second-level:hover { background: #fff; color: #000; } .has-second-level:hover .second-level { display: block; }
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>DropDown</title> <link rel="stylesheet" href="css/reset.css"/> <link rel="stylesheet" href="css/style.css"/> </head> <body> <nav class="navigation"> <ul class="first-level"> <li class="first-level-item showed"><a href="#">Introduction to Irish Culture</a></li> <li class="first-level-item showed"><a href="#">Cellular and Molecular Neurobiology</a></li> <li class="first-level-item showed"><a href="#">Guitar foundations</a></li> <li class="first-level-item showed"><a href="#">Startup Inovation</a></li> <li class="first-level-item showed"><a href="#">Astrophysics</a></li> <li class="first-level-item item-more has-second-level"> <span> More </span> <ul class="second-level"> </ul> </li> </ul> </nav> <script src="https://code.jquery.com/jquery-2.1.1.js"></script> </body> </html>
source share