Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

97
Visualizações
Setting document style width with Javascript makes the animation go from left to right. How do I counteract this?

So I have a (currently empty) div with just the id "menu". I gave it some CSS:

#menu {
    height:70%;
    width:0;
    position:fixed;
    z-index:1;
    top:0;
    left:50%;
    background-color:white;
    overflow-x:hidden;
    transition:0.2s;
    padding-top:70px;
}

inspired almost entirely by W3S' sidebar. I also have another div with onclick="openMenu()" and the function itself is

document.getElementById("menu").style.width = "250px";

So the issue is that despite working, the (currently blank) menu opens from left to right and not right to left or even middle to ends. Is this some default behaviour or an issue in my code? And to fix it, do I need to use CSS before and after or...

edit: I realise transition:0.2s is probably the culprit but I would still like to leave it in there

edit2: I should clarify that my goal is to have a sidebar open in the middle of the screen upon clicking a div. Therefore it should be invisible on startup and then open up (by adding width) with a click, hence the JS. My issue of course is that I would prefer the menu to transition from nothing into something by opening up both ways (instead of left to right) from the middle of the screen or simply making it reverse-fade into existence

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

This snippet shows a very simple approach that you can take for what you're after

Instead of using JS to control the individual CSS properties, it's often better to use classes which you can toggle via JS

document.getElementById("toggle-menu").onclick = function(){
    document.getElementById("menu").classList.toggle("active")
}
html,
body {
  height: 100%;
}

#menu {
  display:none;
  position:fixed;
  background:#f57777;
  border-radius:4px;
  left:0;
  right:0;
  width:250px;
  height:250px;
  margin:auto;
  box-shadow: 0 0 12px #aaa;
}

#menu.active {
  display:block;
}

#toggle-menu {
  background: #fafafa;
  border:1px solid #eee;
  border-radius:4px;
  cursor:pointer;
  padding: 4px;
  display:inline-block;
}
<div id="toggle-menu">Toggle Menu</div>
<div id="menu">
Your content in here
</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

If you position the menu in the center as required and cover it with two 'curtains', one on the left and one on the right, you can shrink them both down to 0 width to reveal the menu from the center outwards.

This snippet does this by introducing before and after pseudo elements which have the same background color as the surroundings (white in this instance) with width 50% of the menu width and before anchored on the left and after on the right.

#menu {
  height: 70%;
  position: fixed;
  z-index: 1;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
  background-color: white;
  overflow-x: hidden;
  padding-top: 70px;
  text-align: center;
}

#menu::before,
#menu::after {
  content: '';
  background: white;
  width: 50%;
  height: 100%;
  top: 0;
  position: absolute;
  transition: width .2s linear;
  display: inline-block;
  z-index: 1;
}

#menu::before {
  left: 0;
}

#menu::after {
  right: 0;
}

#menu.expand::before,
#menu.expand::after {
  width: 0;
}
<button onclick="document.querySelector('#menu').classList.toggle('expand')">CLICK ME</button>
<div id="menu">
  <div>Item</div>
  <div>Item</div>
  <div>Item</div>
  <div>Item</div>
  <div>Item</div>
</div>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda