CSS and Javascript interactivity problem with submenus in the navigation

Hey there! I'm currently working on designing a horizontal menu bar for a website project. The goal is to have top level items with sub menus that appear when you hover over them, while maintaining proper justification.

So far, everything is coming along smoothly - the color changes and visibility of the sub menus are all working as intended. However, I've encountered an issue where once a top level item is clicked and the user is taken to the corresponding page, the sub menu for that specific item should remain visible until hovering over another top level item due to space constraints.

I've been experimenting with CSS alone to achieve this functionality but it seems quite challenging. This has led me to explore using jQuery to handle the dynamic hiding/showing of the active sub menu. Unfortunately, I've hit a roadblock with this approach as well. It seems like I may have made things more complicated than necessary!

If you'd like to take a look at what I mean, I've created a sample on JSFiddle:

http://jsfiddle.net/vLZv7/

This scenario mimics selecting the first top level item and being redirected to "parent1.html".

Any assistance or pointers in the right direction would be greatly appreciated.

Here's a snippet of my code:

<div class='menu'>
<ul>
    <li class='active'><a href="#">Parent 1</a>
    <ul>
        <li><a href="#">Sub 1-1</a></li>
        <li><a href="#">Sub 1-2</a></li>
        <li><a href="#">Sub 1-3</a></li>
    </ul>
    </li>
    <li><a href="what-we-do.php">Parent 2</a>
    <ul>
        <li><a href="#">Sub 2-1</a></li>
        <li><a href="#">Sub 2-2</a></li>
        <li><a href="#">Sub 2-3</a></li>
    </ul>
    </li>
    <li><a href="#">Parent 3</a>
    <ul>
        <li><a href="#">Sub 3-1</a></li>
        <li><a href="#">Sub 3-1</a></li>
    </ul>
    </li>
</ul>
</div>

CSS Styles:

.menu ul{
    text-align:justify;
    width:300px;
    min-width:500px;
    margin-top:60px;
}
.menu ul:after{
    content:'';
    display:inline-block;
    width:100%;
}
/* Additional CSS rules go here */

<h4>Just a Simple jQuery Function:</h4>

<pre><code>$('.menu').hover(
function(){ $('.menu li.active ul li a').addClass('hide') },
function(){ $('.menu li.active ul li a').removeClass('hide') }
)

Answer №1

Not any easier than

$('.menu li:not(.active)').hover(
    function(){ $('.menu li.active ul li a').addClass('hide') },
    function(){ $('.menu li.active ul li a').removeClass('hide') }
)

Answer №2

To modify the element that triggers the event, adjust the selector to target li elements like so:

