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

Removing the gridlines in a Linechart using Apexcharts

I am experiencing issues with the grid and Nodata options on my Apexchart line chart. noData: { text: none , align: 'center', verticalAlign: 'middle', offsetX: 0, offsetY: 0, style: { color: undefined, fontSize: &apo ...

Customizing the initial page layout in Elm

I am new to Elm and I need help with a particular issue. Can someone provide guidance or direct me to a useful resource for solving this problem? The challenge I’m facing involves editing the start page of a website by removing specific elements, as list ...

Pressing ctrl+s will submit TinyMCE+JEditable

Updated: June 8th, 2011 Please check out the solution provided in the Answer section below. Updated: May 5th, 2011 I am currently facing a challenge where I need to trigger the JEditable submit button after hitting Ctrl+S. Thariama has already demonstr ...

Using JQuery to implement draggable text on an image

Looking for a solution that allows users to easily add a watermark text anywhere on an image. I'm envisioning a movable text box functionality where they can freely position the text and change its content. Any suggestions for a JQuery plugin that can ...

The timing of the RequestAnimationFrame function varies based on the dimensions of my canvas

In my application, I have a canvas that dynamically adjusts its CSS size based on the window size. The main gameplay loop in my code looks like this: run = function(){ console.log(timerDiff(frameTime)); game.inputManage(); game.logics(); ...

Dynamically adjust the label position based on the button position

I am currently developing an interface that allows users to change the position of buttons and add a label text under the button. However, I am facing an issue where the label is not always centered below the button due to variations in the size and number ...

Updating the value of a localStorage key after each successful AJAX call in JavaScript

I'm currently facing a challenge with my local storage key value. The issue is that it doesn't update the value when the AJAX call successfully returns data. It only retains the old stored data, requiring me to manually refresh the page by pressi ...

The utilization of media within the meta in Next.js is not allowed

Recently, I came across an interesting article about PWAs on web.dev. The article discusses color schemes and at one point, they talk about it in detail, I'm currently working with Next.js and decided to try implementing the following code snippet: & ...

Ways to display a jQuery datepicker when a button is clicked

I'm looking to display a datepicker only when a button is clicked <input type="button" id=day value="day" /> $("#day").datepicker(); The code above will populate the button text with the selected date I also attempted the following code, but ...

ng-deep is causing changes in sibling components

Facing a challenge with Angular Material Design as I discovered the need to utilize ng-deep to customize the styling of an accordion. The issue is that when I use this method, it affects another accordion in a different section which is undesirable. Is th ...

Button will be disabled unless a value is selected in the dropdown menu

I am currently dealing with a code issue where the button is disabled on page load when the dropdown value is empty. However, even after selecting a value from the populated database dropdown, the button remains disabled. Jquery: <script> $(doc ...

In a two-column layout, ensure that the text in the left box is aligned to the left and the text in the right box is aligned to the

I'm working on a flexbox layout with two columns, each containing text. The text in the left column is aligned to the left, while the text in the right column is aligned to the right. However, I want the text in the right column to switch to left al ...

In jQuery, you can arrange an array in order and retrieve the first key

I have an array with key-value pairs and want to sort it by value, then retrieve the key of the first value. var obj = { fruit:5, vegetable:3, meat:7 }; var tempArr = [ ]; $.each(obj, function(key, value) { tempArr.push({k: key , v:value}); }); tempArr.s ...

What is the best way to incorporate multiple Bootstrap templates into an Angular project without causing conflicts between them?

When incorporating a bootstrap template into the admin interface, it is important to consider that its design may vary from the template used for visitors. Mixing multiple styles based on different templates can lead to conflicting styles and can potenti ...

Rotate background image upon each visit

I have a collection of around 50 background images that I want to display randomly each time a user visits my website. The idea is to provide a unique background image for every visit, without saving any information about which image was previously display ...

Tips for creating a fixed sidebar menu that is aligned to the right side of the page

I've been working on this for quite some time now and I'm trying to figure out how to keep a side menu in place, while having it float on the right side. Every time I try using the fixed element, it ends up moving my sidebar from right to left. A ...

Node JS does not receive a response from JQuery Ajax

I have developed a form on the client side which includes: <html> <body> <script> $(document).ready(function() { $.ajax({ url: "Search.html", type: "POST", dataType : "json", s ...

Managing the "Accept Cookies" notification using Selenium in JavaScript

As I use Selenium to log in and send messages on a specific website, I am faced with a cookie popup that appears each time. Unfortunately, clicking the accept button to proceed seems to be quite challenging for me. Here is an image of the cookie popup Th ...

Dynamically create a button using jQuery and JSON based on specific conditions (either true or false)

Is there a way to generate buttons with specified parameters only if a condition is true? Currently, the buttons are generated without checking conditions. Any help with the correct syntax would be appreciated. Sample JSON code: { "Caption": "Module ...

Implementing array values into a jQuery graph syntax

After json encoding from php, I have an array that contains information on different categories: [{"Type":"Category","Name":"games","TotalClicks":"162"},{"Type":"Category","Name":"apps","TotalClicks":"29"},{"Type":"Category","Name":"music","TotalClicks":" ...