Home > Net >  Menu freezes when I open and close
Menu freezes when I open and close

Time:12-09

I created this menu, I have a problem closing it. When it opens and then I click on the x to close it, the site freezes, as if some function remained open and does not close the menu. I tried deleting the function and it seems that now when opening it it doesn't hang anymore. How do I close it following the js done?

$(document).ready(function() {
  var hamburger = $('#hamburger-icon');
  var nav = $('.canvas');
  hamburger.click(function() {
    nav.toggleClass('shown');
    hamburger.toggleClass('active');
    return false;
  });
});
body,
html {
  overflow: hidden;
}

body {
  font-family: 'Montserrat', sans-serif;
  height: 100%;
  margin: 0;
  font-size: 1rem;
  line-height: 1.5;
  letter-spacing: 0.0625em;
  background-image: url("https://upload.wikimedia.org/wikipedia/commons/thumb/6/62/Paracas_National_Reserve,_Ica,_Peru-3April2011.jpg/1600px-Paracas_National_Reserve,_Ica,_Peru-3April2011.jpg");
}

.menuaperto .card {
  margin: 1rem;
}

.menuaperto {
  margin-top: 20rem;
}

@media (min-width:800px) {
  .menuaperto {
    margin-top: 28rem;
  }
}

.minemenu {
  padding: 1rem;
  color: whitesmoke;
}

.separatore {
  padding-left: 5px;
  padding-right: 5px;
}

.bi-bag {
  margin-right: 12px;
  font-size: 20px;
}

.bi-heart {
  font-size: 20px;
}

.bi-person {
  font-size: 20px;
}

.bi-list {
  margin-right: 12px;
  font-size: 25px;
}

.bi-x {
  margin-right: 14px;
  font-size: 25px;
}

.minemenu a {
  text-decoration: none;
  color: whitesmoke;
  font-size: 18px;
}

#menumio {
  background-color: transparent;
}

.menu {
  font-family: 'Cormorant Garamond', serif;
  color: #c0a680;
  font-size: 23px;
}

.shop {
  font-family: 'Cormorant Garamond', serif;
  color: #c0a680;
  font-size: 20px;
}

@media(max-width:550px) {
  .nascondi {
    display: none!important;
  }
  .minemenu a {
    font-size: 16px;
    line-height: 3px;
  }
}

.canvas {
  padding: 1rem;
  color: whitesmoke;
  left: 0;
  width: 100%;
  height: 100%;
  display: block;
  background: #303030;
  position: fixed;
  opacity: 0;
  max-height: 0;
  overflow: hidden;
  transition: all 0.2s;
  z-index: 10;
  text-align: center;
}

.canvas.shown {
  top: 0;
  opacity: 1;
  max-height: none;
  z-index: 10;
}

.canvas a {
  text-decoration: none;
  color: whitesmoke;
  font-size: 18px;
}

.canvas .separatore {
  padding-left: 5px;
  padding-right: 5px;
}

@media(max-width:550px) {
  .canvas a {
    font-size: 16px;
    line-height: 3px;
  }
  .bi-x {
    margin-right: 8px;
    margin-left: -15px;
  }
}
<!--BOOTSTRAP-->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://getbootstrap.com/docs/5.2/assets/css/docs.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css">

<!--MYSTILE-->
<link href="style.css" rel="stylesheet" type="text/css">

<!--GOOGLE FONT-->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=La Belle Aurore&family=Montserrat:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap" rel="stylesheet">


<!--APERTURA MENU'-->
<nav >
  <div >
    <div >
      <div >
        <a id="closebtn"><i ></i></a><span ><i>Menù</i></span>
      </div>
      <div >
        <a href="#">
          <font style="font-weight: 600;">Brand</font> Name</a>
      </div>
      <div >
        <i ></i><span ><i>Shop</i></span>
        <span >|</span>
        <i ></i>
        <span >|</span>
        <i ></i>

      </div>
    </div>

  </div>


</nav>
<!--CHIUSURA MENU'-->



<nav id="menumio">
  <div >
    <div >
      <div >
        <a id="hamburger-icon" href="#"><i ></i></a><span ><i>Menù</i></span>
      </div>
      <div >
        <a href="#">
          <font style="font-weight: 600;">Brand</font> Name</a>
      </div>
      <div >
        <i ></i><span ><i>Shop</i></span>
        <span >|</span>
        <i ></i>
        <span >|</span>
        <i ></i>

      </div>
    </div>
  </div>
</nav>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-easing/1.4.1/jquery.easing.min.js"></script>

CodePudding user response:

The problem you have is your .canvas when on click on your hamburger button, your .canvas overlaps everything with your z-index:10 declared and you can't click it back again to trigger the toggleClass and added the click event too for your X Button

