The inclusion of Bootstrap 4 in the header seems to be causing issues with mouse events not functioning correctly

Exploring the functionalities of Bootstrap 4 and jQuery has been an interesting journey. I recently worked on creating a Navbar with 4 links, where the 4th link has a submenu that opens upon hover. However, I encountered an issue where this functionality only works without including the bootstrap CDN in the code.

I attempted to resolve this by downloading both the Bootstrap CSS and jQuery min JS files separately, but unfortunately, it did not solve the problem. While I could implement a custom solution using CSS, my preference is to achieve this functionality solely through jQuery.


Code


$(document).ready(function() {

  $("#dropDown-nav, #submenu").mouseenter(function() {
    console.log("mouse entered");
    $("#submenu").show();
  });

  $("#submenu, #dropDown-nav").mouseleave(function() {
    console.log("mouse left");
    $("#submenu").hide();
  });

  $(".topnav a").click(function(event) {
    $("a").css('background-color', '#2F3942');
    $(".dropbtn").css('background-color', '#2F3942');
    $(this).css('background-color', '#E1E1E1');
    $("a").css('color', '#E1E1E1');
    $(".dropbtn").css('color', '#E1E1E1');
    $(this).css('color', '#2F3942');
  });

  $(".dropdown-content a").click(function(event) {
    $(".topnav a").css('background-color', '#2F3942');
    $(".dropbtn").css('background-color', '#E1E1E1');
    $(this).css('background-color', '#E1E1E1');
    $(".topnav a").css('color', '#E1E1E1');
    $(".dropbtn").css('color', '#2F3942');
    $(this).css('color', '#2F3942');
  });

});

function myFunction() {
  var x = document.getElementById("myTopnav");
  if (x.className === "topnav") {
    x.className += " responsive";
  } else {
    x.className = "topnav";
  }
}
#mainNav {
  text-align: left;
  vertical-align: middle;
}

#mainNav_2 {
  text-align: center;
  font-size: 2em;
  color: white;
  font-weight: bolder;
}

#mainNav {
  overflow: hidden;
  background-color: #0D5DB8;
  padding-left: 15px;
  height: 6em;
}

body {
  margin: 0;
  font-family: Arial
}

.topnav {
  overflow: hidden;
  background-color: #2F3942;
}

.topnav a {
  float: left;
  display: block;
  color: #E1E1E1;
  text-align: center;
  padding: 8px 10px;
  text-decoration: none;
  font-size: 14px;
  font-weight: 600;
}

.forMargin {
  margin-right: 15px;
}

...

<!-- <div class="jumbotron">
                    <h1>Bootstrap Tutorial</h1>      
                    <p>Bootstrap is the most popular HTML, CSS, and JS framework for developing responsive, mobile-first projects on the web.</p>
            </div> -->

My goal is to make the hover functionality work seamlessly with both Bootstrap and jQuery integration.

Answer №1

All you have to do is increase the z-index of .dropdown-content (or remove it) and also eliminate the position: absolute.

Take a look at the snippet below to see if that meets your requirements:

$(document).ready(function() {

  $("#dropDown-nav, #submenu").mouseenter(function() {
    console.clear()
    console.log("mouse entered");
    $("#submenu").show();
  });

  $("#submenu, #dropDown-nav").mouseleave(function() {
    console.clear()
    console.log("mouse left");
    $("#submenu").hide();
  });

  $(".topnav a").click(function(event) {
    $("a").css('background-color', '#2F3942');
    $(".dropbtn").css('background-color', '#2F3942');
    $(this).css('background-color', '#E1E1E1');
    $("a").css('color', '#E1E1E1');
    $(".dropbtn").css('color', '#E1E1E1');
    $(this).css('color', '#2F3942');
  });

  $(".dropdown-content a").click(function(event) {
    $(".topnav a").css('background-color', '#2F3942');
    $(".dropbtn").css('background-color', '#E1E1E1');
    $(this).css('background-color', '#E1E1E1');
    $(".topnav a").css('color', '#E1E1E1');
    $(".dropbtn").css('color', '#2F3942');
    $(this).css('color', '#2F3942');
  });

});

