Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wrap overflowing menu items into dropdown list to create a responsive navigation menu?

Tags:

html

jquery

css

I am working on a menu. I have a menu with many list items. What I want is when I resize the browser to small screens like (laptops and tablets) I want the overflowing li's to collapse and appear in a dropdown.

Actual Menu. CSS navigation menu

Responsive view for the same menu. Responsive menu navigation

Here is the code structure.

var menuWidth = $('ul').width(); //get actual width of ul

var listWidth = $.map($('ul li'), function(val) {
  return $(val).width();
}); //get width of each individual li in an array
var arrayTotalValue = listWidth.reduce(function(a, b) {
  return a + b;
}); //adding above array
var totalWidth = Math.ceil(arrayTotalValue); //rounding up total value

if (totalWidth > menuWidth) {
  $('li').css({
    color: '#FF0000'
  })
}
ul {
  display: flex;
  padding: 50px 0;
  box-sizing: border-box;
  align-items: center;
  width: 700px;
  overflow: visible;
}

li {
  text-decoration: none;
  color: #000000;
  font-size: 20px;
  text-transform: capitalize;
  border: 1px solid black;
  margin: 20px 10px;
  list-style: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul>
  <li class="menu-item">menu1</li>
  <li class="menu-item">menu2</li>
  <li class="menu-item">menu3</li>
  <li class="menu-item">menu4</li>
  <li class="menu-item">menu5</li>
  <li class="menu-item">menu6</li>
  <li class="menu-item">menu7</li>
  <li class="menu-item">menu8</li>
  <li class="menu-item">menu9</li>
  <li class="menu-item">menu10</li>
  <li class="menu-item">menu11</li>
  <li class="menu-item">menu12</li>
  <li class="menu-item">menu13</li>
  <li class="menu-item">menu14</li>
  <li class="menu-item">menu15</li>
  <li class="menu-item">menu16</li>
</ul>

Here is the codepen link of my code.

Any kind of suggestion and answers will be helpful.

like image 430
Mohammed Wahed Khan Avatar asked Dec 01 '18 10:12

Mohammed Wahed Khan


People also ask

How do I make a drop-down menu go over content?

Example Explained. HTML) Use any element to open the dropdown content, e.g. a <span>, or a <button> element. Use a container element (like <div>) to create the dropdown content and add whatever you want inside of it. Wrap a <div> element around the elements to position the dropdown content correctly with CSS.

Which class is used to wrap the button or link with dropdown menu?

To add a dropdown to a button, simply wrap the button and dropdown menu in a . btn-group. You can also use <span class = "caret"></span> to act as an indicator that the button is a dropdown.

Can you wrap text in a dropdown?

Unfortunately, there is no 'word-wrap' feature on Comboboxes or Dropdowns. All you can do is try to make the ComboBox wider or the text smaller.


2 Answers

I have tried to fix your issues. And I found a way to fix your issue. And my way is below.

$('.header').click(function() {
  console.log('open menu');
  if ($(this).parent().hasClass('expanded') == false) {
    $(this).parent().addClass('expanded');
  } else {
    $(this).parent().removeClass('expanded');
  }
})
ul {
  display: flex;
  padding: 50px 0;
  box-sizing: border-box;
  align-items: center;
  width: 700px;
  overflow: visible;
}

ul .header {
  display: none;
}

li {
  margin: 20px 10px;
}

li {
  text-decoration: none;
  color: #000000;
  font-size: 20px;
  text-transform: capitalize;
}

li.cat {
  display: block;
  border: 1px solid black;
}

@media screen and (max-width: 800px) {
  ul {
    display: table-row;
    list-style-type: none;
    margin: 0;
    padding: 0;
  }
  ul .header {
    display: block;
  }
  ul .cat {
    display: none;
  }
}

