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

Div flexes and compresses smaller than its content dimensions

Here's a link to the fiddle: check it out Take a look at a normal card below: But when the browser window is resized, it shrinks to this: This is the HTML structure: #product-list { overflow-y : scroll !important; height: 100%; } <lin ...

Selenium: Utilizing the get_attribute() function with a CSS query

I am attempting to retrieve the value of the title attribute. There are multiple links on the page and I need to specifically select the first link and extract its title attribute. Here is the selenium command that I have used: self.se.get_attribute("c ...

When using jQuery, the content loaded with the $ajax function only displays after refreshing the page

Located on an external server is a directory containing various .html files, including one named index.html. The server has the ability to load either the folder name or foldername/index.html in its URL. Each html file within the directory loads a corresp ...

The jQuery function is only operational upon page reload

I encountered an issue with a simple code snippet that I am using in a jQuery Mobile page on a multi-page website. The code, which is loaded from a separate file, was working initially but now only works when I reload the page. Upon inspecting the page loa ...

Retrieve the content of a different page and store it as a variable using ajax

Is it possible to assign the content of another HTML page to a JavaScript variable? I attempted: var X = $(http://www.website.com/home).html() Unfortunately, this did not work, even though it seemed like a good idea. Can anyone provide guidance on how t ...

Dealing with unique constraint violation in Mongodb when using insertMany

Currently, I'm in the process of working on a project that involves using node.js and mongodb version 5. In my collection, I have implemented a unique index for the Parcel property. However, during testing, an error is triggered: MongoBulkWriteError: ...

Effective Ways to Add Space Between Label and Textfield in React Using MaterialUI

Recently delving into the realm of React/MUI, I am faced with the challenge of crafting a login form that showcases a table-like structure where labels and TextFields align on the same row. A key requirement is to ensure equal spacing in the label column t ...

Error messages are appearing during the installation of mediasoup

Attempting to install via Command Prompt on Windows 7 64-bit following the command npm cache --force clean ...

Selection pseudo selectors for tooltips are a great way to provide additional

Can someone explain how tooltips change on Medium.com when you double click a word or sentence to reveal options like share and edit? https://i.stack.imgur.com/0EVmH.png ...

Puppeteer throwing an error when querying selectors cannot be done (TypeError: selector.startsWith is not a supported function)

I recently installed Puppeteer and ran into an issue when trying to query a selector. I keep receiving a TypeError: selector.startsWith is not a function error. I attempted to resolve the problem by tweaking the core code and adding toString(), but unfort ...

Ways to make nodejs return a different value instead of an object promise

Within my user.js file, I have an async function designed to retrieve the avatar's location from the database. The code for this operation is as follows: async findavatar(username) { const sql = `SELECT image FROM users WHERE user = "${userna ...

Is there a way to retrieve JSON data from a different domain and incorporate it into my own website?

I am attempting to utilize JSON data from "" and display the data on my website. My goal is to extract the JSON field name "Stats" and retrieve the sub-field name '\"v\"'. You can refer to the documentation provided by purpleair here: h ...

Using API calls to update component state in React-Redux

I am currently working on setting up a React application where clicking on a map marker in one component triggers the re-rendering of another component on the page. This new component should display data retrieved from a database and update the URL accordi ...

Trying to replace all instances of a word in an HTML document with a <span> element, but only for <p>, <span>, and <div> tags. It shouldn't work if the parent node already contains

Here is the HTML code snippet I am working with: <div> hello world <p> the world is round <img src="domain.com/world.jpg"> </p> </div> I am looking to replace the word "world" (or any mixed case variations) with < ...

Using Bootstrap 3 to implement various grid systems within a single website

For a current project I am working on, my goal is to fill the entire .container with 7 columns within my grid system. To achieve this, I plan on adjusting the Less variable (@grid-columns) before compiling a bootstrap.css file at http://getbootstrap.com/cu ...

Fire an event following the execution of $state.go()

Is there a way to activate an event once the state has been modified in ui.router? Specifically, I am utilizing the ionic framework alongside angularJS which incorporates angular-ui-router. Note: Below is the pseudo code I am hoping to implement. $state. ...

Updating switch data on click using Vuejs: A step-by-step guide

Could someone please explain to me how to swap two different sets of data when clicked? For instance, let's say we have the following data: data() { return { data_one: [ { name: "Simo", ...

Adjusting height dynamically with CSS based on font size

Below is a link followed by a span element <a href="#" style="display:inline-block;font-size:900%">12</a> <span display="block">ACTIVE</span> The issue arises when the 'a' element does not have a specific height, causing ...

Is it advisable to incorporate the <main> element in my code?

While browsing through w3schools, I came across the <main> element. However, according to caniuse.com, this element is not supported by IE, Opera Mini, and the UC Browser for Android. Should I opt for using a <main> element instead or stick wi ...

Modifying the value of an animated status bar using the same class but different section

I need the status bars to work individually for each one. It would be great if the buttons also worked accordingly. I have been trying to access the value of "data-bar" without success (the script is able to process the "data-max"). However, the script see ...