Hover over menu and display submenus

Asked

Viewed 1,521 times

1

I’m having the following problem in my system.

I put a css in the menus, where I pass the mouse over the menus and displays the submenus linked to them.

CSS

ul.nav li.dropdown:hover
{ 
    background-color: #0c7cd5;
}

ul.nav li.dropdown:hover ul.dropdown-menu
{ 
    display: block; 
}


my problem is this, when I click on a submenu it gets fixed, however when I move the mouse on another menu, it displays but does not hide in which I clicked, follows the image below:

inserir a descrição da imagem aqui


Someone could help me ?

  • You have to post the menu code so we can analyze a likely solution

1 answer

0


you can use JQUERY on focusout, on focusin or Blur with the following function :

   $("#id-do-componente").on("focusout",function() {

          $('#id-do-componente-a-remover').addClass("id-da-classe");

    }

That is when your main component loses its focus that would be the center you will add the class referring to the formatting that hides it. You can create in CSS for example and apply in the div class a class.

<div class="menu-remove">
</div>

in css :

.menu-remove{
  display:none
}

in which case our jquery function would be :

$("#id-do-componente").on("focusout",function() {

              $('#id-do-componente-a-remover').addClass("menu-remove");

        }

The focusin works if the component "gains attention", ie a mouse click or a tab to it, already the Blur is when you lose any focus, the structure would only change to this form :

$("#id-do-componente").blur(function() {

                  $('#id-do-componente-a-remover').addClass("menu-remove");

            }

Code Example;

php/html code:

<div id="botao-centro">
   <div id="conteudo-centro">
   //Conteudo do menu do botão centro
   </div>
</div>

css code:

#botao-centro{
  //Conteudo css botao centro
}
#conteudo-centro {
 //Conteudo css menu centro
}
#conteudo-centro .menu-remove{
  display:none,
}

jquery code:

 $("#botao-centro").on("focusout",function() {

          $('#conteudo-centro').addClass("menu-remove");

    }
  • didn’t work put two ids in my less id="center" and id="user" got like this $(Document). ready(Function() { $("#user"). on("focusout", Function() { $('#center'). addClass("menu-remove"); }); });

  • In case the id-of-the-component has to be the center ID, correct, and id-of-the-component-to-remove has to be the id of the menu contents. If the menu content id is let’s assume : menu, Voce will create a css #menu . menu-remove { display:None} that is, when you add this class it stops appearing, and it will occur when the center’s Focus is lost, understood ?

  • I didn’t understand sorry, could explain me better ?

  • ha yes now I understood

  • the code didn’t work

  • i put an example code on top. Notice the Divs ids, and what was written in css, I put in css an edit for #content-center. menu-remove{} if you repair this class does not exist in php or html code, because I will only add it when the center button loses focus, that’s what jquery is doing, or so lose focus add the menu class-remove, so it takes the format of the #content-center css style. menu-remove that in the case is taking the display of all content

  • Ahh I forgot instead of using addClass in jquery use . toggleClass(), it will remove if it is added, and add if it is removed, if not you will have to stay treating the focusin and focusout to add and remove the classes. has a doc here to help you also with this matter of adding or removing classes with jquery https://tableless.com.br/manipulacao-de-classes-com-jquery/ this article below explains well what I’m trying to get you through : http://www.creatweb.com/artigos/basicos-jquery-add-and-take-class-css.html

  • even uses the mouseover and mouseout events that treat if the mouse is on top or if it exits, it will be even better to use this.

Show 3 more comments

Browser other questions tagged

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