function myFunction() {
  var x = document.getElementById("myTopnav");
  if (x.className === "topnav") {
    x.className += " responsive";
  } else {
    x.className = "topnav";
  }
}
#mainNav {
  text-align: left;
  vertical-align: middle;
}

#mainNav_2 {
  text-align: center;
  font-size: 2em;
  color: white;
  font-weight: bolder;
}

#mainNav {
  overflow: hidden;
  background-color: #0D5DB8;
  padding-left: 15px;
  height: 6em;
}

body {
  margin: 0;
  font-family: Arial
}

.topnav {
  overflow: hidden;
  background-color: #2F3942;
}

.topnav a {
  float: left;
  display: block;
  color: #E1E1E1;
  text-align: center;
  padding: 8px 10px;
  text-decoration: none;
  font-size: 14px;
  font-weight: 600;
}

.forMargin {
  margin-right: 15px;
}

.topnav .icon {
  display: none;
}

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

.dropdown .dropbtn {
  font-size: 14px;
  border: none;
  outline: none;
  color: #E1E1E1;
  padding: 8px 10px;
  background-color: inherit;
  font-family: inherit;
  margin: 0;
  font-weight: bolder;
}

.dropdown-content {
  display: none;
  background-color: #2F3942;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
  z-index: 10; /* Increase or remove */
}

.dropdown-content a {
  float: none;
  color: E1E1E1;
  padding: 7px 9px;
  text-decoration: none;
  display: block;
  text-align: left;
}

.topnav a:hover,
.dropdown:hover .dropbtn {
  background-color: #2F3942;
  color: E1E1E1;
}

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


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

@media screen and (max-width: 600px) {
  .topnav a,
  .dropdown .dropbtn {
    display: none;
  }
  .topnav a.icon {
    float: left;
    display: block;
  }
}

