"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

Updating the parameter names in an AJAX request

Can the data parameters sent with AJAX be dynamically changed to a new name? For instance: let NewParamName = `Post_${incremented_value}`; $.post("index.php" { NewParamName : "someValue" }); Even after sending the request, the name in the parameters rem ...

"Positioned at the top of the page is the alert box, with the

I'm looking to add an alert at the top of my webpage containing a form for visitors to enter their phone number. Currently, I have a "alert alert-info" div with the form inside it placed at the top of my body tag, which works perfectly. However, when ...

What could be causing my inline-block divs to not line up correctly?

Presented below is the code snippet of my HTML: .classWrap { text-align: center; } .classInd { display: inline-block; height: 200px; width: 200px; margin: 20px; border: 2px solid #FFF202; border-radius: 10%; box-shadow: 0 0 15px 0 #FFF2 ...

Adjust the dimensions of a box by simultaneously altering both its height and width

I am looking to create a responsive box that automatically adjusts its height when the window width is resized. I have tried using Transform:Translate() to move the picture inside, but so far, the height only changes when I manually adjust the height of ...

Tips for sending information from PHP to Javascript using jQuery?

I am looking to move data from a PHP page that pulls information from MySQL, with the goal of displaying this data on my mobile app using Cordova. I plan to achieve this using JavaScript. Here is the PHP code I currently have implemented: if($count == ...

Utilize jQuery to apply a CSS class to the element that is clicked and inject additional content into a designated div

Here is a link to a page where I am experimenting with creating an interactive choose-your-own-adventure story: My goal is to add the class "disabled" to the element when a user clicks on the word "stairs" and then continue the story. Below is the relevan ...

Central Player Component

Do you need help with centering text and a player on your WP site? Check out my website Here is the code I am currently using: <div class="listen_section"> <div class="container"> <div class="row"> <div class ...

Single quotation mishap in JSON

How can I successfully send JSON data that contains single quotes without encountering errors? I need to include values with single quotes, but how can I do this without causing any issues? $.ajax({ type: "post", url: "canliAyarlari.aspx/dosyaYa ...

Can React components receive props or data when they are inserted into regular HTML code?

I have a project where I need to make updates to an old-fashioned website. The current layout is table-based and coded by hand. My idea to streamline the process is to use React to minimize repetitive coding tasks. Specifically, I want to loop through an a ...

Managing responses from ajax requests that can handle both text and json formats

After submitting a form via Ajax and receiving a response from the servlet in either text or JSON format, I'm wondering if there is a way to handle both types of responses. I've read through the jQuery/Ajax documentation, but the JQuery Ajax page ...

Can a faulty image be cached?

Is there a way to ensure that the browser caches invalid or broken images so that when they are re-fetched, the loading time is immediate? I am particularly interested in two scenarios: 1) The first scenario involves trying to load an image from a URL co ...

Using jQuery to create a smooth transition effect between two blockquote elements

Need help with fading between two blockquotes using jquery. Looking for a continuous fade transition around 10 seconds. Is it possible to achieve this effect with jquery? ...

Ways to access the current element in the Evernote editor that corresponds to the cursor's position?

It seems that Evernote editor uses a Rich Text Editor which differs from the input or textarea tag. Therefore, I am unable to use the provided code to determine its type. Is there a way to retrieve the current element of the Evernote editor where the curso ...

Attempting to align HTML lines in a more compact manner

I can't seem to figure this out - I'm trying to stack three lines of text (one small, one big, one small) with single-spacing between them. However, no matter what I try, the larger font line always ends up with a double-space above it (for examp ...

Remove the list by conducting a comparison analysis

<html> <head> <title> Manipulating List Items Using JavaScript </title> <script type="text/javascript"> function appendNewNode(){ if(!document.getElementById) return; var newlisttext = document.changeform.newlist.val ...

I am looking to place two HTML buttons next to each other on the same line

I am trying to display two HTML buttons next to each other on the same line, with the "Submit Request(s)" button appearing first. The issue I am facing is that the top of the second button aligns with the bottom of the first one. The second button is cor ...

Smoothly reveal a border at the moment of transition

I currently have images set up as checkboxes that display a yellow border upon clicking with a transition effect. However, the transition of the border causes the images to shake and shutter slightly due to margin adjustments. To resolve this issue, I tri ...

Ways to maneuver the vehicle by using the left or right key inputs

I am a beginner with canvas and game development. I am currently working on a project where a car moves in a straight line, but I want to add functionality for the car to turn anti-clockwise when the left key is pressed, and clockwise when the right key is ...

Utilize text alignment effectively by considering language direction - left-to-right for English and right-to-left for Arabic

I am managing a community website and I am looking to customize the text direction based on the language of the posts. For English posts, I want the direction to be LTR with text-align: left) For Arabic posts, I want the direction to be RTL with text-ali ...

Switch the selected option in JQuery UI dropdown using a clickable button

I have a code snippet that is almost working. My goal is to change the selection of a JQuery dropdown select combobox using a separate button named "next". What I want is for the JQuery dropdown to automatically switch to the next selection every time I c ...