$(document).ready(function() {
  var hamburger = $('#hamburger-icon');
  var nav = $('.canvas');
  var xButton = $('.bi-x');
  hamburger.click(function() {
    nav.toggleClass('shown');
    hamburger.toggleClass('active');
    return false;
  });
  xButton.click(function() {
    nav.toggleClass('shown');
    hamburger.toggleClass('active');
    return false;
  });
});
body,
html {
  overflow: hidden;
}

body {
  font-family: 'Montserrat', sans-serif;
  height: 100%;
  margin: 0;
  font-size: 1rem;
  line-height: 1.5;
  letter-spacing: 0.0625em;
  background-image: url("https://upload.wikimedia.org/wikipedia/commons/thumb/6/62/Paracas_National_Reserve,_Ica,_Peru-3April2011.jpg/1600px-Paracas_National_Reserve,_Ica,_Peru-3April2011.jpg");
}

.menuaperto .card {
  margin: 1rem;
}

.menuaperto {
  margin-top: 20rem;
}

@media (min-width:800px) {
  .menuaperto {
    margin-top: 28rem;
  }
}

.minemenu {
  padding: 1rem;
  color: whitesmoke;
}

.separatore {
  padding-left: 5px;
  padding-right: 5px;
}

.bi-bag {
  margin-right: 12px;
  font-size: 20px;
}

.bi-heart {
  font-size: 20px;
}

.bi-person {
  font-size: 20px;
}

.bi-list {
  margin-right: 12px;
  font-size: 25px;
}

.bi-x {
  margin-right: 14px;
  font-size: 25px;
}

.minemenu a {
  text-decoration: none;
  color: whitesmoke;
  font-size: 18px;
}

#menumio {
  background-color: transparent;
}

.menu {
  font-family: 'Cormorant Garamond', serif;
  color: #c0a680;
  font-size: 23px;
}

.shop {
  font-family: 'Cormorant Garamond', serif;
  color: #c0a680;
  font-size: 20px;
}

@media(max-width:550px) {
  .nascondi {
    display: none!important;
  }
  .minemenu a {
    font-size: 16px;
    line-height: 3px;
  }
}

.canvas {
  padding: 1rem;
  color: whitesmoke;
  left: 0;
  width: 100%;
  height: 100%;
  display: block;
  background: #303030;
  position: fixed;
  opacity: 0;
  max-height: 0;
  overflow: hidden;
  transition: all 0.2s;
  z-index: 10;
  text-align: center;
}

.canvas.shown {
  top: 0;
  opacity: 1;
  max-height: none;
  z-index: 10;
}

.canvas a {
  text-decoration: none;
  color: whitesmoke;
  font-size: 18px;
}

.canvas .separatore {
  padding-left: 5px;
  padding-right: 5px;
}

@media(max-width:550px) {
  .canvas a {
    font-size: 16px;
    line-height: 3px;
  }
  .bi-x {
    margin-right: 8px;
    margin-left: -15px;
  }
}
<!--BOOTSTRAP-->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://getbootstrap.com/docs/5.2/assets/css/docs.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css">

<!--MYSTILE-->
<link href="style.css" rel="stylesheet" type="text/css">

<!--GOOGLE FONT-->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=La Belle Aurore&family=Montserrat:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap" rel="stylesheet">


<!--APERTURA MENU'-->
<nav >
  <div >
    <div >
      <div >
        <a id="closebtn"><i ></i></a><span ><i>Menù</i></span>
      </div>
      <div >
        <a href="#">
          <font style="font-weight: 600;">Brand</font> Name</a>
      </div>
      <div >
        <i ></i><span ><i>Shop</i></span>
        <span >|</span>
        <i ></i>
        <span >|</span>
        <i ></i>

      </div>
    </div>

  </div>


</nav>
<!--CHIUSURA MENU'-->



<nav id="menumio">
  <div >
    <div >
      <div >
        <a id="hamburger-icon" href="#"><i ></i></a><span ><i>Menù</i></span>
      </div>
      <div >
        <a href="#">
          <font style="font-weight: 600;">Brand</font> Name</a>
      </div>
      <div >
        <i ></i><span ><i>Shop</i></span>
        <span >|</span>
        <i ></i>
        <span >|</span>
        <i ></i>

      </div>
    </div>
  </div>
</nav>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-easing/1.4.1/jquery.easing.min.js"></script>

CodePudding user response:

From what i see, it's just that the close button is not listening to any event, it should be solved by just adding this

var closeBtn = $('#closebtn');
closeBtn.click(() => {
    nav.removeClass('shown');
    return false;
});

By adding above js, the "X" button that shows up, would be able to hide the nav again.

and for little detail, don't forget to add href="#" or javascript void script to that "X" button as well

<a id="closebtn" href="#"><i ></i></a><span ><i>Menù</i></span>

Below are the script in action if you're to see how it works directly

$(document).ready(function() {
  var hamburger = $('#hamburger-icon');
  var nav = $('.canvas');
  hamburger.click(function() {
    nav.toggleClass('shown');
    hamburger.toggleClass('active');
    return false;
  });
  var closeBtn = $('#closebtn');
  closeBtn.click(() => {
    nav.removeClass('shown');
    return false;
  });
});
body,
html {
  overflow: hidden;
}

body {
  font-family: 'Montserrat', sans-serif;
  height: 100%;
  margin: 0;
  font-size: 1rem;
  line-height: 1.5;
  letter-spacing: 0.0625em;
  background-image: url("https://upload.wikimedia.org/wikipedia/commons/thumb/6/62/Paracas_National_Reserve,_Ica,_Peru-3April2011.jpg/1600px-Paracas_National_Reserve,_Ica,_Peru-3April2011.jpg");
}

.menuaperto .card {
  margin: 1rem;
}

.menuaperto {
  margin-top: 20rem;
}

@media (min-width:800px) {
  .menuaperto {
    margin-top: 28rem;
  }
}

.minemenu {
  padding: 1rem;
  color: whitesmoke;
}

.separatore {
  padding-left: 5px;
  padding-right: 5px;
}

.bi-bag {
  margin-right: 12px;
  font-size: 20px;
}

.bi-heart {
  font-size: 20px;
}

.bi-person {
  font-size: 20px;
}

.bi-list {
  margin-right: 12px;
  font-size: 25px;
}

.bi-x {
  margin-right: 14px;
  font-size: 25px;
}

.minemenu a {
  text-decoration: none;
  color: whitesmoke;
  font-size: 18px;
}

#menumio {
  background-color: transparent;
}

.menu {
  font-family: 'Cormorant Garamond', serif;
  color: #c0a680;
  font-size: 23px;
}

.shop {
  font-family: 'Cormorant Garamond', serif;
  color: #c0a680;
  font-size: 20px;
}

@media(max-width:550px) {
  .nascondi {
    display: none!important;
  }
  .minemenu a {
    font-size: 16px;
    line-height: 3px;
  }
}

.canvas {
  padding: 1rem;
  color: whitesmoke;
  left: 0;
  width: 100%;
  height: 100%;
  display: block;
  background: #303030;
  position: fixed;
  opacity: 0;
  max-height: 0;
  overflow: hidden;
  transition: all 0.2s;
  z-index: 10;
  text-align: center;
}

.canvas.shown {
  top: 0;
  opacity: 1;
  max-height: none;
  z-index: 10;
}

.canvas a {
  text-decoration: none;
  color: whitesmoke;
  font-size: 18px;
}

.canvas .separatore {
  padding-left: 5px;
  padding-right: 5px;
}

@media(max-width:550px) {
  .canvas a {
    font-size: 16px;
    line-height: 3px;
  }
  .bi-x {
    margin-right: 8px;
    margin-left: -15px;
  }
}

#closebtn{
<!--BOOTSTRAP-->
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://getbootstrap.com/docs/5.2/assets/css/docs.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css">

<!--MYSTILE-->
<link href="style.css" rel="stylesheet" type="text/css">

<!--GOOGLE FONT-->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=La Belle Aurore&family=Montserrat:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap" rel="stylesheet">


<!--APERTURA MENU'-->
<nav >
  <div >
    <div >
      <div >
        <a id="closebtn" href="#"><i ></i></a><span ><i>Menù</i></span>
      </div>
      <div >
        <a href="#">
          <font style="font-weight: 600;">Brand</font> Name</a>
      </div>
      <div >
        <i ></i><span ><i>Shop</i></span>
        <span >|</span>
        <i ></i>
        <span >|</span>
        <i ></i>

      </div>
    </div>

  </div>


</nav>
<!--CHIUSURA MENU'-->



<nav id="menumio">
  <div >
    <div >
      <div >
        <a id="hamburger-icon" href="#"><i ></i></a><span ><i>Menù</i></span>
      </div>
      <div >
        <a href="#">
          <font style="font-weight: 600;">Brand</font> Name</a>
      </div>
      <div >
        <i ></i><span ><i>Shop</i></span>
        <span >|</span>
        <i ></i>
        <span >|</span>
        <i ></i>

      </div>
    </div>
  </div>
</nav>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-easing/1.4.1/jquery.easing.min.js"></script>

  • Related