Using jQuery to create dynamic CSS effects directly on the drop-down menu button

Seeking your kind assistance once again...

I am attempting to design a navigation bar that

  • displays the dropdown section upon hovering over the buttons AND
  • shows a bottom border on the button that was last hovered over

My code functions properly when I move my mouse

  • from menu button --> dropdown area --> outside or
  • from menu button --> menu button

However, the issue arises when I move my cursor

  • from menu button --> outside (i.e. left or right of the buttons, not below)

In this case, I wish for the border style to disappear but it remains visible.

Below is the code snippet.

$(document).ready(function(){
  var count = 0;
  $("#menu1,#menu2,.topnav-dropdown").mouseenter(function(){
    count++;
    $(".topnav-dropdown").css({"height": "6em","transition-duration":"0.1s"});
    if($(this).is("#menu1,#menu2")){
      $(".menuitem").children().css({"border-bottom":"none"});
      $(this).children().css({"border-bottom":"0.2em solid #111"});
    }
  })
  $("#menu1,#menu2,.topnav-dropdown").mouseleave(function(){
    count--;
    if (!count){
      $(".topnav-dropdown").css({"height": "0","transition-duration":"0.2s"});
      if($(this).is(".topnav-dropdown")){
        $(".menuitem").children().css({"border-bottom":"none"});
      }
    }
  });
});
* {
  box-sizing: border-box;
  margin: 0;
  font-size: 100%;
}

.clearfix {
  clear: both;
}

.top-section {
  height: 2em;
}

.topnav {
  width: 70%;
  margin: auto;
}

.menuitem {
  float:left;
  margin: -0.1em;
  height: 3em;
  display: flex;
  align-items: center;
  overflow:hidden;
  padding-top: 1em;
}

.menuitem-a {
  font-size: 1em;
  margin: 0 1em 0;
  text-decoration: none;
  color: black;
  height: 100%;
}

.menuitem-a:visited {
  color: black;
}

.topnav-dropdown {
  position: absolute;
  top: 2.9em;
  width: 100%;
  background-color: #333;
  z-index: 1;
  height: 0;
}
<!DOCTYPE html>
<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
  <div class="top-section">
    <div class="topnav">
      <div id="menu1" class="menuitem">
        <a class="menuitem-a" href="#">Menu1</a>
      </div>
      <div id="menu2" class="menuitem">
        <a class="menuitem-a" href="#">Menu2</a>
      </div>
    </div>
    <div class="clearfix"></div>
    <div class="topnav-dropdown">
    </div>
  </div>
</body>
</html>

I attempted modifying the second last line of jQuery:

if($(this).is(".topnav-dropdown"))

to

if($(this).is("#menu1,#menu2,.topnav-dropdown"))

however, this also eliminates the border when transitioning from the button to the dropdown area..

Your assistance in this matter would be highly appreciated!

Answer №1

This task is more challenging than it may seem at first glance. I believe I have addressed all the key points.

$(document).ready(function() {

  $("#menu1").mouseenter(function() {
    $("#menu2").children().css({
      "border-bottom": "none"
    });
    $(this).children().css({
      "border-bottom": "0.2em solid #111"
    });
    $(".topnav-dropdown").css({
      "height": "6em",
      "transition-duration": "0.1s"
    });
  });

  $("#menu2").mouseenter(function() {
    $("#menu1").children().css({
      "border-bottom": "none"
    });
    $(this).children().css({
      "border-bottom": "0.2em solid #111"
    });
    $(".topnav-dropdown").css({
      "height": "6em",
      "transition-duration": "0.1s"
    });
  });

  $("#menu1, #menu2").mouseleave(function() {
    if (!$(".topnav-dropdown").is(":hover")) {
      $(".menuitem").children().css({
        "border-bottom": "none"
      });
      $(".topnav-dropdown").css({
        "height": "0",
        "transition-duration": "0.2s"
      });
    }
  });

  $(".topnav-dropdown").mouseleave(function() {
    $(".menuitem").children().css({
      "border-bottom": "none"
    });
    $(this).css({
      "height": "0",
      "transition-duration": "0.2s"
    });
  });

});
* {
  box-sizing: border-box;
  margin: 0;
  font-size: 100%;
}

.clearfix {
  clear: both;
}

.top-section {
  height: 2em;
}

.topnav {
  width: 70%;
  margin: auto;
}

.menuitem {
  float: left;
  margin: -0.1em;
  height: 3em;
  display: flex;
  align-items: center;
  overflow: hidden;
  padding-top: 1em;
}

.menuitem-a {
  font-size: 1em;
  margin: 0 1em 0;
  text-decoration: none;
  color: black;
  height: 100%;
}

.menuitem-a:visited {
  color: black;
}

.topnav-dropdown {
  position: absolute;
  top: 2.9em;
  width: 100%;
  background-color: #333;
  z-index: 1;
  height: 0;
}
<!DOCTYPE html>
<html>

<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>

<body>
  <input type="hidden" id="hid">
  <div class="top-section">
    <div class="topnav">
      <div id="menu1" class="menuitem">
        <a class="menuitem-a" href="#">Menu1</a>
      </div>
      <div id="menu2" class="menuitem">
        <a class="menuitem-a" href="#">Menu2</a>
      </div>
    </div>
    <div class="clearfix"></div>
    <div class="topnav-dropdown">
    </div>
  </div>
</body>

</html>

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

SignalR is experiencing an issue with a protocol error that is related to an unknown

I've been trying to implement the concepts from this article on SignalR and Knockout.js, but I'm encountering some issues. Here is the article I am referencing: http://www.codeproject.com/Articles/322154/ASP-NET-MVC-SIngalR-and-Knockout-based-Re ...

Hugo inquired about the process of including a class specifically for the final post

I've set up my baseof.html file to add the class first-post to the body tag in order to style the first post differently from the rest, especially when paginated: <body class=" {{ with where site.RegularPages "Type" "posts" }} {{ $first_post = ind ...

Navigating to two separate webpages concurrently in separate frames

I am working on creating a website with frames, where I want to set up a link that opens different pages in two separate frames. Essentially, when you click the link, one page (such as the home page in a .html file) will open in frame 1 on the left side, ...

Updating text color in JavaFX for a textarea

I'm having trouble getting this to work. After checking the CSS analyzer in the scene builder, it seems that changing the text color in a textarea requires something like this: .text-area .text { -fx-fill: #1e88e5; -fx-font-size: 32px; } How ...

Is it possible to establish a scope for jquery.ajaxSetup()?

Currently, I am working on a project involving numerous ajax calls with repetitive parameters. After some consideration, I am contemplating using jquery.ajaxSetup() to streamline the code. However, jQuery does not recommend this approach in their API docu ...

Is there a way to combine the input tag's name with a growing integer variable?

Within a while loop, I need to modify the names of input tags by concatenating them with an increasing integer variable. However, attempts to use the $ symbol have not yielded successful results. <html> <head> <meta http-equiv="Content- ...

Decoupling the Top Navigation Bar from the Side Navigation Bar

I currently have a top navigation bar with some menus and a side navigation bar with additional menus. I have applied styling to the vertical navigation bar, but it seems to be affecting all the navigation bars, resulting in an output as shown in the image ...

Steps to send a table to PHP and store it as a text file

I have this program for a form data entry. It includes text boxes to input values, and upon clicking 'Submit', the input values should be displayed below while resetting the text box for another set of inputs. The issue I am facing is with the sa ...

The JSON response unexpectedly includes null values, despite there being no null values found in the database during the query

When trying to retrieve data using PHP by passing an email as a parameter, I encountered some formatting issues and null values in the response. It seems like there might be a mistake in my code causing this problem. Here is what the output looks like: {" ...

Choose checkboxes from an array of values using AngularJS

I have recently developed a page that includes text boxes, radio buttons, and checkboxes. This page serves as a platform for saving new data or modifying existing information. While I can successfully save new data, I encountered an issue when trying to ed ...

Ways to implement material-ui button design on an HTML-native button

I am using pure-react-carousel which provides me an unstyled HTML button (ButtonBack). I would like to customize its style using material-ui. Trying to nest buttons within buttons is considered not allowed. An approach that works is manually assigning th ...

How to implement variables within the .map() function in JavaScript?

I am working on a map function where I need to pass in a variable as a Key to change the object item key to be based on that variable instead. For example, I want the obj.Salary below to actually represent the salary value when day equals "Day" instead o ...

Utilizing jQuery to Sort Table Rows based on an Array of Class Names

I have a table containing values and a filter option where users can select multiple values to filter the table. I want to create a filter with numbers ranging from 1 to 10, and assign class names like filter_1, filter_2, filter_3, etc. to each table row ( ...

Can we activate or attach a jQuery UI event?

Similar Question: jQuery AutoComplete Trigger Change Event After successfully implementing the jQuery UI Autocomplete widget using version 1.9, I am curious to know if it is possible to trigger or bind a jQuery UI event. For example, can the jQuery UI ...

What could be the reason for the malfunction of my jquery loader?

I'm currently working on enhancing a jQuery script by adding a loading GIF to it. I came across this plugin that I've used successfully in the past, but for some reason, it's not triggering when I submit a form. Could it be misplaced in my c ...

Stop iFrame from inheriting parent CSS styles

Is there a way to prevent an iFrame object from inheriting CSS properties from the main page? Specifically: I have created an iFrame object in my main class, and I want to adjust the opacity of the main page without affecting the iFrame. However, when I ...

show various commands and outcomes of the getElementsByClassName() function

Can someone help me figure out how to use the "getElementsByClassName() Method" in JavaScript to change or display different colors? I am trying to change the background color to blue for the class "ex" and red for the class "example", but it's not wo ...

CLS notifies about an Unrecognized CSS Element in Page Insights' Core Web Performance

My experience with Google PageSpeed Insights has been quite frustrating due to the numerous alerts I'm receiving about Unsupported CSS Properties. The importance of avoiding non-composited animations: Animations that are not composited can appear gli ...

When attempting to incorporate the "active" class into a Bootstrap 4 carousel using jQuery, issues arise specifically when retrieving data from a MongoDB database

I'm working on a carousel feature where I need to load images from my MongoDB database. However, the issue I'm facing is that even though I've tried adding the active class to the first item, the carousel doesn't transition to display t ...

Performing Jquery functions on several elements at once

Looking at the code snippet below, there are two buttons and an input in each container. The input calculates and adds up the number of clicks on the 2 buttons within the same container. However, it currently only works for the first container. How can thi ...