0

Good day to all! I am facing an overlap issue when opening a submenu. If you open the first section and open the second submenu tab, the content that comes from 1 tab does not close and overlaps with second tab. How to make it so that when you click on the second title, the first one is hidden? Thanks for helping!

enter image description here

Code:

var nav = document.querySelector('.nav-component');
var menuToggle = document.querySelector('.menu-toggle');
var menuItem = document.querySelector('.menu-item');
var itemOpened = document.querySelector('.item-opened');

menuToggle.addEventListener("click", function() {
  nav.classList.toggle('open');
  document.querySelectorAll('.menu-item.item-opened').forEach(openitem => {
    openitem.classList.remove('item-opened');
  });
  document.querySelectorAll('.chevron').forEach(item => {
    item.addEventListener('click', event => {
      document.querySelectorAll('.menu-item.item-opened').forEach(openitem => {
        openitem.classList.remove('item-opened');
      });
      item.classList.toggle('item-opened');
    })
  })
}, false);
@charset "UTF-8";
html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  width: 100%;
}

body {
  background-color: #fff;
  font-family: "Helvetica", sans-serif;
  font-weight: 100;
}

a {text-decoration: none;color: #fff;}

p {
  font-size: 20px;
  line-height: 28px;
  margin: 0 0 18px;
}
p span {
  position: relative;
  display: block;
  font-size: 16px;
  line-height: 20px;
  padding: 12px 20px;
  margin-bottom: 24px;
  background-color: white;
  box-shadow: rgba(0, 0, 0, 0.05) 0px 2px 4px;
}
p span:before {
  content: '';
  position: absolute;
  top: 0;
  bottom: 0;
  left: -12px;
  margin: auto;
  background-color: #000000;
  width: 24px;
  height: 24px;
  border-radius: 100%;
  box-shadow: rgba(0, 0, 0, 0.1) 0 0 0 3px;
}
p span:after {
  content: '';
  pointer-events: none;
  position: absolute;
  top: -2px;
  bottom: 0;
  left: -6px;
  margin: auto;
  width: 0;
  height: 0;
  border-style: solid;
  border-width: 0 6px 8px 6px;
  border-color: transparent transparent white transparent;
}

h1 {
  font-size: 40px;
  font-weight: normal;
  line-height: 44px;
  text-align: center;
  margin-bottom: 18px;
}


h3 {
  font-size: 24px;
  font-weight: normal;
  text-align: center;
  margin-bottom: 60px;
}

ul {
  list-style: none;
  margin: 0;
  padding: 0;
}
ul li {
  cursor: pointer;
  padding: 6px 20px;
  font-size:18px;
}

button {
  opacity: 1;
  background-color: transparent;
  position: relative;
  z-index: 2;
  top: 24px;
  right: 20px;
  border: none;
  width: 150px;
  height: 32px;
  outline: none;
  cursor: pointer;
  position:absolute;
  top: 10px;
  left:50%;
  right: 0;
  background: blue;
  zoom: 1.2;
}
button:before {
  content: "";
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  margin: auto;
  right: auto;
  width: 100%;
  background: url(burger.svg) no-repeat;background-size: contain;
}
button:after {
  opacity: 1;
  content: '';
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  font-family: Arial, sans-serif;
  line-height: 0;
  background: url(close.svg) no-repeat;background-size: contain;
  -webkit-transition: opacity 0.4s ease-out;
  transition: opacity 0.4s ease-out;
}
button:active {
  -webkit-transform: translateY(2px);
          transform: translateY(2px);
}
button:hover {
  opacity: 1;
}
.open button {
  opacity: 1;
}
.open button:before {
  opacity: 0;
  width: 0;
}
.open button:after {
  opacity: 1;
  -webkit-transform: translate3d(0, 0, 0) rotate(360deg);
          transform: translate3d(0, 0, 0) rotate(360deg);
  -webkit-transition: opacity 0.4s 1s ease-out, -webkit-transform 0.4s 1s ease-out;
  transition: opacity 0.4s 1s ease-out, -webkit-transform 0.4s 1s ease-out;
  transition: transform 0.4s 1s ease-out, opacity 0.4s 1s ease-out;
  transition: transform 0.4s 1s ease-out, opacity 0.4s 1s ease-out, -webkit-transform 0.4s 1s ease-out;
}

nav {
  z-index: 1;
  position: fixed;
  top: -100%;
  left: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
}
nav:before {
  content: '';
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  margin: auto;
  background: #776968;
  width: 100%;
  height: 0;
  padding-bottom: 100%;
  overflow: hidden;
}
.open nav {
  top: 0;
}
.open nav:before {

}

ul.menu {
  color: white;
}

ul.menu > li {
  opacity: 0;
  text-align: right;
}

.open ul.menu li {
  opacity: 1;
  position: relative;
  padding-left: 32px;
}

.chevron {cursor: pointer;display: inline-block;position: absolute;left: 0;width: 32px;text-align: center;}

.chevron:before {
    border-style: solid;
  border-width: 3px 3px 0 0;
  border-color: #fff;
    content: '';
    display: inline-block;
    height: 16px;
    position: relative;
  top: 0.30em;
  left:-50px;
    transform: rotate(-135deg);
    vertical-align: top;
  width: 16px;
  opacity: 1;
}



.submenu{
  display:none;
  opacity:0;
  position: fixed;
  left:0;
  top: 0;
  right: 40%;
  bottom:0;
  z-index: 10;
  font-weight: 100;
}

.submenu .submenu {
  right: 60vw;
}

.submenu .chevron:before {height: 10px;width: 10px;}

.item-opened ~ .submenu{
  opacity:1;
  display:inline-flex;
  flex-direction: column;
  align-items: flex-end;
  justify-content: center;
  z-index: 0;

}
.submenu li{
  text-align: right;
  font-size: 16px;
  padding:0;
}

.submenu .submenu li{
  text-align: right;
  font-size: 18px;
  padding:0;
}

article {
  padding: 24px;
  max-width: 600px;
  margin: 60px auto;
  -webkit-transition: 0.2s ease-out;
  transition: 0.2s ease-out;
}
.open article {
  -webkit-transform: scale(0.92);
          transform: scale(0.92);
  -webkit-transition: -webkit-transform 0.2s 0.41s ease-out;
  transition: -webkit-transform 0.2s 0.41s ease-out;
  transition: transform 0.2s 0.41s ease-out;
  transition: transform 0.2s 0.41s ease-out, -webkit-transform 0.2s 0.41s ease-out;
}


/* Da bootstrap */
.container {
  width: 100%;
  padding-right: 15px;
  padding-left: 15px;
  margin-right: auto;
  margin-left: auto;
}

.h-100 {
  height: 100%!important;
}
.position-relative {
    position: relative!important;
}
.align-items-center {
  -webkit-box-align: center!important;
  -ms-flex-align: center!important;
  align-items: center!important;
}
.justify-content-end {
  -webkit-box-pack: end!important;
  -ms-flex-pack: end!important;
  justify-content: flex-end!important;
}
.d-flex {
  display: -webkit-box!important;
  display: -ms-flexbox!important;
  display: flex!important;
}
<div class="nav-component">
  <div class="container position-relative d-flex align-items-center justify-content-end">
    <button class="menu-toggle">OPEN MENU</button>
  </div>
  <nav>
    <div class="container position-relative d-flex align-items-center justify-content-end h-100">
      <ul class="menu">
        <li class="menu-item">
          <a href="#about-us">Lorem ipsum title 1</a>
          <span class="chevron"></span>
          <ul class="submenu">
            <li>
              <a href="#history">Lorem ipsum dolor</a>
              <span class="chevron"></span>
              <ul class="submenu">
                <li>Sub sub menu</li>
                <li>Sub sub menu</li>
              </ul>
            </li>
            <li>FIRST SUBMENU</li>
            <li>FIRST SUBMENU</li>
            <li>FIRST SUBMENU</li>
          </ul>
        </li>
        <li class="menu-item">
          <a href="#capabilities">Lorem ipsum title 2</a>
          <span class="chevron"></span>
          <ul class="submenu">
            <li> SECOND SUBMENU</li>
            <li> SECOND SUBMENU</li>
            <li> SECOND SUBMENU</li>
          </ul>
        </li>
        <li class="menu-item">
          <a href="#sustainability">Lorem ipsum title 3</a>
          <span class="chevron"></span>
          <ul class="submenu">
            <li> THIRD SUBMENU</li>
            <li> THIRD SUBMENU</li>
            <li> THIRD SUBMENU</li>
          </ul>
        </li>
      </ul>
    </div>
  </nav>
</div>
Isabella Monza
  • 161
  • 1
  • 9

4 Answers4

1

You missed a space in this selector document.querySelectorAll('.menu-item.item-opened') because .item-opened is a child of .menu-item so they need a space between them. When the classes are next to each other, it means that the same component has the two classes at the same time.

Check out the snipped code

var nav = document.querySelector('.nav-component');
var menuToggle = document.querySelector('.menu-toggle');
var menuItem = document.querySelector('.menu-item');
var itemOpened = document.querySelector('.item-opened');

menuToggle.addEventListener("click", function() {
  nav.classList.toggle('open');
  document.querySelectorAll('.menu-item.item-opened').forEach(openitem => {
    openitem.classList.remove('item-opened');
  });
  document.querySelectorAll('.chevron').forEach(item => {
    item.addEventListener('click', event => {
      document.querySelectorAll('.menu-item .item-opened').forEach(openitem => {
        openitem.classList.remove('item-opened');
      });
      item.classList.toggle('item-opened');
    })
  })
}, false);
@charset "UTF-8";
html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  width: 100%;
}