.expanded li {
  display: block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<ul>
  <li class="header">dropdown
    <li>
      <li class="cat">menu1</li>
      <li class="cat">menu2</li>
      <li class="cat">menu3</li>
      <li class="cat">menu4</li>
      <li class="cat">menu5</li>
      <li class="cat">menu6</li>
      <li class="cat">menu7</li>
      <li class="cat">menu8</li>
      <li class="cat">menu9</li>
      <li class="cat">menu10</li>
      <li class="cat">menu11</li>
      <li class="cat">menu12</li>
      <li class="cat">menu13</li>
      <li class="cat">menu14</li>
      <li class="cat">menu15</li>
      <li class="cat">menu16</li>
</ul>

above example show simple way. So you may costomize.

like image 119
Jin Avatar answered Oct 02 '22 20:10

Jin


First, I would adjust the CSS in way to make it easier. I will enable the wrap (remove the flexbox) and hide all the elements that aren't in the first row by setting a max-height and overflow:hidden. Then I will add the click element that we will see only when there is at least one element hidden. I used a pseudo element trick to hide it.

ul {
  padding:5px 40px 5px 5px;
  box-sizing: border-box;
  overflow: hidden;
  max-height:55px;
  position:relative;
}

li {
  display:inline-block;
  text-decoration: none;
  color: #000000;
  font-size: 20px;
  text-transform: capitalize;
  border: 1px solid black;
  margin:10px;
  list-style: none;
  position:relative;
}

li:last-child::after {
  content:"";
  position:absolute;
  left:102%;
  width:100vw;
  top:-10px;
  bottom:-10px;
  background:#fff;
  z-index:2;
}

.click {
  background:red;
  position:relative;
  width:40px;
  height:40px;
  margin-left:auto;
  margin-top:-65px;
  margin-right:5px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul>
  <li class="menu-item">menu1</li>
  <li class="menu-item">menu2</li>
  <li class="menu-item">menu3</li>
  <li class="menu-item">menu4</li>
  <li class="menu-item">menu5</li>
  <li class="menu-item">menu6</li>
  <li class="menu-item">menu7</li>
  <li class="menu-item">menu8</li>
  <li class="menu-item">menu9</li>
  <li class="menu-item">menu10</li>
  <li class="menu-item">menu11</li>
  <li class="menu-item">menu12</li>
  <li class="menu-item">menu13</li>
  <li class="menu-item">menu14</li>
</ul>
<div class="click"></div>

Now you may add some JS in order to show the hidden elements when clicking the red square by adjusting their position to make them look like a dropdown. To select a hidden elements you need to test if its offesetTop value is bigger than a certain value.

var h = 30;
var val = 0;

$('.click').click(function() {
  if ($('ul').hasClass('show')) {
    $('ul').removeClass('show');
    $('ul li.menu-item').removeClass('drop')
    return;
  }
  val = 0;
  $('ul li.menu-item').each(function() {
    var of = $(this).offset().top - $('ul').offset().top;
    if ( of > 20) {
      $(this).addClass('drop');
      $(this).css('top', 'calc(100% + ' + val + 'px)');
      val += h;
    }
    $('ul').addClass('show');
  })
})
ul {
  padding: 5px 40px 5px 5px;
  box-sizing: border-box;
  overflow: hidden;
  max-height: 55px;
  position: relative;
}

ul.show {
  overflow: visible;
}

li {
  display: inline-block;
  text-decoration: none;
  color: #000000;
  font-size: 20px;
  text-transform: capitalize;
  border: 1px solid black;
  margin: 10px 8px;
  list-style: none;
  position: relative;
}

li.drop {
  display: block;
  position: absolute;
  right: 0;
}

li:last-child::after {
  content: "";
  position: absolute;
  left: 102%;
  width: 100vw;
  top: -10px;
  bottom: -10px;
  background: #fff;
  z-index: 2;
}

li.drop::after {
  content: none;
}

.click {
  background: red;
  position: relative;
  width: 40px;
  height: 40px;
  margin-left: auto;
  margin-top: -65px;
  margin-right: 5px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul>
  <li class="menu-item">menu1</li>
  <li class="menu-item">menu2</li>
  <li class="menu-item">menu3</li>
  <li class="menu-item">menu4</li>
  <li class="menu-item">menu5</li>
  <li class="menu-item">menu6</li>
  <li class="menu-item">menu7</li>
  <li class="menu-item">menu8</li>
  <li class="menu-item">menu9</li>
  <li class="menu-item">menu10</li>
  <li class="menu-item">menu11</li>
  <li class="menu-item">menu12</li>
  <li class="menu-item">menu13</li>
  <li class="menu-item">menu14</li>
</ul>
<div class="click"></div>

The above is a simplified example where I used some hardcoded values that you can easily adjust or make dynamic.

like image 32
Temani Afif Avatar answered Oct 02 '22 19:10

Temani Afif