navigation bar: retain link hover state even after displaying sub-menu

My navigation bar has submenus with background images in .png format. Only 3 links in my navbar have these submenus. The issue I am facing is that when the menu link is in a hover state and the submenu is being hovered over, the nav link loses its hover state. How can I create a navbar with a custom background on the submenu while keeping the link in a hover state? I tried achieving this using div elements, but it's proving to be difficult with unordered lists.

Note: Below my links, when hovered, an arrow pointing down appears

<ul class="menu">
    <li><a href="#" id="empresa" class="">empresa</a>
        <div class="sub-empresa">
            <ul class="menu-empresa">
                <li><a href="historia.html">história</a></li>
                <li class="separador"></li>
                <li><a href="politica-privacidade.html">política de privacidade</a></li>
                <li class="separador"></li>
                <li><a href="#">certificações e prêmios</a></li>
            </ul>
        </div>
    </li>
    <li class="pipe"></li>
    <li><a href="produtos.html">produtos</a></li>
    <li class="pipe"></li>
    <li><a href="representantes.html">representantes</a></li>
    <li class="pipe"></li>
    <li><a href="distribuidores.html">distribuidores</a></li>
    <li class="pipe"></li>
    <li><a href="#" id="infotec">informativos tecnicos</a>
        <div class="sub-informativo-tec">
            <ul class="menu-infotec">
                <li><a href="#">generalidades</a></li>
                <li class="separador"></li>
                <li><a href="#">recomendações para instalações</a></li>
                <li class="separador"></li>
                <li><a href="#">conceitos básicos sobre condutores</a></li>
                <li class="separador"></li>
                <li><a href="#">simbologia</a></li>
                <li class="separador"></li>
                <li><a href="#">critério de dimensionamento de circuitos</a></li>
                <li class="separador"></li>
                <li><a href="#">queda de tensão</a></li>
                <li class="separador"></li>
                <li><a href="#">dimencionamento de eletrodutos</a></li>
                <li class="separador"></li>
                <li><a href="#">correntes máximas de curto-circuito</a></li>
            </ul>
        </div>
    </li>
    <li class="pipe"></li>
    <li><a href="#">eventos</a></li>
    <li class="pipe"></li>
    <li><a href="#">videos</a></li>
    <li class="pipe"></li>
    <li><a href="#" id="contato">fale conosco</a>
        <div class="sub-fale-conosco">
            <ul class="menu-faleconosco">
                <li><a href="fale-conosco.html">fale conosco</a></li>
                <li class="separador"></li>
                <li><a href="trabalhe-conosco.html">trabalhe conosco</a></li>
                <li class="separador"></li>
                <li><a href="#">seja nosso representante</a></li>
                <li class="separador"></li>
                <li><a href="#">seja nosso distribuidor</a></li>
            </ul>
        </div>
    </li>
</ul><!-- /.menu -->


//menu do topo
$("#empresa").mouseover(function() {
    $(".sub-empresa").slideDown(199);
});

$(".sub-empresa").mouseleave(function() {
    $(".sub-empresa").fadeOut("slow", function() {
        $(".sub-empresa").css("display","none");
    });
});

$("#infotec").mouseover(function() {
        $(".sub-informativo-tec").slideDown(199);
});
$(".sub-informativo-tec").mouseleave(function() {
        $(".sub-informativo-tec").fadeOut("slow", function() {
        $(".sub-informativo-tec").css("display", "none");
    });
});

$("#contato").mouseover(function() {
    $(".sub-fale-conosco").slideDown(199);
});
$(".sub-fale-conosco").mouseleave(function() {
        $(".sub-fale-conosco").fadeOut("slow", function() {
        $(".sub-fale-conosco").css("display", "none");
    });
});

$("body").click(function(){
    $(".sub-empresa, .sub-informativo-tec, .sub-fale-conosco").fadeOut("slow");
});

Answer №1

It appears that the div containing the submenu is unnecessary.

A simpler solution could be:

$('.menu ul').each(function () {
    $(this).parent().eq(0).hover(function () {
        $(this).addClass('on');//for styling
        $('ul:eq(0)', this).slideDown(200);
    }, function () {
        $(this).removeClass('on');//for styling
        $('ul:eq(0)', this).stop(true, true).slideUp(400);
    });
});

I improvised some CSS since I'm not sure what your current styles are.

Check out the fiddle here: http://jsfiddle.net/filever10/8xu5m/

Answer №2

If you're interested in learning more, check out this detailed tutorial:

This tutorial provides a step-by-step guide on creating a sleek dropdown menu using only CSS and unordered lists. It should help you effectively address the issue you're facing.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Filter a nested array in AngularJS and retrieve the parent object as the result

I've recently started experimenting with AngularJS to create checkbox filters for my wine store. Here is an example of an item in my store: { "id": 17, "name": "Ermelinda Freitas Reserva", "tag_ids": [40, 12, 56, 6, 60], " ...

I am in need of a customized 'container' template that will display MyComponent based on a specific condition known as 'externalCondition'. MyComponent includes the usage of a Form and formValidation functionalities

container.html <div ngIf="externalCondition"> <!--Initially this is false. Later became true --!> <my-component #MyComponentElem > </my-component> <button [disabled]= "!myComponentElemRef.myDetailsF ...

unable to receive the data transmitted by the socket.io client

I hit a roadblock while trying to follow the tutorial on socket.io. I'm currently stuck on emitting events. Previously, I successfully received the console logs for user connected and user disconnected. However, when it comes to emitting messages, I a ...

Ways to retrieve the baseURL of an axios instance

This question is being posted to provide an easy solution for fellow developers who may be looking for an answer. //Suppose you have an axios instance declared in a module called api.js like this: var axios = require('axios'); var axiosInstance ...

Troubleshooting minified JavaScript in live environment

Trying to wrap my head around AngularJS and Grunt. In my GruntFile.js, I have set up two grunt tasks for development and production. In production, I am using uglification to combine multiple js files into one. I am in need of some advice or tips on how t ...

What is the best way to continuously verify login and password credentials?

I am a newcomer to this industry and am facing a challenge with testing the process of entering the correct username and password using Selenium Webdriver. The current method of creating this process without a loop is extremely lengthy and monotonous. Here ...

Filtering nested arrays in Javascript involves iterating through each nested

I have a nested array inside an array of objects in my Angular app that I'm attempting to filter. Here is a snippet of the component code: var teams = [ { name: 'Team1', members: [{ name: 'm1' }, { name: 'm2' }, { name ...

What exactly is the purpose of the script type importmap?

Can you explain the role of <script type="importmap"> and why it has become necessary for my code to function properly? <script type="importmap"> { "imports": { "three": "http ...

Tips for enhancing the functionality of components within Vue

My expertise lies primarily in React, and I'm now exploring the Vue-centric approach to achieve the following: I want to enhance this component: , so that the label-position is set to top on mobile devices and left on desktop. However, I'm not s ...

Is there a way to "stream" a specific part of my website to a separate window?

Is there a way to stream a specific container from my webpage to another window? The scenario is similar to a Point of Sale system, where there is an operator-facing display and a second customer-facing display. The operator-facing display will show all ...

Execute script when on a specific webpage AND navigating away from another specific webpage

I created a CSS code that adds a fade-in effect to the title of my website, and it works perfectly. However, I now want to customize the fade-in and fade-out effect based on the page I am transitioning from. My desired outcome: If I am leaving the "Biolo ...

Obtaining the Span value inside a DIV when the text in the DIV is selected - JavaScript

I am dealing with a series of div elements structured in the following way: <div id="main1"> <span class="username">Username_1</span> white some text content inside div... blue some text content inside div... yellow some ...

Using a variable to contain a loop in Jquery

Seeking assistance with a jQuery function that retrieves values from a PHP form to create a variable for an ajax call. Is it possible to include a loop within a variable? Below is a snippet of my code to provide better context: ... var teacher_ids = $( & ...

"Utilizing Material-UI in React to create a textfield input with number validation

I am currently working on an input field that should only accept values within a specified range of min and max. However, I have encountered an issue where manually entering a number bypasses this control mechanism. Is there a way to prevent this from happ ...

Utilizing the CSS :not() selector within a nested table

I am having an issue with my AjaxControlToolkit where the main-page CSS is affecting the inner "calendar" popup. To illustrate what I need, what I've tried, and the temporary workaround, I have simplified everything into a one-page example. I am in s ...

Unable to obtain the height of a new iframe within the DOM

Upon page load, I utilize the following code to adjust the height of the iframe based on its content's height, this aspect is functioning properly. $("iframe").height($("iframe").contents().height()); I have an iframe element on my webpage which I u ...

Issues arise when dealing with a CSS and HTML accordion

I decided to create a CSS and HTML collapsing menu system, also known as an accordion. I found a tutorial on how to do it here. However, I had to make some modifications because I wanted to include images in the 'sections' later on. Instead of us ...

Using select tags in conjunction with JavaScript can add dynamic functionality to your website

I've been working on adding dropdown menus to my website and have been using code similar to this: <select> <option value="volvo">Volvo</option> <option value="saab">Saab</option> <option value="opel">Opel</opti ...

How can I achieve the quickest image loading speed with JavaScript?

If I have a large ecommerce website with 15,000 image elements that need to be added to the HTML, what is the best approach using JavaScript to optimize efficiency and enhance user experience? ...

Show or hide the sibling element of the current element without using a specific identifier in jQuery

I am looking to make div.edit-button toggle its corresponding sibling div.additionalFieldForm. There are multiple instances of both on the page, so I want to target only the pair that are related within the same group. If a div.edit-button is clicked, the ...