Advertisement
Guest User

Untitled

a guest
Feb 12th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. var $nav = $('.greedy-nav');
  2. var $btn = $('.greedy-nav button');
  3. var $vlinks = $('.greedy-nav .visible-links');
  4. var $hlinks = $('.greedy-nav .hidden-links');
  5.  
  6. var breaks = [];
  7.  
  8. function updateNav() {
  9.  
  10. var availableSpace = $btn.hasClass('hidden') ? $nav.width() : $nav.width() - $btn.width() - 30;
  11.  
  12. // The visible list is overflowing the nav
  13. if($vlinks.width() > availableSpace) {
  14.  
  15. // Record the width of the list
  16. breaks.push($vlinks.width());
  17.  
  18. // Move item to the hidden list
  19. $vlinks.children().last().prependTo($hlinks);
  20.  
  21. // Show the dropdown btn
  22. if($btn.hasClass('hidden')) {
  23. $btn.removeClass('hidden');
  24. }
  25.  
  26. // The visible list is not overflowing
  27. } else {
  28.  
  29. // There is space for another item in the nav
  30. if(availableSpace > breaks[breaks.length-1]) {
  31.  
  32. // Move the item to the visible list
  33. $hlinks.children().first().appendTo($vlinks);
  34. breaks.pop();
  35. }
  36.  
  37. // Hide the dropdown btn if hidden list is empty
  38. if(breaks.length < 1) {
  39. $btn.addClass('hidden');
  40. $hlinks.addClass('hidden');
  41. }
  42. }
  43.  
  44. // Keep counter updated
  45. $btn.attr("count", breaks.length);
  46.  
  47. // Recur if the visible list is still overflowing the nav
  48. if($vlinks.width() > availableSpace) {
  49. updateNav();
  50. }
  51.  
  52. }
  53.  
  54. // Window listeners
  55.  
  56. $(window).resize(function() {
  57. updateNav();
  58. });
  59.  
  60. $btn.on('click', function() {
  61. $hlinks.toggleClass('hidden');
  62. });
  63.  
  64. updateNav();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement