When I use .fadeToggle, my div transitions smoothly between visible and hidden states

Looking to create a sleek navigation menu that showcases a colored square when hovered over? I'm currently experiencing an issue where the squares are not aligning correctly with the items being hovered. Switching the position to absolute would likely solve this, but I require it to remain relative for location purposes. Any guidance would be greatly appreciated.

CodePen

HTML:

<div id='navbar'>
  <ul class='navmen'>
    <a href='#' class='link'>
      <li class='navitem' id='home'>HOME</li>
    </a>
    <a href='#' class='link'>
      <li class='navitem' id='item2'>ITEM2</li>
    </a>
    <a href='#' class='link'>
      <li class='navitem' id='item3'>ITEM3</li>
    </a>
    <a href='#' class='link'>
      <li class='navitem' id='item4'>ITEM4</li>
    </a>
  </ul>
</div>
<ul class='navmen'>
  <li class='itemam'>
    <div class='divmen' id='divred'></div>
  </li>
  <li class='itemam'>
    <div class='divmen' id='divorange'></div>
  </li>
  <li class='itemam'>
    <div class='divmen' id='divblue'></div>
  </li>
  <li class='itemam'>
    <div class='divmen' id='divgreen'></div>
  </li>
</ul> 

CSS:

.navmen {
  display: inline;
  position: relative;
  left: 230px;
}

.link {
  text-decoration: none;
  color: black;
  display: inline;
}

.navitem {
  font-family: 'Pragati Narrow', sans-serif;
  display: inline;
  padding-left: 80px;
  padding-right: 80px;
  font-size: 22px;
}

.navitem:hover {
  background-color: #CCCCCC;
}

.divmen {
  background-color: black;
  width: 210px;
  height: 10px;
}

#divred {
  background-color: red;
  position: relative;
  bottom: 77px;
  left: 40px;
  display: none;
}

#divorange {
  background-color: orange;
  position: relative;
  left: 250px;
  bottom: 107px;
  display: none;
}

#divblue {
  background-color: blue;
  position: relative;
  left: 460px;
  bottom: 137px;
  display: none;
}

#divgreen {
  background-color: green;
  position: relative;
  bottom: 167px;
  left: 670px;
  display: none;
}

JavaScript:

$(document).ready(function() {
  $('#home').hover(function() {
    $('#divred').fadeToggle('slow')
  })
});
$(document).ready(function() {
  $('#item2').hover(function() {
    $('#divorange').fadeToggle('slow')
  })
});
$(document).ready(function() {
  $('#item3').hover(function() {
    $('#divblue').fadeToggle('slow')
  })
});
$(document).ready(function() {
  $('#item4').hover(function() {
    $('#divgreen').fadeToggle('slow')
  })
});

Answer №1

please review the code below:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
  $('#home').hover(function() {
    $('#divred').fadeToggle('slow')
  })
  $('#item2').hover(function() {
    $('#divorange').fadeToggle('slow')
  })
  $('#item3').hover(function() {
    $('#divblue').fadeToggle('slow')
  })
  $('#item4').hover(function() {
    $('#divgreen').fadeToggle('slow')
  })
});
</script>
<style type="text/css">
.navmen {
  display: inline;
  position: relative;
  left: 230px;
}

.link {
  text-decoration: none;
  color: black;
  display: inline;
}

.navitem {
  font-family: 'Pragati Narrow', sans-serif;
  display: inline;
  padding-left: 80px;
  padding-right: 80px;
  font-size: 22px;
}

.navitem:hover {
  background-color: #CCCCCC;
}

.itemam{
  height: 18px;
}

.divmen {
  width: 210px;
  display:none;
}
</style>
</head>
<body> 
<div id='navbar'>
  <ul class='navmen'>
    <a href='#' class='link'>
      <li class='navitem' id='home'>HOME</li>
    </a>
    <a href='#' class='link'>
      <li class='navitem' id='item2'>ITEM2</li>
    </a>
    <a href='#' class='link'>
      <li class='navitem' id='item3'>ITEM3</li>
    </a>
    <a href='#' class='link'>
      <li class='navitem' id='item4'>ITEM4</li>
    </a>
  </ul>
</div>
<ul class='navmen'>
  <li class='itemam'>
    <div class='divmen' id='divred'>divred</div>
  </li>
  <li class='itemam'>
    <div class='divmen' id='divorange'>divorange</div>
  </li>
  <li class='itemam'>
    <div class='divmen' id='divblue'>divblue</div>
  </li>
  <li class='itemam'>
    <div class='divmen' id='divgreen'>divgreen</div>
  </li>
</ul>
</body>
</html>

Answer №2

If you're looking for a solution, this might be what you need.

No JavaScript or jQuery needed

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
.navmen {
  margin-top: 25px;
  list-style-type: none;
  text-align: center;
}
.navitem {
  font-family: 'Pragati Narrow', sans-serif;
  display: inline-block;
  border-top: 10px solid transparent;
  transition: border .5s ease;
}
.navitem a {
  text-decoration: none;
  padding-left: 20px;
  padding-right: 20px;
  font-size: 22px;
  display: block;
  color: #000;
}
.navitem:hover a {
  background: #ccc;
}
.red:hover {
  border-top-color: red;
}
.green:hover {
  border-top-color: green;
}
.blue:hover {
  border-top-color: blue;
}
<div id="navbar">
  <ul class="navmen">
    <li class="navitem red" id="home"><a href="#">HOME</a>
    </li>
    <li class="navitem green" id="item2"><a href="#">ITEM2</a>
    </li>
    <li class="navitem blue" id="item3"><a href="#">ITEM3</a>
    </li>
  </ul>
</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

Unable to apply border-radius to a specific HTML table

I am facing an issue where the border-radius is not being displayed on a specific HTML table. I am having difficulty in applying rounded corners to the entire table so that the table edges have a 30px curve. Here is an example of what I am looking for: I ...

When using BeautifulSoup, there may be instances where the table element is not always detected, resulting in a 'NoneType' error being returned on occasion

Hello everyone, I am trying to retrieve accurate daily temperatures from www.wunderground.com and encountering the 'NoneType' error occasionally. For instance, the code below attempts to fetch the temperature data from March 1 to March 5, 2020. S ...

Fixed navbar at the bottom of the page that transitions to static when scrolling reaches a certain location

Is it feasible to achieve this using Bootstrap v3? After conducting extensive research, it appears that a custom solution may be necessary. In essence, I am working with a navbar positioned at the bottom of the content area. Upon page load, if the navbar& ...

What is preventing me from updating the value of a variable in this manner?

Trying to understand the reason behind an issue with overwriting a value passed to an angularJS directive using an isolate scope (@). When attempting to change the value of vm.index with the following: vm.index = parseInt(vm.index, 10) It does not seem t ...

Node.js can be used to easily set the values of HTML elements

After successfully setting up a node.js connection to my mysql database and being able to retrieve and input data using connection.query(), I now want to display the database information on my HTML elements. Is there an equivalent of getElementById for t ...

Utilize resources from webpack's bundled npm package assets

I've been racking my brain over this issue for quite some time now, and I'm starting to wonder if it's even achievable. Any assistance on this matter would be greatly appreciated! The npm dilemma I have an npm package that serves as a coll ...

Determining the decimal width of an element using jQuery

I've been searching everywhere for a method to accurately determine the width of an element without rounding. The reason behind this is that I have multiple elements floated to the left with display inline-block and I am attempting to automatically ad ...

What's the best way to fill checkboxes in EJS when editing a route?

As a beginner, I am working on my first project - a simple content/property listings app. I have created a form to collect user data and display it on a show form. The form includes sections for checkboxes and radio buttons, with the values being stored i ...

Animate my banner images only after they have fully loaded using Jquery

I recently came across a banner image slideshow in jQuery, but it seems to be animating even before the images are fully loaded. This results in the slideshow displaying image descriptions without the actual images themselves. Can anyone help me modify th ...

Tips for removing the y-axis line in ChartJs

How can I hide the y axis line in a bubble chart? I attempted to use the code snippet below but it did not work as expected. yAxes: [{ angleLines: { display: false } }] ...

Execute the knockout function using jQuery

There's a scenario where I am trying to trigger a knockout method using jQuery. The Knockout viewModel has already been bound, but I'm unsure of how to call it using jQuery. Below is the snippet of my code: $(document).ready() { form_submit( ...

Using Angular to assign a CSS variable to the before/after pseudo-classes

Having trouble passing a variable with [ngStyle] and reading it on the ::before class, any guidance in the right direction would be much appreciated! CSS: .handle-slider__control { height: 7px; z-index:1; background: $colour-alto; positi ...

What is the best way to obtain a reference to the parent form element within an AngularJS directive?

Here is the HTML code I am working with: <form name="my-form"> <div class="col-sm-4"> <input type="phone" name="mobileNumber" ng-model="form.mobileNumber" value="0439888999" required> </div> </form> In addition, I ha ...

Troubleshooting the issue with the <div> tag in ASP.NET MVC 2 and the "float: left;" property. Any solutions to

Issue with the usage of <div> elements Everything runs smoothly: Text "Whatever" appears in column 1, followed by a radio button: No issues here (text without a radio button): The cell immediately following the text "Whatever" should display in co ...

What is the best method for initializing the value of ng-model as an empty string in AngularJS?

I need help setting the initial value for an attribute in my model. Here's the code I'm using: <input type="text" ng-model="selectedModel.title" name="title" value="" > What I want is for the attribute value to be initially set as an empt ...

Traversing an array of objects to extract and display the key-value pairs for each object

Here is an array I am working with: const cuisines = [ { african: "African" }, { american: "American" }, { arabian: "Arabian" }, { argentine: "Argentine" }, { asian: "Asian" }, { asian_fusion: "Asian Fusion" }, { australian: "Australi ...

The functionality of Angular material input fields is compromised when the css backface property is applied

I utilized this specific example to create a captivating 3D flip animation. <div class="scene scene--card"> <div class="card"> <div class="card__face card__face--front">front</div> <div class="card__face card__face--ba ...

What is the process for eliminating transparency from the overlay feature while utilizing Dojox/Standby?

Currently, I'm using a spinner image to indicate loading while retrieving data from the backend. However, the spinner widget creates a translucent overlay over the control. Is there a way to disable this translucency so that only the spinner is visibl ...

An effective way to connect the ng-model of a <select> element with ng-options within an ng-repeat loop

My task list consists of: [{ Title: "Title1", Position: "9" },{ Title: "Title2", Position: "1" },{ Title: "Title3", Position: "5" },{ Title: "Title4", Position: "7" }] I am attempting to generate a series of <select> ...

Loading Images in Advance with jQuery, Native JavaScript, and CSS

After successfully implementing a method to hover on a small image and load an additional image to the right side of the page, I am now looking to enhance user experience by preloading images. Is there a way to preload an image using JQuery, allowing it t ...