Skip to content Skip to sidebar Skip to footer

Function Javascript : On Menu Css Html

I have problems with my menu. Currently, it's only working on the submenu dropdown. How to enable it on the main menu too? Fiddle: http://jsfiddle.net/thepio/pn0ym10e/2/

Solution 1:

Here's a working example. You only need to catch click events for all the elements with class has-submenu and toggle classes etc accordingly.

document.addEventListener('click', function(e) {
  e = e || window.event;
  var target = e.target || e.srcElement;
  if (target.parentElement && target.parentElement.className.indexOf('has-submenu') > -1) {
    e.target.parentElement.classList.toggle('open');
  }
}, false);
#menu {
  background: #343434;
  color: #eee;
  height: 35px;
  border-bottom: 4px solid #eeeded
}

#menuul,
#menuli {
  margin: 00;
  padding: 00;
  list-style: none
}

#menuul {
  height: 35px
}

#menuli {
  float: left;
  display: inline;
  position: relative;
  font: bold 12px Arial;
  text-shadow: 0 -1px0#000;
  border-right: 1px solid #444;
  border-left: 1px solid #111;
  text-transform: uppercase
}

#menuli:first-child {
  border-left: none
}

#menua {
  display: block;
  line-height: 35px;
  padding: 014px;
  text-decoration: none;
  color: #eee;
}

#menuli:hover > a,
#menulia:hover {
  background: #111
}

#menuinput {
  display: none;
  margin: 00;
  padding: 00;
  width: 80px;
  height: 35px;
  opacity: 0;
  cursor: pointer
}

#menulabel {
  font: bold 30px Arial;
  display: none;
  width: 35px;
  height: 36px;
  line-height: 36px;
  text-align: center
}

#menulabelspan {
  font-size: 12px;
  position: absolute;
  left: 35px
}

#menuul.submenu {
  height: auto;
  width: 180px;
  background: #111;
  position: absolute;
  z-index: 99;
  display: none;
  border: 0;
}

#menuul.submenuli {
  display: block;
  width: 100%;
  font: 12px Arial;
  text-transform: none;
}

#menua.home {
  background: #c00;
}

#menua.prett {
  padding: 027px014px
}

#menua.prett::after {
  content: "";
  width: 0;
  height: 0;
  border-width: 6px5px;
  border-style: solid;
  border-color: #eee transparent transparent transparent;
  position: absolute;
  top: 15px;
  right: 9px
}

#menu.has-submenu.open > a.prett::after {
  content: "";
  width: 0;
  height: 0;
  border-width: 6px5px;
  border-style: solid;
  border-color: transparent transparent #eee transparent;
  position: absolute;
  top: 9px;
  right: 9px
}

#menuula:hover {
  background: #333;
}

#menuul.submenu {
  display: none;
  position: absolute;
  left: 180px;
  background: #111;
  top: 0;
  width: 180px;
}

#menuul.menus.submenuli {
  background: #111;
}

#menuli.has-submenu.open > .submenu {
  display: block;
}
<nav><ulid='menu'><li><aclass='home'href='/'>Home</a></li><liclass='has-submenu'><aclass='prett'href='#'title='Menu'>Menu</a><ulclass='submenu'><liclass='has-submenu'><aclass='prett'title='Dropdown 1'>Dropdown 1 + Sub Menu</a><ulclass='submenu'><li><ahref="#"title="Sub Menu">Sub Menu</a></li><li><ahref="#"title="Sub Menu">Sub Menu 2</a></li><li><ahref="#"title="Sub Menu">Sub Menu 3</a></li></ul></li><li><ahref='#'title='Dropdown 2'>Dropdown 2</a></li><li><ahref='#'title='Dropdown 3'>Dropdown 3</a></li></ul></li></ul></nav>

Post a Comment for "Function Javascript : On Menu Css Html"