rasimx
8/30/2017 - 3:40 AM

адаптивное меню

адаптивное меню

$(function() {

  var $nav = $('nav.greedy');
  var $btn = $('nav.greedy button');
  var $vlinks = $('nav.greedy .links');
  var $hlinks = $('nav.greedy .hidden-links');

  var numOfItems = 0;
  var totalSpace = 0;
  var breakWidths = [];

  // Get initial state
  $vlinks.children().outerWidth(function(i, w) {
    totalSpace += w;
    numOfItems += 1;
    breakWidths.push(totalSpace);
  });

  var availableSpace, numOfVisibleItems, requiredSpace;

  function check() {

    // Get instant state
    availableSpace = $vlinks.width() - 10;
    numOfVisibleItems = $vlinks.children().length;
    requiredSpace = breakWidths[numOfVisibleItems - 1];

    // There is not enought space
    if (requiredSpace > availableSpace) {
      $vlinks.children().last().prependTo($hlinks);
      numOfVisibleItems -= 1;
      check();
      // There is more than enough space
    } else if (availableSpace > breakWidths[numOfVisibleItems]) {
      $hlinks.children().first().appendTo($vlinks);
      numOfVisibleItems += 1;
    }
    // Update the button accordingly
    $btn.attr("count", numOfItems - numOfVisibleItems);
    if (numOfVisibleItems === numOfItems) {
      $btn.addClass('hidden');
    } else $btn.removeClass('hidden');
  }

  // Window listeners
  $(window).resize(function() {
    check();
  });

  $btn.on('click', function() {
    $hlinks.toggleClass('hidden');
  });

  check();

}); 
<nav class='greedy'>
  <h1>GreedyNav</h1>
  <ul class='links'>
    <li><a href='#'>navbar</a></li>
    <li><a href='#'>that</a></li>
    <li><a href='#'>handles</a></li>
    <li><a href='#'>overflowing</a></li>
    <li><a href='#'>menu</a></li>
    <li><a href='#'>elements</a></li>
    <li><a href='#'>effortlessly</a></li>
  </ul>
  <button>MENU</button>
  <ul class='hidden-links hidden'></ul>
</nav>
<p>This project is on <a target="_blank" href="https://github.com/lukejacksonn/GreedyNav">github</a></p>
<h5>Resize the window and any overflowing items will get stacked into the dropdown menu</h5>
nav.greedy {
  position: relative;
  height: 60px;
  display: flex;
  align-items: center;
  background: #f2f2f2;
}

nav.greedy h1 {
  display: flex;
  align-self: stretch;
  align-items: center;
  background: #d8d8d8;
  color: #404040;
  padding: 0 1.5rem;
  font-weight: bold;
}

nav.greedy button {
  align-self: stretch;
  transition: all .4s ease-out;
  padding: 0 1rem 0 1.5rem;
  outline: 0;
  border: 0;
  font-size: 0.9rem;
  font-weight: bold;
  background: #c8c8c8;
  color: #404040;
}

nav.greedy button.hidden {
  transition: none;
  border-right: 0.5rem solid #b6b6b6;
  width: 0;
  padding: 0;
  overflow: hidden;
}

nav.greedy button::after {
  content: attr(count);
  display: inline-flex;
  width: 30px;
  height: 30px;
  align-items: center;
  justify-content: center;
  background: #9f9f9f;
  color: #f2f2f2;
  box-shadow: 0 0 1px 0 rgba(0,0,0,0.8);
  border-radius: 50%;
  font-size: 14px;
  line-height: 14px;
  margin-left: 1rem;
  margin-right: calc(-1rem + -8px);
}

ul.links {
  display: flex;
  justify-content: flex-end;
  flex: 1;
  overflow: hidden;
}

ul.links li {
  flex: none;
  padding: 1rem;
}

ul.links li a {
  color: #404040;
}

ul.hidden-links {
  position: absolute;
  background: #d8d8d8;
  right: 0;
  top: 100%;
}

ul.hidden-links li a {
  color: #404040;
  padding-right: 1rem;
}

ul.hidden-links.hidden {
  display: none;
}

ul.hidden-links li {
  padding: 1rem;
}

/*
-- Demo Styles
*/

* {
  box-sizing: border-box;
}

html {
  background: #fefefe;
}

body {
  padding: 2rem;
}

h5 {
  text-align: center;
  margin: auto;
  font-size: 0.8rem;
  line-height: 140%;
  margin-top: 1rem;
  max-width: 50ex;
}

p {
  text-align: center;
  margin-top: 3rem;
}

html {
  width: 100%;
  height: 100%;
  font-family: Helvetica, sans-serif;
}
https://codepen.io/lukejacksonn/pen/BowbWE