1
I am developing a site whose menu is the dropdown navbar of bootstrap 4. When I view the site on a smaller screen, the Nav is replaced by the burger button and is only "activated" when this button is clicked. However, I want it to be "disabled" when any menu item is clicked. For example, if I click on "Contact", in addition to the screen scroll to the contact part, I want the menu to go up and just stay the button up there.
<header>
<nav id="menu" class="navbar navbar-expand-lg">
<img id="icone" src="imgs/logoBranca.png" height="35" alt="Logo da Clara Quitutes & Cia">
<a id="nome" class="navbar-brand" href="#">Clara Quitutes & Cia</a>
<button class="navbar-toggler ml-auto custom-toggler" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNavDropdown">
<ul class="navbar-nav justify-content-end">
<li class="nav-item">
<a class="nav-link active" href="#inicio">Início</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Serviços</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
<a class="dropdown-item" href="servicos.html#bolos">Bolos</a>
<a class="dropdown-item" href="servicos.html#salgados">Salgados</a>
<a class="dropdown-item" href="servicos.html#bebidas">Bebidas</a>
<a class="dropdown-item" href="servicos.html#docinhos">Docinhos</a>
<a class="dropdown-item" href="servicos.html#quitutes">Quitutes</a>
<a class="dropdown-item" href="servicos.html#empratados">Empratados</a>
<a class="dropdown-item" href="servicos.html#opcoes-saudaveis">Opções Saudáveis</a>
<a class="dropdown-item" href="#">Garçonete/Copeira</a>
</div>
</li>
<li class="nav-item">
<a class="nav-link" href="#contato">Contato</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#sobre">Sobre</a>
</li>
</ul>
</div>
</nav>
</header>
I tried using this function in JS (found in Github), but it didn’t work. What’s wrong with the code?
//Função para o menu subir quando um item for clicado
$(document).ready(function(){
$(".navbar-nav li.-collapse a").click(function(event){
$(".navbar-collapse").collapse('hide');
});
});
Is there any other way I can do this? I am not using jQuery in the project and would not like to use only for this because it is too heavy, so preferably that the solution suggestion can be developed in JS or even CSS. I want the following:
Menu without clicks
Menu when I click on the burger
Then, when I click on any list item (in this case, "home", "users" or "Places"), the menu goes back to this way:
But the bootstrap is almost entirely jQuery dependent, mainly on NAVBAR, dropdown menus and Carousel.
– Guilherme Nascimento
It’s okay to send me a suggestion with jQuery. I just PREFER it to be with css or javascript.
– Rafaela Lopes
but the problem is not in my html or css. To using a bootstrap navbar. If you go here: https://getbootstrap.com/docs/4.1/components/navbar/ and test any navbar as mobile, by clicking on the burger menu and then on an item of it, you will see that it does not close. I want to know how I can change that.
– Rafaela Lopes