Is there a way to make all menu items animate when hovering over any item except for the first

How can I create a menu item hover animation using a combination of jQuery and CSS?

I am looking for assistance with creating menu items that have a hover effect where hovering over one item triggers an animation on all other items except the one being hovered on.

Below, I have included CSS animations that I would like to incorporate into this requirement.

I have also written jQuery code that achieves this functionality by changing the "text-decoration" property to "line-through". However, I find the line-through effect to be lacking in visual appeal. I am seeking help in combining the provided CSS and jQuery to accomplish the desired hover effect.

This task involves implementing the hover effect on WordPress menu items.

jQuery(".menu-menu-container ul li").hover(function() { // Mouse over
  jQuery(this).siblings().stop().css("text-decoration", "line-through");
  jQuery(this).parent().siblings().stop().css("text-decoration", "none");
}, function() { // Mouse out
  jQuery(this).siblings().stop().css("text-decoration", "none");
  jQuery(this).parent().siblings().stop().css("text-decoration", "none");
});
.overlay-content ul li a:hover:after,
.overlay-content ul li a:focus:after,
.overlay-content ul li a:active:after {
  width: 40%;
  background: #000;
}

.overlay-content ul li a:after {
  content: '';
  position: absolute;
  left: 50%;
  width: 0%;
  transform: translateX(-50%);
  height: 2%;
  margin-top: 3.2%;
  background-color: transparent;
  transition: .35s;
}
<div class="menu-menu-container">
  <ul>
    <li> <a href="#">Home</a> </li>
    <li> <a href="#">About</a> </li>
    <li> <a href="#">Contact</a> </li>
    <li> <a href="#">Help</a> </li>
  </ul>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

I am looking to merge the CSS and jQuery provided above to achieve the desired hover effect and animation on the menu items as described.

Answer №1

Instead of directly applying CSS styles in jQuery, a better approach would be to add and remove classes based on events. This method provides more flexibility to leverage the power of CSS features. Consider implementing something like this:

jQuery(".menu-menu-container ul li").hover(function() { // Mouse over
    jQuery(this).addClass("hovered");
    jQuery(this).siblings("li").addClass("notHovered")
  }, function() { // Mouse out
    jQuery(this).siblings().stop().removeClass("notHovered"); 
    jQuery(this).removeClass("hovered")
  });

And for the CSS part:

.overlay-content ul li a:hover:after, .overlay-content ul li a:focus:after, .overlay-content ul li a:active:after {
  width: 40%;
  background: #000;
}

.menu-menu-container ul li a:after {
    content: '';
    position: absolute;
    left: 50%;
    width: 0%;
    transform: translateX(-50%);
    height: 2%;
    margin-top: 3.2%;
    background-color: transparent;
    transition: .35s;
}

.menu-menu-container{
    width: 100%;
    background-color: black;
    padding: 0 7.5px;
    box-shadow: 0 4px 4px rgba(0,0,0,0.5)
}

.menu-menu-container li{
  display: inline-block;
  list-style: none;
  padding: 15px;
  border-top: 3px solid transparent;
  border-left: none;
  border-right: none;
  font-family: 'Poppins', sans-serif


}

.notHovered{
  filter: blur(2px);

}

li.hovered{
  border-top-color: white
}

.menu-menu-container a{
  text-decoration: none;
  color: white;
  font-size: 1rem;
}
.menu-menu-container li, .menu-menu-container *{
  transition: 1s;

}

For a visual demonstration of this concept, check out this basic codepen example. It's just meant to give you an idea. Hopefully, this explanation helps.

Answer №2

Forget about using JS altogether! Just adjust the styles when hovering over the UL element, and then separately for each A link on hover:

.menu-menu {
  padding: 0;
}

.menu-menu a {
  text-decoration: none;
  text-align: center;
  display: block;
  /* With relative positioning, we can now use absolute elements inside */
  position: relative;
}

.menu-menu a:after {
  content: '';
  display: block;
  position: absolute;
  left: 50%;
  top: 50%;
  margin-top: -2px;
  width: 0%;
  transform: translateX(-50%);
  height: 3px;
  background-color: #000;
  transition: .35s;
}

.menu-menu:hover a:after {
  width: 100%;
}

.menu-menu a:hover:after,
.menu-menu a:focus:after,
.menu-menu a:active:after {
  width: 0%;
  background: #000;
}
<ul class="menu-menu">
  <li><a href="#">Home</a></li>
  <li><a href="#">About</a></li>
  <li><a href="#">Contact</a></li>
  <li><a href="#">Help</a></li>
</ul>

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

Rails is sending multiple duplicate post requests

There seems to be a strange issue with my Rails app where it is submitting two post requests in rapid succession only when I manually submit forms using jQuery. Oddly enough, if I navigate to another page and try to submit the form again, only one request ...

What could be the reason my IE6 Conditional stylesheet is not functioning properly?

