Skip to content

Instantly share code, notes, and snippets.

@floster
Created January 10, 2017 15:09
Show Gist options
  • Save floster/74733da7b562bd452021648d31e79571 to your computer and use it in GitHub Desktop.
Save floster/74733da7b562bd452021648d31e79571 to your computer and use it in GitHub Desktop.
$().ready(function () {
//we reconstruct menu on window.resize
$(window).on("resize", function (e) {
var parentWidth = $("#submenu-items-visible").parent().width() - 50;
var ulWidth = $("#submenu-items-hidden").outerWidth();
var menuLi = $("#submenu-items-visible > li");
var liForMoving = new Array();
//take all elements that can't fit parent width to array
menuLi.each(function () {
ulWidth += $(this).outerWidth();
if (ulWidth > parentWidth) {
// console.log(ulWidth);
liForMoving.push($(this));
}
});
if (liForMoving.length > 0) { //if have any in array -> move em to "more" ul
e.preventDefault();
liForMoving.forEach(function (item) {
item.clone().appendTo("#hidden-items-holder");
item.remove();
});
}
else if (ulWidth < parentWidth) { //check if we can put some 'li' back to menu
liForMoving = new Array();
var moved = $("#hidden-items-holder > li");
for (var i = moved.length - 1; i >= 0; i--) { //reverse order
var tmpLi = $(moved[i]).clone();
tmpLi.appendTo($("#submenu-items-visible"));
ulWidth += $(moved[i]).outerWidth();
if (ulWidth < parentWidth) {
$(moved[i]).remove();
}
else {
ulWidth -= $(moved[i]).outerWidth();
tmpLi.remove();
}
}
}
if ($("#hidden-items-holder > li").length > 0) { //if we have elements in extended menu - show it
$("#submenu-items-hidden").show();
}
else {
$("#submenu-items-hidden").hide();
}
});
$(window).trigger("resize"); //call resize handler to build menu right
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment