How can I keep a div open when the mouse hovers over it, and then close it when the mouse

My current markup looks like this:

<div class="wrapper-header">
<div class="container">
<div class="navbar">
<ul class="nav navbar-nav">
<li class=""><a href="#" class="toggle">Show Categories</a></li>
</ul>
</div>
</div>

<div class="wrapper-categories">
<div class="container">
Content Here
</div>
</div>

The .wrapper-categories is set to display: none; by default, so it only becomes visible when clicked on with:

 $(".toggle").on('click', function (event){
               event.preventDefault();
               $(".wrapper-categories").slideToggle("fast");
                 $(this).html(function(i,html) {
                     if (html.indexOf('Browse') != -1 ){
                        html = html.replace('Show','Hide');
                     } else {
                        html = html.replace('Hide','Show');
                     }
                     return html;
                 });
             });

Now, I want to change it so that the .wrapper-categories shows when hovered over instead of clicked. Furthermore, the content should remain visible if the mouse is over it, and should hide when the mouse is no longer over the link or the content div.

I attempted to change it to $(".toggle").hover(function() { and it worked, but the content is not staying open. What additional adjustments are needed?

Answer №1

The reason your code is not working as expected is because the hover event of .toggle only works for itself. When you try to hover over the contents under .wrapper-categories, the cursor goes out of the .toggle scope.

To achieve the desired functionality, you can modify the menu structure by using a simple structure of ul and li.

Check out this FIDDLE for a working example.

HTML:

<ul class="nav navbar-nav">
<li class="menu"><a href="#" class="toggle">Show Categories</a>
    <ul>
        <li>
            Content Here
        </li>
        <li>
            Content Here
        </li>
        <li>
            Content Here
        </li>

    </ul>
</li>
</ul>

JS:

$(".menu").mouseover(function(){
    $(this).find('ul').css('visibility', 'visible');    
});
$(".menu").mouseout(function(){
    $(this).find('ul').css('visibility', 'hidden'); 
});

CSS:

.menu > ul{
    visibility:hidden;
}
.menu > ul > li:hover{
    font-weight:bold;
}

Answer №2

Here's the solution to your issue. https://codepen.io/7htvz/pen/PoPxPEL

I've enclosed everything in a nav class.

Instead of using mouseout(), I opted for mouseleave(). Refer to the Jquery documentation for more information on these methods.

Take a look at the code for a better grasp of the solution.

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

Encountering issues when trying to incorporate SASS and CSS libraries in NextJS

I have been attempting to integrate the wix-style-react plugin into my NextJS project, but I am encountering difficulties when trying to build. According to their documentation, they utilize Stylable, SASS, and CSS Modules. After installing the plugin and ...

Disappearing act: CSS3 transform - rotate makes font vanish

When applying a simple css3 transformation rule to an html element which contains various other elements like h1, h2, inputs, and img, I encounter the following issue: div{ -moz-transform: scale(1) rotate(-3deg) translateX(0px) translateY(0px) skew ...

Efficiently Loading JavaScript Files in Django for Optimal Website Performance

I have a Django blog app with a Post model that includes a field called body. This field may contain Latex, so I utilize MathJax.js, as well as code snippets, for which I use highlight.js. Sometimes I use both, and other times neither. Is there a way to a ...

React function failing to utilize the latest state

I'm facing an issue with my handleKeyUp function where it doesn't seem to recognize the updated state value for playingTrackInd. Even though I update the state using setPlayingTrackInd, the function always reads playingTrackInd as -1. It's p ...

Where should the live() method be implemented to ensure the successful functioning of this jQuery crossfade transition

My jQuery crossfade is mostly working, but I believe I need to use the live() function because the class "active" is added to a list item element via the code. I'm unsure of where to include the live() function in my code for it to work properly. He ...

Problems with Vuex getter reactivity when used in conjunction with Vue router

I've encountered an issue with my Vuex getter that fetches data for the current route and displays it in a Vuetify v-data-table. Everything works perfectly fine when the component is initially created. However, when I add a new entry to the data array ...

jQuery - Strip the parent of its class

How do I take off a parent's class? Link to jsfiddle here const panels = document.querySelectorAll('.panel'); const titles = document.querySelectorAll('.section-titles'); panels.forEach(panel => { panel.addEventListener(&apo ...

Having trouble with basic authorization for Facebook API using JavaScript?

I am having an issue with my source code. When I run it, I receive an alert that says "No Response" and then the Facebook page displays an error message stating "An error occurred with MYAPP. Please try again later" <div id="fb-root"></div> & ...

Displaying or concealing fields through Javascript depending on the category type of the results item

I am facing an issue with displaying certain fields in each individual property search result based on the property type. For example, if a property is classified as Land, I do not want the bedrooms and bathrooms fields to be visible, but if it is a Villa, ...

Mantine - Showing search results in a scrollable list that stops at the parent's height and then adds vertical scrolling

I am currently in the process of developing a web application that needs to occupy the entire height and width of the screen using 100vh and 100vw. The main app itself should not have any vertical or horizontal scrolling, but individual components/divs wi ...

Issue with jQuery datepicker not triggering onChangeMonthYear event

Recently, I've been working on creating an app using jQuery date picker. To see my progress so far, feel free to check out this fiddle: http://jsfiddle.net/Lf6sD/2/. In the options, there's a mention of an onChangeMonthYear event that should trig ...

Pull data from another domain's DIV using jQuery's load/ajax function

I need to load content from a different domain into a DIV on my JSP page. For example: $("#myDiv").load("https://www.google.com") The issue I'm facing is that the request is being blocked by the browser's same origin policy. I've explore ...

jquery is not providing any text content from the body

My current setup involves an ajax post that sends a website address to PHP for retrieval, and then I want jQuery to locate specific elements within the site and retrieve the text from those elements. While this method works fine for certain elements like h ...

Incorporating Microsoft's Emotion API into an HTML website

Currently, I am attempting to develop a HTML webpage that can detect emotions from images submitted by the user. By referring to Microsoft's documentation, I have produced the following HTML file: <!DOCTYPE html> <html> <head> & ...

Is it possible to develop a web-based chat application using socket.io without the need for ssh access or having to keep the terminal

I have been using php to develop my website. Up until now, I have relied on setTimeout with ajax for updating chats simultaneously. However, when this method stopped working, I discovered socket.io. My goal is to implement private messaging, and while I ha ...

Tips on validating interconnected strings with the help of yup within a react native environment

In my scenario, I am dealing with two date strings called start_date and end_date. Initially, both of these start off as empty strings: export interface FilterSchema { start_date?: any; end_date?: any; } const initialValues: FilterSchema = { s ...

Is it possible to use a jQuery pop-up div box to make edits to a selected

I have a situation where I need to update the rating of LI elements with span text that initially reads "unrated". My goal is to create a functionality where users can press a button, trigger a popup div where they can select a rating from multiple radio b ...

custom vertical tab label text not aligning to the right in material-ui with textAlign

I am struggling with customizing the tabs in material-ui to display them vertically. I also want the text labels of these tabs to align to the right for a more cohesive look. Despite trying various styling options, I have not been successful in achieving t ...

Two CSS borders of varying heights

Just wondering, can we achieve something similar to the style shown in the attachment using only CSS? <h3 style="border-bottom:1px solid #515151"><span style="border-bottom:3px solid #0066b3">HEADER</span></h3> ...

Moving images displayed on a webpage

Is animating 5 pictures/photos a simple task? Take a look at this example: Which programming languages are recommended for achieving this effect? Thank you Tea ...