I'm working on a responsive website with an navigation overlay (nodes and subnodes). The navigation should be split to 4 columns for desktop view or 2 columns for tablet view. It's a nested list navigation, so a column is build with <ul />
around it. My idea was, simply write the navigation in 1 column and arrange it to 4 or 2 cols dynamic with jquery.
html:
<!-- overlay -->
<nav class="overlay">
<!-- ul for column -->
<ul>
<!-- nodes & subnodes -->
<li><a href="#">node</a>
<ul>
<li><a href="#">subnode</a></li>
<li><a href="#">subnode</a></li>
<li><a href="#">subnode</a></li>
<li><a href="#">subnode</a></li>
</ul>
</li>
<li><a href="#">node</a>
<ul>
<li><a href="#">subnode</a></li>
<li><a href="#">subnode</a></li>
<li><a href="#">subnode</a></li>
<li><a href="#">subnode</a></li>
<li><a href="#">subnode</a></li>
<li><a href="#">subnode</a></li>
<li><a href="#">subnode</a></li>
<li><a href="#">subnode</a></li>
</ul>
</li>
<li><a href="#">node</a> ...
</ul>
</nav>
I wrote this function:
$.fn.arrangeObjects = function(wrapWith, maxCols) {
this.each(function() {
if ($(this).parent(wrapWith).length) $(this).unwrap();
});
this.parent().each(function() {
var $el = $(this).children();
amount = $el.length,
wrapAmount = amount / maxCols;
for (var i = 0; i < amount; i += wrapAmount) {
$el.slice(i, i + wrapAmount).wrapAll('<'+ wrapWith +'/>');
}
});
};
Fired for desktops:
$(".overlay > ul > li").arrangeObjects('ul', 4);
Fired for tablets:
$(".overlay > ul > li").arrangeObjects('ul', 2);
This solution splits the nodes in equal parts into the cols. Unfortunately it looks not really nice this way:
http://bern09.ch/notgood.png
What i want to achieve is an arranging with almost the same column heights, like this:
http://bern09.ch/good.png
I have to respect the number of subnodes in a way, but I don't really know how to achieve that. Maybe anyone has the great tip, a little help would be appreciated.