It's as if my brain is going in circles. I'm attempting to utilize a conditional stylesheet specifically for IE6. You can see the test page here. This snippet of code was placed within the head tags: <!--[if IE 6]> <link rel="stylesheet ...

Acquire a set of picture cards for displaying in a grid layout

After creating a cardArray to store card objects, I went through each item in the array to generate images as cards. These card images were then appended to the grid div as children. Additionally, I set up an event listener for the shuffle button. However, ...

Is it possible to utilize react for constructing a static HTML, CSS, and JavaScript document?

As a front end content developer, my main focus is on creating content assets. I typically use vscode to build my html, css, and js files, and then upload these static files to the content management system. However, this approach doesn't give me the ...

What is the process for establishing connections in a NoSQL database?

What is the process for establishing relationships in a NoSQL database? You can establish relationships in a Firebase database, where the database is stored in a JSON format. ...

Using gradients in the app bar with ReactJS and Material UI is not currently supported

Recently, I decided to create my own custom appbar for a personal project and opted to use the Erica One font. As it is still in its initial stages, there isn't much code written yet. However, I've encountered two issues related to linear and ra ...

PHP Email Form Sending Duplicate Emails to Recipients

My PHP mail form is set up to use AJAX & jQuery for validation and submission. Everything appears to be working fine, but the client receiving multiple copies of each email sent by the form. The number of duplicates ranges from 1 to 10 with no clear pa ...

When using div elements for tables, the width and text ellipsis properties are not enforced

I need help creating a simple table that will truncate text with ellipses if it exceeds the width. Despite my efforts, the width is not being applied correctly. You can see the issue here: Check out this JSFiddle for the table width Here is the code sni ...

"Empower your forms with Symfony, jQuery, and Prototype for

I am currently working on creating a dynamic form in Symfony 2.0.13 that allows for the addition and removal of fields, although I am only able to add fields at the moment. I have been following the instructions provided in the documentation at this link, ...

Adaptable background visuals

Struggling to find a solution for a seemingly simple problem. I am looking to create responsive/fluid images that scale relative to the viewport size. I also need to load them using CSS with the background property for selective loading of different versio ...

Using a jQuery variable in PHP on the same page without needing to submit the form can easily be achieved

Is there a way to pass a Jquery variable to a php variable on the same page when the selected list item changes? <select name="RegistrationTypeID" class="formselectbox" id="RD" onchange="getText(this.val)"> <option value="">Select Registra ...

Is there a way to make a sass file universally accessible without having to import it into every file and causing an increase in bundle size?

Question How can I efficiently import variables.scss globally without the need to duplicate them in every file and reference them in my build instead of importing? Setup I am using Vue2 with laravel-mix, where I have imported index.scss in my main.js ...

I want the navigation bar to appear only upon scrolling, but when I refresh the page it is already visible, and then vanishes as I start scrolling

I'm working on a project where I want the navigation bar to only appear after scrolling a certain distance down the page. However, I've noticed that when I refresh the browser, the navigation bar appears immediately and then disappears once I sta ...

Arrange elements in different directions to accommodate smaller screens

I need help aligning my brand and the links on opposite sides (brand on left, other links on the right) for mobile view. The issue I'm facing is that the links on the right end up moving to the next line. Any suggestions or advice would be greatly app ...

Unable to provide feedback on the input elements

ISSUE: Unable to provide input in the input fields // Enable dragging for the DIV element: dragElement(document.getElementById("mydiv")); function dragElement(elmnt) { var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0; if (document.getElementB ...

Developing a Restful Put Request using JQuery

Having some trouble calling a restful service that was developed in ServiceStack. I've got the GET(s) calls working, but Put and Post are giving me issues. Here's my client-side script: function savePartner(e) { $.ajax({ ...

Blurring images with a combination of jQuery Backstretch and Blurjs

I have a background image displayed using Backstretch and want to apply Blur.js to blur elements on top of it. However, when I try to use Blur.js on the backstretched image, it doesn't work. I believe this is happening because Blur.js uses the CSS pro ...

Having trouble getting the custom font to display correctly in xhtml2pdf for a Django project

I'm having trouble incorporating a custom font into my PDF generated from HTML. Although I successfully displayed the font in an HTML file, indicating that the font path is correct and it's being used properly, the .ttf font type doesn't re ...

Introducing fresh background images for every MDlite card

Hello StackOverflow community! I am a long-time user and this is my first post. I need some help with adding two MDlite cards to my website, each with a different background image. However, I'm encountering an issue where the last-declared background ...

Error encountered while attempting Ajax call in jQuery: "Uncaught TypeError: Cannot read property 'error' of null"

Having some trouble with my ajax call in jQuery. When I try to make the call, Chrome is giving me an error message saying "uncaught typeerror cannot read property 'error' of null." This prevents the class 'success' from being added. An ...