The implementation of this is quite simple if the menu can be static and does not need to be adjusted when the window is resized; @ Skip405 example is a really good solution in this case (+1).
If the execution needs to dynamically change the menu when the window is resized, it becomes complicated, though ... The window.onresize event is fired quite often while the user zooms in the browser window, so a naive implementation (for example, @ skip405 approach on each event) will be too slow / expensive.
I would solve the problem as follows:
- Calculate and add the outer width of all links at the beginning.
- Add all available links to the more tab (cloning) so that they can show / hide dynamically. This is much faster than creating new (accordingly destroying) DOM elements all the time.
- Bind a handler to the
onresize event. In the handler, get the current width of the menu and compare it with the width of the links. Hide all links that do not fit the menu, and show their copies on the tab "more". The same thing happens the other way around to display links if the menu is wide enough.
Here is my implementation: http://jsfiddle.net/vNqTF/
Just resize the window and see what happens .;) Please note that the solution can still be optimized, of course, this is just an example.
source share