The main menu vanishes when you hover over it after the affix class is activated

I am currently working on creating a horizontal dropdown menu that expands on mouseover and collapses on mouseout under one of the main menu items. The functionality of this effect is working fine, except for one issue. When I scroll down and activate the affix top class, if I hover over the main menu item with the sub-menu, the main menu disappears. Interestingly, this problem only occurs when hovering over the parent div of the sub-menu divs and when the affix top class is active due to scrolling down. However, this issue does not arise when hovering over other main menu items and the affix top class functions properly in those cases. Once you scroll back up, the problem disappears. Any assistance or tips on how to resolve this would be greatly appreciated. Below are the codes I am currently using:

$(document).ready(function(){

var parent = document.getElementById("parent");
var dropdown = document.getElementById("dropdown");
var child = document.getElementById("child")

parent.addEventListener("mouseover", expand);
parent.addEventListener("mouseout", close);
dropdown.addEventListener("mouseover", expand);
dropdown.addEventListener("mouseout", close);

  function expand() {
    parent.style.background = "#aaa";
    parent.style.color = "#fff";
    dropdown.style.background = "#aaa";
    dropdown.style.display = "inline-block";
    mainnav.className = "expand";
  }

  function close() {
    dropdown.style.display = "none";
    parent.style.background = "#817f7f";
    mainnav.className = "";
    
      }
})
#mainnav {
  background: rgb(129, 127, 127);
  color: #333;
  font-weight: bold;
  text-transform:uppercase;
  letter-spacing: .05em;
  z-index: 1000;
  height: 47px;
  transition: .5s ease-in-out;
}
#mainnav.expand {
  height: 94px;
  transition: .5s ease-in-out;
}
#mainnav #menu {
  display: none;
  padding: .8em 1.5em;
  cursor: pointer;
  overflow:hidden; 
}
#mainnav ul {
  display: block;
  margin: 0;
  text-align: right;
}
#mainnav ul li {
  margin: 0;
  list-style: none;
  display: inline-block;
}
#mainnav ul li a {
  color: #fff;
  font-size: .75em;
  text-decoration: none;
  display: inline-block;
  padding: .9em 1.5em .8em 1.5em;  
}

#dropdown > ul li a {
  color: #fff;
  font-size: .75em;
  text-decoration: none;
  display: inline-block;
  padding: .9em 1.5em .8em 1.5em;  
}
#mainnav ul li a:hover {
  color: #000;
  background: #aaa;  
  transition: .5s ease-in-out;
}
#mainnav #child{
  width: 100vw !important;
  margin-left: calc((100% - 100vw) / 2);
  display: block;
  background-color: #aaa;
  text-align: center;
  color:#fff;  
  transition: 1s ease-in-out;
}   
 #mainnav #dropdown {
  display: none;
  overflow:hidden !important;
  height: 100%;
}  


/* STICKY ON SCROLL NAV */

nav.affix {
  top: 0 !important;
  left: 0;
  right: 0;
width: 100% !important;
} 
<nav id="mainnav" class="group" data-spy = "affix", data-offset-top="100">
          <div class="container">
            <div id="menu">&#x2261; Menu</div>
              <ul class="menu">
                  <li><a href="#">1111</a></li>
                  <li><a href="#">2222</a></li>
                  <li id="parent"><a href="#">3333</a></li>
                  <li><a href="#">4444</a></li>
                  <li><a href="#">5555</a></li>
              </ul>
              <div id="child">
                  <ul id="dropdown">
                    <li class="child"><a href="#">A</a></li>
                    <li class="child"><a href="#">B</a></li>
                    <li class="child"><a href="#">C</a></li>
                    <li class="child"><a href="#">D</a></li>
                    <li class="child"><a href="#">ORM</a></li>
                    <li class="child"><a href="#">E</a></li>
                </ul>
              </div>
            </div>
        </nav>

Answer №1

It seems like you were attempting to create a horizontal drop-down menu with sub-menu items. If that's the case, here is a simplified code suggestion:

HTML:

  <div class="dropdown">
    <button class="dropbtn">1111 
      <i class="fa fa-caret-down"></i>
    </button>
    <div class="dropdown-content">
      <a href="#">Link 1</a>
      <a href="#">Link 2</a>
      <a href="#">Link 3</a>
    </div>
  </div>

  <div class="dropdown">
    <button class="dropbtn">2222 
      <i class="fa fa-caret-down"></i>
    </button>
    <div class="dropdown-content">
      <a href="#">Link 4</a>
      <a href="#">Link 5</a>
      <a href="#">Link 6</a>
    </div>
  </div>

  <div class="dropdown">
    <button class="dropbtn">3333 
      <i class="fa fa-caret-down"></i>
    </button>
    <div class="dropdown-content">
      <a href="#">Link 7</a>
      <a href="#">Link 8</a>
      <a href="#">Link 9</a>
    </div>
  </div>

</div>

CSS:

.navbar {
  overflow: hidden;
  background-color: #333;
}

.navbar a {
  float: left;
  font-size: 16px;
  color: white;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}

.dropdown {
  float: left;
  overflow: hidden;
}

.dropdown .dropbtn {
  font-size: 16px;  
  border: none;
  outline: none;
  color: white;
  padding: 14px 16px;
  background-color: inherit;
  font-family: inherit;
  margin: 0;
}

.navbar a:hover, .dropdown:hover .dropbtn {
  background-color: red;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 1;
}

