1
I’m trying to make this script that closes the MENU when I click on any area of main
.
The MENU opens the left of the mobile page.
But as you can see MENU is out of the main
. So I couldn’t make that code with closest
by checking if any PAI element has the class menu-in
.
// Abre o Menu Mobile
$('.open-menu-mobile').on('tap click', function(){
$('main, nav.menu').addClass('menu-in');
});
nav.menu{
position: absolute;
width: 240px;
background-color: #37474f;
height: 100%;
z-index: 1;
opacity: 0;
left: -240px;
transition: all 0.3s ease;
&.menu-in{
transform: translateX(100%);
opacity: 1;
}
}
{{-- MENU --}}
<nav class="menu">
<div class="search">
Procurar
</div>
<ul>
<li><a href="#">Quem Somos</a></li>
<li><a href="#">Produtos</a></li>
<li class="logo"><a href="#"></a></li>
<li><a href="#">Contato</a></li>
<li><a href="#">Onde Estamos</a></li>
</ul>
</nav>
{{-- MAIN --}}
<main>
{{--- Cabeçalho do Site Mobile --}}
<header id="header">
{!! Html::image('img/open-menu-mobile.png', 'Abrir Menu Mobile', ['class'=>'open-menu-mobile']) !!}
<a href="{!! URL::to('/') !!}">
</a>
</header>
@yield('content')
</main>
Guys, I tweaked this code and now it’s working almost the way I need it.
// Abre o Menu Mobile
$('.open-menu-mobile').on('tap click', function(e){
$('main, nav.menu').stop(true, true).addClass('menu-in');
e.stopPropagation();
});
// Clicar em alguma área do site e fechar MENU
$(document).on('tap click', function(e){
if(!$(this).hasClass('.open-menu-mobile'))
$("main, nav.menu").removeClass('menu-in');
});
What happens now is this:
Inside my MENU, have LINKS and a Search field. When I click on these places, the MENU closes too. I thought using the stopPropagation()
would solve this.
If you already know the name of the class and I believe that it is unique, because you need to use the
closest
? You can’t look for her directly?$(".menu-in")
?– user3603
It is not unique. See function, jQuery. I apply it in two places.
– Diego Souza