$('.nav li').hover( ......

Next, use the unbind() method to remove the hover action for items with the class of .current:

$('.nav li.current').unbind('mouseenter mouseleave');

http://jsfiddle.net/pR45t/3/

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

CSS clearfix restricted to immediate parent div

Below is the code snippet that illustrates the objective I am attempting to achieve <div> <div style="float:left; width:220px; height:300px; border: 1px solid #aaa"> Left div <br>float:left <br> fixed width 220px </div> ...

CSS - Help! Seeing different results on Internet Explorer

I'm currently handling an OSCommerce website, and I'm trying to figure out why this issue is occurring. You can view the website at this link: The layout looks completely different on Internet Explorer, and I'm puzzled as everything should ...

Ways to extract the data-id attribute's value

I am attempting to locate each data-id and then display them on the console. I experimented with the following: $('.raffle-user-entries .raffle-user-entry').each(function() { console.log($(this).find('raffle-use ...

Master the art of shifting focus from one div to another by simply pressing the arrow key

Looking for a way to navigate between divs using arrow keys? Check out my div structure below: <div id="InsertDiv"> <div draggable="true" onclick="openPropDyn(event)" data-serial="6018" ondragstart="drag(event,6018)" class="subtaskDetailDiv ...

Tips for excluding a CSS property from a class override

I have a file called _buttons.scss: .btn { display: inline-block; font-family: $btn-font-family; font-weight: $btn-font-weight; color: $body-color; text-align: center; text-decoration: if($link-decoration == none, null, none); white-space: $b ...

What is the best way to create a reset button for a timing device?

Is there a way to reset the timer when a button is clicked? Having a reset button would allow users to revisit the timer multiple times without it displaying the combined time from previous uses. Check out this code snippet for one of the timers along wit ...

Need assistance with CSS layout: How to extend a div to the bottom of the page

I am currently working on a design layout that includes a 'header' section with a logo and links, as well as a content area that should extend to the bottom of the page. However, I am facing some challenges in achieving this. Initially, I enclos ...

The two items are not situated side by side

Trying to make my page responsive, but when I inspect and look into mobile view, the elements do not align next to each other like they do on desktop. https://i.sstatic.net/8WhxD.png I want the shopping cart icon and button to be positioned next to each o ...

`Trick Ajax into displaying 200 status code even on error responses`

Is it possible to suppress exceptions in the console by manually setting the status code to 200 for all errors? For example, can I change a 500 internal server error response to 200? Although I understand that hard-coding status codes is generally conside ...

How CSS can affect the layout of elements on a webpage

I am looking to achieve something similar to this: The left box should maintain a fixed width, while the right one resizes to fit the full width of the browser window. I also need the height of both boxes to be resizable. Apologies for any inconvenience ...

Issue with html2canvas image download in Firefox

I am currently using HTML2Canvas to try and download a div as an image. It works perfectly fine on Google Chrome, but I am experiencing issues with Firefox. Below is the code snippet: <script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0. ...

Close button for body

I have created a form that floats in the center of the screen On this form, I have included a button designed to close it However, I would like for the form to be closed anywhere on the screen when clicked with the mouse Here is my code: $(".offer-clo ...

Converting Excel Files from ".xls" to ".xlsx" Using PHP

Currently, I am working on a PHP project which involves sending an email with an Excel attachment. To handle the email functionality, I am utilizing PHPMailer. However, I am encountering an issue with the format of the Excel file generated by the code belo ...

Tips for adjusting the background and text color of Bootstrap dropdowns using CSS

I've been experimenting with different approaches to modify the background color and text color of the dropdown menu (dropdown-menu & dropdown-item), but I haven't had any success so far. Currently, the dropdown has a very light grey background c ...

jQuery fade-in messing with Hashtag linking functionality

Hey there, I've got a cool hide/fadeIn effect going on my page with jQuery. Everything was running smoothly until I noticed that using delay() + fadeIn() is causing my <a href="http://example.com/my_page/#my_ID">Hashtag Links</a> to load ...

CSS animation for dynamic zoom in and out effect inspired by Apple's slideshow feature

I really love the smooth auto zoom in and out effect on this website: , especially for the banner images at the top. I tried to recreate it by inspecting the developer tools in my browser, but I'm having trouble implementing it as some properties are ...

DIVs Not Aligning Correctly

I'm attempting to position two divs next to each other. The first item should have an image on the left and text on the right. The second item should have text on the left and an image on the right. And for the third item, there should be an image on ...

Using innerHTML in PHP to create a dynamic two-step echo effect

step 1: I am creating a form where input fields will be dynamically generated using innerHTML. var d = document.getElementById("d1p_1"); d.innerHTML += "<input class='add' name='field_" + i + "' type='text'>"; step 2: ...

transforming the jquery form submission to angular

I am facing a challenge trying to convert this jquery code into Angular, and I am having trouble making it work. // jquery $('#formsubmit').click(function(ev){ ev.preventDefault(); $.ajax({ url: "/url.json", type: "POST", data: { ...

Secure your online file with a password

I need to access a specific file on my computer through a program, not a web browser. However, I want to ensure that only authorized users can view the file by requiring a password with the request. How can I set up the file to prompt for a password befor ...