"Enhance Your Slide Up/Down Menu with a Unique Hover Effect in jQuery

HTML:

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="navigation_desktop">

            <div class="button_desktop_01">1.0 Main Menu
                <div class="SlideItem_01">
                    <div>1.1 Sub Menu</div>
                    <div class="button_desktop_02">1.2 Sub Menu
                        <div class="SlideItem_02">
                            <div>
                                <div>1.2.1 Sub Menu</div>
                                <div>1.2.2 Sub Menu</div>
                            </div> 
                        </div>
                    </div> 
                </div>
            </div>

            <div class="button_desktop_01">2.0 Main Menu
                <div class="SlideItem_01">
                    <div class="button_desktop_02">2.1 Sub Menu
                        <div class="SlideItem_02">
                        <div>
                                <div>2.1.1 Sub Menu</div>
                                <div>2.1.2 Sub Menu</div>
                                <div>2.1.3 Sub Menu</div>
                            </div> 
                        </div>
                    </div>  
                    <div>2.2 Sub Menu</div>
                </div>
            </div>  

        </div>

CSS:

 .button_desktop_01{
 float: left;
 width: 20%;
 cursor: pointer;
 background: #5882FA;
 }

 .button_desktop_02 {
 position: relative;
 cursor: pointer;
 }

.SlideItem_01, .SlideItem_02 {
 display: none;
}

.SlideItem_02 {
 position: absolute;
 top: 0;
 left: 100%;
 width: 100%;
     background: #5882FA;
    background-color:#FFD700;
    }

 .button_desktop_01:hover, .button_desktop_02:hover,
 .button_desktop_01:active, .button_desktop_02:active,
 .button_desktop_01:link, .button_desktop_02:link,
 .button_desktop_01:visited, .button_desktop_02:visited {
 background: #CCCCCC;
 text-shadow: none;
}

jQuery:

$(document).ready(function() {
  $(".button_desktop_01, .button_desktop_02").mouseenter(function() {
    $(this).children(".SlideItem_01, .SlideItem_02").slideDown(500);
  });
  $(".button_desktop_01, .button_desktop_02").mouseleave(function() {
    $(this).children(".SlideItem_01, .SlideItem_02").slideUp(500);
  });
});

The code above slideUp/Down some menus. All this works good so far. However, now I want that each Sub Menu changes its colour once the user is hover it. I tried different ways but I could not make it work.

For example the code above changes the colour of the entire menu and not just from the specific menu where the cursor is on. Besides, it does not cover anything that is part of SlideItem_02.

What do I have to change in my CSS to make the hover effect work?

You can also find the code here: https://jsfiddle.net/by3d0kt9/6/

Answer №1

Check out the demo here: https://jsfiddle.net/Nisarg0/by3d0kt9/7/

To make a div responsive to hover, simply add a class called button to each div you want to target, like this:

<div class="SlideItem_02">
    <div>
        <div class="button">2.1.1 Sub Menu</div>
        <div class="button">2.1.2 Sub Menu</div>
        <div class="button">2.1.3 Sub Menu</div>
    </div> 
</div>

Add CSS styles to change the appearance of the div on hover:

.button:hover{
 background: #eee;
 text-shadow: none;
 color: red;
}

While there are other methods to achieve this, using a dedicated button class for button-related tasks is a simple and clean approach, especially when dealing with multiple navigation elements.

Answer №2

Here is a solution for you: https://jsfiddle.net/by3d0kt9/11/

$(document).ready(function() {
  $(".button_desktop_01, .button_desktop_02").mouseenter(function() {
    $(this).children(".SlideItem_01, .SlideItem_02").slideDown(500);
  });
  $(".button_desktop_01, .button_desktop_02").mouseleave(function() {
    $(this).children(".SlideItem_01, .SlideItem_02").slideUp(500);
  });
  
  $(".button_desktop_01 > div > div").not('.button_desktop_02').click(function(e){
    e.stopPropagation();
  $(this).addClass('visited_once');
  });
  
  $(".button_desktop_02 > div > div div").click(function(){
  $(this).addClass('visited_once');
  });
});
 .button_desktop_01{
 float: left;
 width: 20%;
 cursor: pointer;
 background: #5882FA;
 }

 .button_desktop_02 {
 position: relative;
 cursor: pointer;
 }

.SlideItem_01, .SlideItem_02 {
 display: none;
}

.SlideItem_02 {
 position: absolute;
 top: 0;
 left: 100%;
 width: 100%;
 background: #5882FA;
}

 .button_desktop_01:hover, .button_desktop_02:hover,
 .button_desktop_01:active, .button_desktop_02:active,
 .button_desktop_01:link, .button_desktop_02:link,
 .button_desktop_01:visited, .button_desktop_02:visited {
 background: #CCCCCC;
 text-shadow: none;
}