@media screen and (max-width: 600px) {
  .topnav.responsive {
    position: relative;
  }
  .topnav.responsive .icon {
    position: absolute;
    right: 0;
    top: 0;
  }
  .topnav.responsive a {
    float: none;
    display: block;
    text-align: left;
  }
  .topnav.responsive .dropdown {
    float: none;
  }
  .topnav.responsive .dropdown-content {
    position: relative;
  }
  .topnav.responsive .dropdown .dropbtn {
    display: block;
    width: 100%;
    text-align: left;
  }
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>

<nav class="navbar navbar-expand-lg navbar-expand-sm j" id="mainNav">
  <div class="col-4" id="mainNav_1">
  </div>
  <div class="col-4" id="mainNav_2">
  </div>
  <div class="col-4" id="mainNav_3">
  </div>
</nav>

<div class="topnav" id="myTopnav">
  <a href="#A" class="forMargin">A</a>
  <a href="#B" class="forMargin">B</a>
  <a href="#C" class="forMargin">B</a>
  <div class="dropdown">
    <button class="dropbtn" id="dropDown-nav" class="forMargin">D 
                            <i class="fa fa-caret-down"></i>
                        </button>
    <div class="dropdown-content" id="submenu">
      <a href="#link1">Link 1</a>
      <a href="#link2">Link 2</a>
      <a href="#link3">Link 3</a>
    </div>
  </div>
  <a href="javascript:void(0);" style="font-size:15px;" class="icon" onclick="myFunction()">&#9776;</a>
</div>

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 Tailwind style did not completely translate when applied to the content of dangerouslySetInnerHtml

I have an HTML file containing Tailwind styles stored in my database, which needs to be fetched first. This will result in an HTML string that will be inserted into dangerouslySetInnerHtml. Here is the code snippet (utilizing Qwik): export const useHTML = ...

Steps to create a toggle feature for the FAQ accordion

I am currently working on creating an interactive FAQ accordion with specific features in mind: 1- Only one question and answer visible at a time (I have achieved this) 2- When toggling the open question, it should close automatically (having trouble with ...

Items that do not commence from the start of the list

I'm encountering an issue with my unordered list where the li elements are not rendering properly. The first element seems to have a margin, and I'm unsure why this is happening. How can I fix this problem? function App() { return ( < ...

Jquery CSS malfunctioning on Post's div element

After retrieving a div using jquery's post method on my page, I am encountering an issue when attempting to apply CSS to the divs. Strangely enough, applying CSS with jquery to divs that are already present on the page works perfectly fine. Javascrip ...

Cyrillic characters cannot be shown on vertices within Reagraph

I am currently developing a React application that involves displaying data on a graph. However, I have encountered an issue where Russian characters are not being displayed correctly on the nodes. I attempted to solve this by linking fonts using labelFont ...

Troubleshooting problem with CSS combinators

I'm having trouble with this, if I have these two tag combinations in my CSS: aside h4 { color:yellow; } article h4 { color:blue; } And I apply them to the following HTML: <div> <h4>International new</h4> ...

What is the best way to apply CSS styles to different states in React?

I am endeavoring to adjust the size of an element by utilizing this code snippet. const [size, setSize] = useState({}); The initial size I wish to set is: transform: scale(1, 1) translate3d(0, 0, 0); Upon clicking on the element, my aim is to enlarge it ...

Adjusting CSS to realign images on various screen types

I am facing an issue with displaying a background image on a static web page. The height appears differently on various LCD screens. How can I ensure that the height adjusts to approximately 80% of the area? I have tried giving a percentage in the style fo ...

React: Issue encountered when updating props from parent component (Error executing removeChild operation)

Currently, I am utilizing react along with AJAX to retrieve data for a list. However, every time componentWillReceiveProps(nextProps) is triggered after the initial setup, an error occurs. This error typically arises when the URL for the AJAX call has chan ...

Tips for creating AngularJS forms which display radio buttons and populate data from a JSON file

I'm having trouble displaying the json data correctly. How can I show the radio buttons from my json file (plunker demo)? Additionally, I want to validate the form when it is submitted. Here is the HTML code: <my-form ng-app="CreateApp" ng- ...

Accessing the identical file concurrently through ajax 12 times

I'm facing an issue with loading the same external file multiple times on a page. When the page loads, it takes up to a minute due to large load times. I've tried using Ajax to load each file individually, but running a loop to load all files at ...

What is the best way for me to show comments beneath all of my posts?

Currently, I am facing an issue with my application where I need to retrieve comments from posts. I am unsure of the best approach to tackle this problem. The existing system only displays posts that have comments, but it shows only a single comment inste ...

Using AngularJs to have two ui-views on a single page

As a beginner in AngularJs, I encountered an issue with having two ui-views on the same page. When I click a button to show the view for goals, both the form for goals appear in ui-view 1 and ui-view 2 simultaneously. I have been working with states but I ...

Changing the link color within a repeater in ASP .NET when it is clicked

The given code snippet is being executed on an iPhone 4, causing some CSS elements like Hover to not function as expected. <asp:Repeater ID="rpt" runat="server"> <HeaderTemplate> <div class="table withshadow"> </Header ...

Observing the CSS class of an input element during async validation does not yield the desired results

Recently, I implemented a bootstrap directive that monitors all the input elements on a form and updates the CSS of its parent div to display validation errors in a Bootstrap-friendly manner. The directive checks for the presence of the ng-invalid class in ...

Methods for establishing state within a React Native render function

I encountered an issue when attempting to set state within the render lifecycle. Warning: It is not recommended to update state during an ongoing state transition, such as in the render method or another component's constructor. The render method s ...

The Wordpress admin-ajax.php script is failing to process the function and returning a "0" error code

I have been experimenting with processing AJAX requests in Wordpress and I'm following a particular tutorial to achieve this. The goal is to create a basic AJAX request that will display the post ID on the page when a link is clicked. The Approach ...

turning off row rearrangement feature in jquery datatable

I am currently utilizing the DataTables Row Reordering Add-on (http://jquery-datatables-row-reordering.googlecode.com/svn/trunk/index.html ) and I am seeking a way to deactivate the reordering using JavaScript. I have attempted to use the following code sn ...

Examples, templates, and best practices for creating top-notch API documentation

Currently, I am in the process of developing the user interface for a web service, while another organization is handling the back end. I am looking for a clear, simple, and easily comprehensible method of creating a document outlining API calls that will ...

Check to see if an array contains any falsy values and return accordingly

My goal is to only return the error message if any value is falsy, and never return the hooray message. I am utilizing lodash. var jawn = [ { "cheese" : true, "with" : true, "without" : true }, { "cheese" ...