.dropdown-content a {
  float: none;
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
}

.dropdown-content a:hover {
  background-color: #ddd;
}

.dropdown:hover .dropdown-content {
  display: block;
}

If this aligns with what you intended, I hope it proves helpful!

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

The CSS syntax definition for list styles

Inside the body of my site is this code: Here is the HTML: <ul id="navlist"> <li class="first"> <a href="/" id="current">Home</a> </li> <li> <a href="/Store/">Store</a> </ ...

MUI: Issue with pseudo element appearing cropped outside of Paper container

I am facing an issue where a red arrow pseudo element '::before' is partially cut off outside its container '.MuiPaper-root'. I need the arrow to remain visible, any suggestions on how to fix this? Here is the relevant code snippet and ...

Tips for marking a textarea as mandatory when a choice is made

I'm creating an .html document for conducting a complete system check and confirming various aspects of its functionality. In this page, there is a table within a form structure where each row represents a step in the system verification process (e.g. ...

What is the best method to transfer and incorporate essays and information onto my HTML page?

Here are some unique events and observances for the month of December: DECEMBER 1 World AIDS Day National Pie Day National Eat a Red Apple Day Bifocals at the Monitor Liberation Day Day With(out) Art Day Rosa Parks Day DECEMBER 2 International Day for th ...

Using Django to pass context data in a JsonResponse

Currently working on a webpage that incorporates filters to refine the displayed results. Upon triggering an Ajax call, the filters are transmitted to the Django backend for processing. The filtered data is then expected to be returned to the front-end. T ...

Utilizing long-polling AJAX to connect with a REST API and Memcached in a PHP-based system

Instead of throwing a bunch of buzzwords into my question title, let's get straight to the point. In my PHP application, I am regularly making REST requests through cURL to various webservices. Unfortunately, these requests are experiencing severe la ...

Using a restricted set of special characters in a jQuery regular expression

My requirement is to only allow alphanumeric data along with the following special characters: ' (single quote) - (hyphen) . (dot) single space I attempted this approach: var userinput = $(this).val(); var pattern = [A-Za-z0-9_~\-!@#\$% ...

Two-column layout with consistent height vertically, but varying heights on individual rows

By using JavaScript, I am able to ensure that two columns have equal height. The left column contains user input which can vary in length, causing some columns to have more content than others. My current issue is that there are multiple rows instead of ju ...

Both the maxlenght and ng-maxlength directives appear to be ineffective in AngularJS

In my HTML file, I have the following input: <input name="password" id="newPasswordConfirmation" ng-model="newPasswordConfirmation" type="number" inputmode="numeric" placeholder="" required ...

Tips for updating the chosen value with jquery or javascript

I am facing a situation where I need to change the selected option in a dropdown menu using a function triggered onClick later in the application. <select id="myselect"> <option value=aa>aa</option> <option value=bb>bb</option&g ...

Looking to display an alert message upon scrolling down a specific division element in HTML

In the midst of the body tag, I have a div element. <div id="place"> </div> I'm trying to achieve a scenario where upon scrolling down and encountering the div with the ID "place", an alert is displayed. My current approach involves che ...

Concealing overflow in a sticky container when a fixed child element is present

I am currently working on a website where I have implemented slide-up section panels using position: sticky while scrolling. However, I am encountering issues with fixed elements within the sticky panels not properly hiding outside of the respective sectio ...

How does the size of a font affect the amount of screen space a character occupies in pixels?

I am currently working on a custom user interface that requires precise measurement of how many pixels are taken up by a specific string of text. My attempts to control the character size using the font-size property have been met with unexpected results. ...

The functionality of Javascript Regular Expressions is not performing as expected

I am currently facing an issue with email validation in JavaScript as it doesn't seem to be working properly. PROBLEM: Even when entering a VALID email address, the alert still shows that my address is faulty... What could I possibly be missing he ...

Issue with FullPage.js scrollOverflow feature not properly accommodating loaded content

I am currently working on a full-page website and have opted to utilize the Fullpage.js plugin. However, I seem to be facing some challenges when it comes to loading content through an AJAX request. The pages are being populated with JSON content, but for ...

Ways to conceal numerous objects

I need to create a function where I can hide multiple elements by pressing a button based on the value of checkboxes. If a checkbox is checked, the corresponding element should be hidden. Currently, I have some code that only works for the first element: ...

Switching button appearance when hovered over

Looking to create a page layout with 4 buttons, each occupying one quadrant of the page? These buttons should feature images as their background and change to different images when hovered over. Wondering how this can be achieved using CSS? ...

What is the best way to save a current HTML element for later use?

Here is a simple HTML code that I would like to save the entire div with the class test_area and then replicate it when needed. Currently, my goal is to duplicate this div and place the clone underneath the original element. How can I achieve this? Unfortu ...

The returned JSON object lacks a specified name

After receiving the JSON data from the server, I noticed an unnamed node within the 'someStuff' object: { "someStuff": { "": { "foo": 0 }, "moreStuff": { "foo": 2 } } } This raises ...

Tips for Retrieving Html Element Attributes Using AngularJS

Update: Even though the discussion veered off track, the main question still stands - how can I access an attribute of an HTML element within an Angular controller? Check out my attempt on Plnkr: http://plnkr.co/edit/0VMeFAMEnc0XeQWJiLHm?p=preview // ...