body {
  background-color: #fff;
  font-family: "Helvetica", sans-serif;
  font-weight: 100;
}

a {text-decoration: none;color: #fff;}

p {
  font-size: 20px;
  line-height: 28px;
  margin: 0 0 18px;
}
p span {
  position: relative;
  display: block;
  font-size: 16px;
  line-height: 20px;
  padding: 12px 20px;
  margin-bottom: 24px;
  background-color: white;
  box-shadow: rgba(0, 0, 0, 0.05) 0px 2px 4px;
}
p span:before {
  content: '';
  position: absolute;
  top: 0;
  bottom: 0;
  left: -12px;
  margin: auto;
  background-color: #000000;
  width: 24px;
  height: 24px;
  border-radius: 100%;
  box-shadow: rgba(0, 0, 0, 0.1) 0 0 0 3px;
}
p span:after {
  content: '';
  pointer-events: none;
  position: absolute;
  top: -2px;
  bottom: 0;
  left: -6px;
  margin: auto;
  width: 0;
  height: 0;
  border-style: solid;
  border-width: 0 6px 8px 6px;
  border-color: transparent transparent white transparent;
}

h1 {
  font-size: 40px;
  font-weight: normal;
  line-height: 44px;
  text-align: center;
  margin-bottom: 18px;
}


h3 {
  font-size: 24px;
  font-weight: normal;
  text-align: center;
  margin-bottom: 60px;
}

ul {
  list-style: none;
  margin: 0;
  padding: 0;
}
ul li {
  cursor: pointer;
  padding: 6px 20px;
  font-size:18px;
}

button {
  opacity: 1;
  background-color: transparent;
  position: relative;
  z-index: 2;
  top: 24px;
  right: 20px;
  border: none;
  width: 150px;
  height: 32px;
  outline: none;
  cursor: pointer;
  position:absolute;
  top: 10px;
  left:50%;
  right: 0;
  background: blue;
  zoom: 1.2;
}
button:before {
  content: "";
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  margin: auto;
  right: auto;
  width: 100%;
  background: url(burger.svg) no-repeat;background-size: contain;
}
button:after {
  opacity: 1;
  content: '';
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  font-family: Arial, sans-serif;
  line-height: 0;
  background: url(close.svg) no-repeat;background-size: contain;
  -webkit-transition: opacity 0.4s ease-out;
  transition: opacity 0.4s ease-out;
}
button:active {
  -webkit-transform: translateY(2px);
          transform: translateY(2px);
}
button:hover {
  opacity: 1;
}
.open button {
  opacity: 1;
}
.open button:before {
  opacity: 0;
  width: 0;
}
.open button:after {
  opacity: 1;
  -webkit-transform: translate3d(0, 0, 0) rotate(360deg);
          transform: translate3d(0, 0, 0) rotate(360deg);
  -webkit-transition: opacity 0.4s 1s ease-out, -webkit-transform 0.4s 1s ease-out;
  transition: opacity 0.4s 1s ease-out, -webkit-transform 0.4s 1s ease-out;
  transition: transform 0.4s 1s ease-out, opacity 0.4s 1s ease-out;
  transition: transform 0.4s 1s ease-out, opacity 0.4s 1s ease-out, -webkit-transform 0.4s 1s ease-out;
}

nav {
  z-index: 1;
  position: fixed;
  top: -100%;
  left: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
}
nav:before {
  content: '';
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  margin: auto;
  background: #776968;
  width: 100%;
  height: 0;
  padding-bottom: 100%;
  overflow: hidden;
}
.open nav {
  top: 0;
}
.open nav:before {

}

ul.menu {
  color: white;
}

ul.menu > li {
  opacity: 0;
  text-align: right;
}

.open ul.menu li {
  opacity: 1;
  position: relative;
  padding-left: 32px;
}

.chevron {cursor: pointer;display: inline-block;position: absolute;left: 0;width: 32px;text-align: center;}

.chevron:before {
    border-style: solid;
  border-width: 3px 3px 0 0;
  border-color: #fff;
    content: '';
    display: inline-block;
    height: 16px;
    position: relative;
  top: 0.30em;
  left:-50px;
    transform: rotate(-135deg);
    vertical-align: top;
  width: 16px;
  opacity: 1;
}



.submenu{
  display:none;
  opacity:0;
  position: fixed;
  left:0;
  top: 0;
  right: 40%;
  bottom:0;
  z-index: 10;
  font-weight: 100;
}

.submenu .submenu {
  right: 60vw;
}

.submenu .chevron:before {height: 10px;width: 10px;}

.item-opened ~ .submenu{
  opacity:1;
  display:inline-flex;
  flex-direction: column;
  align-items: flex-end;
  justify-content: center;
  z-index: 0;

}
.submenu li{
  text-align: right;
  font-size: 16px;
  padding:0;
}

.submenu .submenu li{
  text-align: right;
  font-size: 18px;
  padding:0;
}

article {
  padding: 24px;
  max-width: 600px;
  margin: 60px auto;
  -webkit-transition: 0.2s ease-out;
  transition: 0.2s ease-out;
}
.open article {
  -webkit-transform: scale(0.92);
          transform: scale(0.92);
  -webkit-transition: -webkit-transform 0.2s 0.41s ease-out;
  transition: -webkit-transform 0.2s 0.41s ease-out;
  transition: transform 0.2s 0.41s ease-out;
  transition: transform 0.2s 0.41s ease-out, -webkit-transform 0.2s 0.41s ease-out;
}


/* Da bootstrap */
.container {
  width: 100%;
  padding-right: 15px;
  padding-left: 15px;
  margin-right: auto;
  margin-left: auto;
}

.h-100 {
  height: 100%!important;
}
.position-relative {
    position: relative!important;
}
.align-items-center {
  -webkit-box-align: center!important;
  -ms-flex-align: center!important;
  align-items: center!important;
}
.justify-content-end {
  -webkit-box-pack: end!important;
  -ms-flex-pack: end!important;
  justify-content: flex-end!important;
}
.d-flex {
  display: -webkit-box!important;
  display: -ms-flexbox!important;
  display: flex!important;
}
<div class="nav-component">
  <div class="container position-relative d-flex align-items-center justify-content-end">
    <button class="menu-toggle">OPEN MENU</button>
  </div>
  <nav>
    <div class="container position-relative d-flex align-items-center justify-content-end h-100">
      <ul class="menu">
        <li class="menu-item">
          <a href="#about-us">Lorem ipsum title 1</a>
          <span class="chevron"></span>
          <ul class="submenu">
            <li>
              <a href="#history">Lorem ipsum dolor</a>
              <span class="chevron"></span>
              <ul class="submenu">
                <li>Sub sub menu</li>
                <li>Sub sub menu</li>
              </ul>
            </li>
            <li>FIRST MENU</li>

          </ul>
        </li>
        <li class="menu-item">
          <a href="#capabilities">Lorem ipsum title 2</a>
          <span class="chevron"></span>
          <ul class="submenu">
            <li>2222 Lorem ipsum dolor</li>

          </ul>
        </li>
        <li class="menu-item">
          <a href="#sustainability">Lorem ipsum title 3</a>
          <span class="chevron"></span>
          <ul class="submenu">
            <li>333333 Lorem ipsum dolor</li>
          </ul>
        </li>
      </ul>
    </div>
  </nav>
</div>
dev-cc
  • 434
  • 3
  • 13
  • 2
    `.menu-item` and `.item-opened` are on the same element, though. The `
  • ` element. So, actually, the code appears to be correct in that part: https://stackoverflow.com/a/13672822/2430549 You can also just *delete* `.item-opened` from the query and that "fixes" it, too.
  • – HoldOffHunger Jan 20 '21 at 14:43
  • Thanks for reply! If i use this JS, don't open children of submenu (another arrow "level 3"). How can i resolve it ? Thanks. – Isabella Monza Jan 20 '21 at 14:46
  • you cannot add **item.addEventListener('click', event => {** every time you click on menuToggle. This way you create a lot of same click event handler on the same element.... – gaetanoM Jan 20 '21 at 14:47