Skip to content

Instantly share code, notes, and snippets.

@madmanlear
Created December 20, 2011 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save madmanlear/1501707 to your computer and use it in GitHub Desktop.
Save madmanlear/1501707 to your computer and use it in GitHub Desktop.
Split Nav
var splitNav = (function() {
var that = {};
that.container = null;
that.lean = 'left';
that.min = 10;
that.sumArr = function(arr) {
z = 0;
for(var i in arr) {
z += arr[i];
}
return z;
}
that.setup = function() {
$('li.top', that.container).each(function() {
var weights = [];
var li = $(this);
var dad = li.children('ul');
//Gets the weights of each subnav
dad.children('li').each(function() {
var w = 2;
w += $(this).children('ul').children('li').length;
weights.push(w);
});
//Wrap subnav in a div for further control
//Change the classname to whatever
dad.wrap('<div class="sub" />');
var sum = that.sumArr(weights);
var half = Math.ceil(sum/2);
//If threshold is met, split
if(sum >= that.min) {
var check = true;
var index = -1;
var total = 0;
for(var i in weights) {
total += weights[i];
if(check === true && total >= half) {
if(that.lean == 'left') {
//The left column is taller
index = parseInt(i)+1;
} else {
//The right column is taller
index = parseInt(i);
}
check = false;
}
}
if(index != -1) {
var sub = dad.children('li').slice(index);
dad.after($('<ul></ul>').append(sub));
}
}
});
};
that.init = function() {
//Update your selector to suit
that.container = $('.split_it');
if(that.container.length > 0) {
that.setup();
}
};
return that;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment