make a fixed menu

Asked

Viewed 115 times

-1

I made a hamburger menu for a site that I want to develop, however my idea is that this menu accompanies the scrolling of the screen and opens perfectly independent of where it is on the site, however I did some tests and always gave some problem, so I just left the burger menu itself with nothing fixed, I was wondering if someone could take a look and explain how to fix this.

HTML

<!-- Inicio do menu !-->
<script>
  function myFunction(x) {
      x.classList.toggle("change");
  }
</script>

<input type="checkbox" id="chec">
<label for="chec">
  <div class="container" onclick="myFunction(this)">
      <div class="bar1"></div>
      <div class="bar2"></div>
      <div class="bar3"></div>
  </div>
</label>
<div class="bg"></div>
<nav id="principal">
  <ul>
      <li><a href="#">Home</a></li>
      <li id="sub10">
          <a href="#">Obras <span>+</span></a>
          <nav id="obras">
              <ul>
                  <li ><a href="#">3D Pavement Art</a></li>
                  <li ><a href="#">Evolution</a></li>
                  <li ><a href="#">The Caves</a></li>
                  <li ><a href="#">Unconditional Love</a></li>
                  <li ><a href="#">The Ark</a></li>
              </ul>
          </nav>
      </li>
      <li><a href="#">FAQ</a></li>
      <li><a href="#">Sobre</a></li>
      <li><a href="#">Contato</a></li>
      <li><a href="#">Estilo das obras</a></li>
  </ul>

CSS

@charset "utf-8";

/* Formatação padrão */
* {
    margin: 0;
    padding: 0;
}

a {
    text-decoration: none;
    color: black;
}

body {
    font-family: "Agency FB", Arial, SansSerif;
    font-size: 1.4em;
    background-color: #F1F1F1;
}

ul {
    list-style: none;
}

/* Inicio do css icone do menu hamburguer */
.container {
    display: inline-block;
    cursor: pointer;
}

.bar1,
.bar2,
.bar3 {
    width: 35px;
    height: 5px;
    background-color: #333;
    margin: 6px 0;
    transition: 0.4s;
}

.change .bar1 {
    -webkit-transform: rotate(-45deg) translate(-9px, 6px);
    transform: rotate(-45deg) translate(-9px, 6px);
    background-color: #ddd;
}

.change .bar2 {
    opacity: 0;
}

.change .bar3 {
    -webkit-transform: rotate(45deg) translate(-8px, -8px);
    transform: rotate(45deg) translate(-8px, -8px);
    background-color: #ddd;
}

/* Fim do css do icone do menu hamburguer */

/* Inicio Menu hamburguer */

#chec {
    display: none;
    /* esconde o checkbox */
}

#chec:checked ~ #principal {
    transform: translateX(100%);
    /* Faz o menu aparecer e reaparecer */
}

#chec:checked ~ .bg {
    display: block;
}



#principal {
    background-color: rgba(110, 110, 110, 0.5);
    /* cor do fundo do menu */
    width: 200px;
    height: 100vh;
    /* unidade base do viewport, pega 100% da pagina */
    left: -200px;
    position: absolute;
    transition: all .4s;
    /* Faz a transição/animação do menu */
}

#obras {
    background-color: rgba(110, 110, 110, 0.5);
    /* cor do fundo do sub-menu */
    /* width: 14%; */
    height: 100vh;
    /* unidade base do viewport, pega 100% da pagina */
    left: -100%;
    transition: all .4s;
    position: relative;
    /* Faz a transição/animação do menu */
}

a {
    display: block;
    padding: 20px 5px;
    color: white;
}

a:hover {
    /* hover do menu */
    background-color: rgb(176, 224, 230);
    color: black;
}

label {
    padding: 5px;
    margin: 10px;
    position: absolute;
    z-index: 1;
}

ul {
    top: 70px;
    position: absolute;
    width: 100%
}

ul li span {
    float: right;
}

.bg {
    width: 100%;
    height: 100vh;
    left: 0;
    top: 0;
    position: fixed;
    background-color: rgba(0, 0, 0, .4);
    display: none;
}

/* Fim Menu hamburguer */

#sub10 #obras {
    top: -70px;
    position: absolute;
    width: 170px;
    pointer-events: none;
}

#sub10:hover #obras {
    left: 100%;
    pointer-events: initial
}

#sub10 {
    overflow: hidden;
    height: 66px;
}

1 answer

0


You can go to the CSS formatting of this menu and put the attribute "position: Fixed;". It will pin your Mac on the screen, then, to adjust it to your screen, use the attributes "top: Npx;" or bottom, left and right to set your margin relative to the page (N is the number of pixels that the menu will walk).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.