When I hover over one menu item, it interferes with my CSS transitions because the other menu items are hidden

I've been attempting to create a menu with very specific behavior: 1) Submenu transitions in when its corresponding menu item is hovered over 2) Submenu remains visible for a moment after moving the mouse away before transitioning out 3) If another menu item is hovered over, any remaining submenus from previous hovers disappear instantly.

I've been struggling with this for some time now and encountering unexplainable bugs along the way.

Here's the HTML structure I used (using divs instead of a list):


    <div class="menu">
<div class="menu-item" id="item-1">
  Item 1
  <div class="menu-child" id="child-1">Child 1</div>
 </div>

 <div class="menu-item" id="item-2">
  Item 2
  <div class="menu-child" id="child-2">Child 2</div>
 </div>

 <div class="menu-item" id="item-3">
  Item 3
  <div class="menu-child" id="child-3">Child 3</div>
 </div>
 </div>

The accompanying CSS:

.menu-item .menu-child {
  margin-top:10px;
  border:1px solid #000000;
  height:22px;
  visibility:hidden;
  opacity:0;
  -webkit-transition: visibility 1s 1s, opacity 1s 1s;
  transition: visibility 1s 1s, opacity 1s 1s;
}

.menu-item:hover .menu-child {
  visibility:visible;
  opacity:1;
  -webkit-transition: visibility 1s, opacity 1s;
  transition: visibility 1s, opacity 1s;
}

.menu {
  display: -webkit-flex;
  display:flex;
}

.menu-item {
  display:inline-block;
  width:32%;
  padding-left:20px;
  background:none;
  box-sizing: border-box;
  -webkit-box-sizing: border-box;
}

And here's the JavaScript snippet:


   $('#item-1').hover(
    function() {
    $("#child-2, #child-3").hide();
    },
    function() {
    $("#child-2, #child-3").show();
    }
);
   $('#item-2').hover(
    function() { 
    $("#child-1, #child-3").hide();
    },
    function() { 
    $("#child-1, #child-3").show();
    }
);
   $('#item-3').hover(
    function() { 
    $("#child-2, #child-1").hide();
    },
    function() { 
    $("#child-1, #child-2").show();
    }
);

Link to live demo on JSFiddle

...and so on.

Answer №1

It's essential to highlight the importance of formulating concise queries on SO and how it can affect the likelihood of receiving helpful responses. To demonstrate this, consider the following example of how I would have presented the question:


The goal is to eliminate the transition effect on menu items when hovering over another sibling menu item. Here is my current attempt:

<!-- /* your existing code here, added using the snippet tool */ -->

If your inquiry had been more succinctly stated, SO could have potentially offered a quicker and more efficient solution. However, this is just a conjecture. :)


Now, are you looking for the code snippet below?

$('.menu-item').on('mouseenter', function(){
$('.menu-item').addClass('hide-children');
  $(this).removeClass('hide-children');
  setTimeout(function(){
  $('.menu-item').removeClass('hide-children');
  }, 42)
})
// CSS for fixing transition issue
/* Additional CSS styles */
.menu-item.hide-children .menu-child {
  display: none;
  -webkit-transition: visibility 0s linear 0s, opacity 0s linear 0s;
       -o-transition: visibility 0s linear 0s, opacity 0s linear 0s;
     -moz-transition: visibility 0s linear 0s, opacity 0s linear 0s;
          transition: visibility 0s linear 0s, opacity 0s linear 0s;
}
.menu-item.hide-children:hover .menu-child {
  display: block;
}
// End of additional CSS

// Note about CSS fix
// The provided CSS should resolve the transition problem if integrated into your existing CSS. 
// Also, ensure to incorporate the jQuery part.

// Additionally, based on my observation, the menu items were causing content displacement,
// which ideally shouldn't occur. Hence, I made some modifications to your markup and adjusted
// the CSS accordingly to offer an alternative solution.

body {
  background-color: #f5f5f5;
}
// More CSS styles follow...

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

Press the "Print All" button to instantly print out all of the images in one go

I have successfully implemented a feature where multiple images are displayed as thumbnails from a specified folder. Now, I am looking to add a "Print All" button that will allow users to print all the images in one go. Is it possible to print all images ...

What is the best way to eliminate YouTube recommended videos that appear at the conclusion of my video?

I am attempting to incorporate my own YouTube video onto my blog without having recommended videos display at the end. I have tried using ?rel=0 and &rel=0 but they do not seem to be working. <iframe class="embed-responsive-item" src="https://www.y ...

Locate the HTML element and assign an ID to its parent

Current code is working correctly, but encountering an issue with changed image paths. $('td img[src="/images/nav/prev_dim.gif"]').parent().closest("table").attr('class','robsid'); The image path changes after '.gif&ap ...

unusual occurrences with CSS and JavaScript when hovering over TEXTAREA or A elements

Lately, I've been facing an unusual issue in my web application (php) that was fine just a month ago. Upon hovering over a specific < TEXTAREA > or two buttons (add, exit) within a DIV, the background color of the DIV fills up, rendering the IN ...

Tips for quickly referencing the HTML select tag

I have a dropdown menu in my HTML that is linked to my database's location table. When a user selects an item from this dropdown, I want the second dropdown (linked to the product table) to dynamically update based on the user's selection from th ...

Experiencing an "Unsupported media type-415" error when making an ajax call using Jquery and ajax technology

When I try to update a specific record using an ajax function, I keep receiving a 415-Unsupported Media type error. Below is the JavaScript code I am using: $("#update").on("click", function(){ event.preventDefault(); return $.ajax('/upda ...

Guide to making the top margin of Chinese characters consistent on all web browsers

Currently, I am working on a website that features flashcards for Chinese character learning. The issue I am facing is that Internet Explorer displays fonts slightly higher on the line compared to other browsers I have tested. Although this may seem insign ...

Slider-activated Pie Chart Controller

I have successfully created a pie chart using highchart and javascript, allowing me to adjust each slice individually using a slider. However, I am facing an issue where I want the maximum value of the pie to be 100%, with the other sliders contributing to ...

Step-by-step guide for activating a text box when a check box is marked

I'm looking to activate and deactivate two text boxes when a check box is selected. Currently, only one text box is enabled when the check box is checked. How can I modify my code to enable and disable two text boxes based on the check box status? Her ...

The background image in the hovering textbox shifts and changes size

I've been trying to set a background image on a Kendo UI Textbox. It looks good until you hover over it. But when you do hover over it, this issue arises: How can I resolve this? The background image should remain in place even when I hover and cli ...

What's the best way to eliminate blank space in my Bootstrap grid columns?

I'm facing an issue with the search filter layout on my website. It includes input fields for a search term, two datepickers, and two buttons: <div id="search-holder"> <div id="search-box"> <div ...

Guide to verifying if an element contains multiple CSS classes using jQuery

Looking at the HTML structure provided below, the goal is to loop through all elements inside the div with the id "accordionContents." Within this loop, there's a need to check whether the h2 element has both the css classes "acc_trigger" and "active" ...

JavaScript and AJAX: Dynamically Create URLs

I don't have any experience with web development, so could you please explain in detail? If my questions are unclear, feel free to ask for more information. Imagine I have a webpage with the following URL: www.mycompany.com/category1 This page has ...

Experiencing issues when attempting to access the open weather API through Ajax?

I have been working on calling the open weather API endpoint in order to retrieve weather data. I am implementing an AJAX call to achieve this, but unfortunately I am facing difficulties in retrieving the data and encountering errors. The script executi ...

Export jqgrid information into Excel and PDF while preserving the currency format

I am looking for a way to export jqgrid data in currency format, such as $4,355. Here is my colmodel. I am using the function exportGrid('griddtable',[1,2,3,4,5]) name : 'totalSpend', index : 'tota ...

Positioning Text in a Responsive Video Background

Trying to center text within a video has been a challenge for me. Even after removing the header container, achieving the desired results is still possible, which works fine with me. However, I'm still struggling with getting this text centered! < ...

HTML Button without Connection to Javascript

When attempting an HTML integration with GAS, the code provided seems to be generating a blank form upon clicking "Add" instead of functioning as expected: //GLOBALS let ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); var lastRow = ss.getLa ...

Here's a unique version of the text: "Learn how to customize a datepicker to display only the next 3 months as a dropdown starting from the current date. Once the current

How can I limit the date picker to show only 3 months as a dropdown starting from the current date? $(function() { $("#date").datepicker({ hideIfNoPrevNext: true, minDate: '0M', maxDate: '+90D' }); }); <link rel="s ...

Alternative for Jquery: a new Hash Table solution using Prototype JS

Greetings everyone! I have experience as a prototype JS developer but now I am transitioning to jQuery for work/client reasons. One feature I really liked in Prototype was the Hash class, such as var h = new Hash();. However, I understand that jQuery doe ...

Enhancing JSON URLs with jQuery's Double-colon Syntax

Working on a project in EZPublish, I am utilizing the EZJScore module to create JSON arrays. The Ezjscore module requires me to use double colons to separate parameters, like this: http://[domain]/ezjscore/call/news::cat::3 After extensive testing, I ...