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

120
Visualizações
Javascript Blur background when div is block?

I'm having a problem with bluring the background when my menu is open. I tried writing something on my own but it's not working.

function backgroundBlur() {
  var menuBox = document.getElementById("mobile-menu");
  var blur = document.getElementById("body");
  if (menuBox.style.dsiplay = "block") {
    blur.style.filter = "blur(3px)";
  }
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I think the problem can be caused by three reasons:

  • Typo noted by @RyanWalls
  • The backgroundBlur() method is not called
  • The display property of the menuBox is not block

I made a run to simulate this event:

let button = document.getElementById('setBlur');
let control = false;

function backgroundBlur(control) {
  var menuBox = document.getElementById("mobile-menu");
  var blur = document.getElementById("body");
  
  if (menuBox.style.display === "block")
  {
    if(!control)
    {
      blur.style.filter = "blur(3px)";
      button.innerHTML = 'Remove Blur';
    }
    else
    {
      blur.style.filter = "blur(0px)";
      button.innerHTML = 'Add Blur';
    }
  }
}

button.addEventListener('click', function() {
    backgroundBlur(control);
    control = !control;
});
*{
  margin: 0;
}
#body{
  background-color: black;
}
button{
  margin-top: 25px;
  background-color: #4CAF50;
  border: none;
  color: white;
  padding: 15px 32px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  width: 100%;
}
.vertical-menu {
  width: 200px;
}
.vertical-menu a {
  background-color: #eee;
  color: black;
  display: block;
  padding: 12px;
  text-decoration: none;
}
.vertical-menu a:hover {
  background-color: #ccc;
}
.vertical-menu a.active {
  background-color: #04AA6D;
  color: white;
}
<div id="body">
  <div id="mobile-menu" style="display: block;">
    <div class="vertical-menu">
      <a href="#" class="active">Home</a>
      <a href="#">Link 1</a>
    </div>
  </div> 
</div>

<button id="setBlur">Add Blur</button>

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