.visited_once{
  background: #fff;
  color: #000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="navigation_desktop">

<div class="button_desktop_01">1.0 Main Menu
<div class="SlideItem_01">
<div>1.1 Sub Menu</div>
<div class="button_desktop_02">1.2 Sub Menu
<div class="SlideItem_02">
<div>
<div>1.2.1 Sub Menu</div>
<div>1.2.2 Sub Menu</div>
</div> 
</div>
</div> 
</div>
</div>

<div class="button_desktop_01">2.0 Main Menu
<div class="SlideItem_01">
<div class="button_desktop_02">2.1 Sub Menu
<div class="SlideItem_02">
  <div>
<div>2.1.1 Sub Menu</div>
<div>2.1.2 Sub Menu</div>
<div>2.1.3 Sub Menu</div>
</div> 
</div>
</div> 
<div>2.2 Sub Menu</div>
</div>
</div>
  
</div>

Code has been updated

JS

$(".button_desktop_01 > div > div").not('.button_desktop_02').click(function(e){
   e.stopPropagation();
   $(this).addClass('visited_once');
});

$(".button_desktop_02 > div > div div").click(function(){
   $(this).addClass('visited_once');
});

CSS

.visited_once{
  background: #fff;
  color: #000;
}

Color will be modified upon the click event.

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

Modifying an HTML attribute dynamically in D3 by evaluating its existing value

I'm facing a seemingly simple task, but I can't quite crack it. My web page showcases multiple bar graphs, and I'm aiming to create a button that reveals or conceals certain bars. Specifically, when toggled, I want half of the bars to vanish ...

Enhance your PrimeVue experience with the power of Tailwind CSS

After installing PrimeVue, I encountered an issue where the component style was not working, but Tailwind CSS was functioning correctly. It seems that when I install Tailwind first, it works fine until I add Primevue's button component, which then cau ...

Investigate the presence of a vertical scrollbar using JQuery or JavaScript

Within an HTML report, I have applied the style "overflow:auto" to allow for dynamic data filling. I am now facing the challenge of detecting whether a vertical scrollbar exists within the report. After scouring various forums for solutions, I came across ...

Surprising sight of a blank canvas behind the font-awesome icon

Is there a way to remove the unexpected white background that appears when adding a font-awesome icon? Here is the HTML code: <div class="notifications-window" id="close-notifications"> <div class="notifications-header&qu ...

What is the best way to dynamically update styleUrls or style properties in Angular?

One of my goals is to give users the ability to customize colors and certain styles within my Angular application. I am thinking about creating a structure like this: Structure: component-one   folder-with-css-files     style-for-component-1-fo ...

Display and conceal frequently asked questions using JQuery

I'm currently facing an issue with using JQuery to toggle between showing and hiding content when a user clicks on a specific class element. Here is my HTML code: <div class="faqSectionFirst"> Question? <p class="faqTextFirst" style=' ...

Looking to either include a div around fancybox on click or apply a class to the body

My goal is to incorporate multiple fancyboxes on a single page, each opening with either an iframe in an iPhone, iPad, or desktop browser upon clicking. Although I have successfully displayed the image on the default overlay, I am facing difficulty in add ...

Switching the displayed image depending on the JSON data received

As a beginner in javascript and jQuery, I am working on displaying JSON results in the browser. My goal is to generate dynamic HTML by incorporating the JSON data. Below is an example of the JSON structure: [{"JobName":"JobDoSomething","JobStatus":2,"JobS ...

Tips for postponing the execution of inline javascript

Main page <script> $.ajax({ type: 'GET', url: 'ajax.php', context: document.body, success: function(data) { $("#content").html(data); } }); </script> <div id="content"></div> Ajax ...

Converting a PHP timestamp to a jQuery-compatible format

Can someone help me find the equivalent function in jQuery that will give me a time format similar to this: date( 'Y-m-d\TH:i:sP'); //the output is like this. 2013-10-30T18:10:28+01:00 I am looking for this specific format in jQuery to use ...

Is Ajax a potential security risk in PHP applications?

I developed a PHP system with a page named x.php. On this page, I have created a variable called $accountid and retrieved the account ID from the session. There are other variables in PHP on the same page that call functions from another page named functi ...

I am struggling to get the pop-up to close using the current code. I suspect that the issue might be related to the variable I was previously using in WordPress. I have made changes but the pop-up

While delving deeper into the realm of Javascript, I encountered a stumbling block with a single popup intended for the main page of a WordPress website I am constructing. Despite my attempts to modify the code's variables, they seem unyielding. Surpr ...

Is it possible for iText 5 to convert PDF files into HTML format?

My latest project involved using iText 5 to generate a visually appealing report complete with tables and graphs. Now I'm curious to know if iText also has the capability to convert PDF files into HTML format. If it does, how would one go about doing ...

How to achieve smooth transitions in CSS3 with dynamic height changes?

Currently, I am in the process of designing a unique layout. The main challenge lies in toggling between two classes for a div element, one of which has a height of 0px. To achieve this, I have successfully utilized CSS3 animations that effectively scale t ...

What are the potential drawbacks of utilizing <div> spacers in web design?

Is it considered poor practice to utilize <div> elements as a means of creating space between other elements? If so, what are the reasons behind this? For instance: <div class="panel"> <!-- Content --> </div> <div class="s ...

Is there a way to retrieve the HTML code of a DOM element created through JavaScript?

I am currently using java script to generate an svg object within my html document. The code looks something like this: mySvg = document.createElementNS("http://www.w3.org/2000/svg", "svg"); myPath = document.createElementNS("http://www.w3.org/2000/svg", ...

Transmitting HTML5 application settings via URL

I am interested in creating an HTML5 app that utilizes the WebAudio API. This app will allow users to customize certain parameters. Users should be able to 'share' these settings by sending a share URL, which can be clicked by others to view the ...

"Encountering a problem with jQuery AJAX - receiving an error message despite the status code being

Attempting to make a jQuery Ajax request using the following code: $.ajax({ url: '<MY_URL>', type: 'POST', contentType: 'application/json;charset=UTF-8', data: JSON.stringify(data), dataType: 'a ...

JavaScript for rotating an element upon button click

My webpage design <div id="yabanner"> <img src="img/ok.jpg"> </div> <button>Click Me</button> <button>Click Me</button> <button>Click Me</button> My script code var button = document.getElementsBy ...

Allow users to zoom in and out on a specific section of the website similar to how it works on Google Maps

I am looking to implement a feature on my website similar to Google Maps. I want the top bar and side bars to remain fixed regardless of scrolling, whether using the normal scroll wheel or CTRL + scroll wheel. However, I would like the central part of the ...