Highlight the menu when the user is actively browsing this section

I have successfully implemented a fixed and sticky menu with a progress bar that resembles an underline. However, I am facing an issue with positioning the progress bar correctly under each menu section. For example, when a user hovers over the first section (call data), the progress bar loads, but it does not align perfectly under the menu. I need assistance in fixing this issue to ensure that the menu progress bar lines up correctly with each section.

Is there anyone who can help me correct this? I want the Menu Progress bar to be displayed directly under each section.

Here is the code on Fiddle. Please test it on a larger screen!

I have tried searching online for a solution but haven't found any relevant information so far.

.nav {
  position: sticky;
  top: 0;
  overflow: hidden;
  background-color: white;
  position: -webkit-sticky;
  /* Safari */
  position: -moz-sticky;
  /* firefox */
  z-index: 5;
}

.nav a {
  float: left;
  display: block;
  color: black;
  text-align: center;
  padding: 14px 24px;
  text-decoration: none;
  font-size: 17px;
}

.nav a:hover {
  color: #42AC82;
}

.nav .icon {
  display: none;
}

#menu_progress {
  height: 3px;
  width: 0%;
  float: left;
  background: #42AC82;
  position: sticky;
  top: 0;
  overflow: hidden;
  position: -webkit-sticky;
  /* Safari */
  position: -moz-sticky;
  /* firefox */
  z-index: 5;
}
<div class="nav" id="myNav">
  <a href="#call-data">Call Data</a>
  <a href="#source">Source</a>
  <a href="#lead">Lead</a>
  <a href="#address">Address</a>
  <a href="#motivation">Motivation</a>
  <a href="#property">Property</a>
  <a href="#visit">Visit</a>
  <a href="#finish">Finish</a>
  <a href="javascript:void(0);" class="icon" onclick="myFunction()">
    <i class="fa fa-bars"></i>
  </a>
  <div id="menu_progress"></div>
</div>

 //menu progress bar
$('.nav a').mouseover(function() {
var x = $(this).offset().left + $(this).width();
$('#menu_progress').animate({ width: x });
});

Answer №1

Are you aiming to achieve this? I have implemented the following modifications, please check them on a larger screen. Thank you

  1. Replaced $(this).offset() with $(this).position().left to get the offset width relative to the parent instead of the page.

  2. Replaced $(this).width() with $(this).outerWidth() to include the padding within the width of the container.

  3. Shifted <div id="menu_progress"> outside the

    <div class="nav" id="myNav">
    .

//menu progress bar
$(".nav a").mouseover(function() {
  var x = $(this).position().left + $(this).outerWidth();
  $("#menu_progress").animate({
    width: x
  });
});
.nav {
  position: sticky;
  top: 0;
  overflow: hidden;
  background-color: white;
  position: -webkit-sticky;
  /* Safari */
  position: -moz-sticky;
  /* firefox */
  z-index: 5;
}

.nav a {
  float: left;
  display: block;
  color: black;
  text-align: center;
  padding: 14px 24px;
  text-decoration: none;
  font-size: 17px;
}

.nav a:hover {
  color: #42AC82;
}

.nav .icon {
  display: none;
}

#menu_progress {
  height: 3px;
  width: 0%;
  float: left;
  background: #42AC82;
  position: sticky;
  top: 0;
  overflow: hidden;
  position: -webkit-sticky;
  /* Safari */
  position: -moz-sticky;
  /* firefox */
  z-index: 5;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="nav" id="myNav">
  <a href="#call-data">Call Data</a>
  <a href="#source">Source</a>
  <a href="#lead">Lead</a>
  <a href="#address">Address</a>
  <a href="#motivation">Motivation</a>
  <a href="#property">Property</a>
  <a href="#visit">Visit</a>
  <a href="#finish">Finish</a>
  <a href="javascript:void(0);" class="icon" onclick="myFunction()">
    <i class="fa fa-bars"></i>
  </a>
</div>
<div id="menu_progress"></div>

Answer №2

I have modified the code to reset the progress bar when exiting the menu and to stop the continuous animation when quickly hovering over the menu items.

//menu progress bar
$(".nav a").mouseover(function() {
  $("#menu_progress").stop();    
  var x = $(this).offset().left + $(this).outerWidth();
  $("#menu_progress").animate({
    width: x
  });
});

$(".nav").mouseout(function() {
  $("#menu_progress").stop(); 
  $("#menu_progress").animate({width: 0});
});
.nav {
  position: sticky;
  top: 0;
  overflow: hidden;
  background-color: white;
  position: -webkit-sticky;
  /* Safari */
  position: -moz-sticky;
  /* firefox */
  z-index: 5;
}

.nav a {
  float: left;
  display: block;
  color: black;
  text-align: center;
  padding: 14px 24px;
  text-decoration: none;
  font-size: 17px;
}

.nav a:hover {
  color: #42AC82;
}

.nav .icon {
  display: none;
}

#menu_progress {
  height: 3px;
  width: 0%;
  float: left;
  background: #42AC82;
  position: sticky;
  top: 0;
  overflow: hidden;
  position: -webkit-sticky;
  /* Safari */
  position: -moz-sticky;
  /* firefox */
  z-index: 5;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="nav" id="myNav">
  <a href="#call-data">Call Data</a>
  <a href="#source">Source</a>
  <a href="#lead">Lead</a>
  <a href="#address">Address</a>
  <a href="#motivation">Motivation</a>
  <a href="#property">Property</a>
  <a href="#visit">Visit</a>
  <a href="#finish">Finish</a>
  <a href="javascript:void(0);" class="icon" onclick="myFunction()">
    <i class="fa fa-bars"></i>
  </a>
</div>
<div id="menu_progress"></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

Ensuring consistent column height in HTML Tables

Hey there, I'm just starting out with CSS and I have a question about table design. I have three separate tables below and I want to ensure that all the columns in each table are of equal height. Is there an easy way to achieve this? Any suggestions o ...

Attempting to conditionally apply CSS to a component based on a prop, but unfortunately it is not functioning as expected

.storyMobile{ color:green; } .storyWeb{ color:red; } <div class="storyMobile"> hii </div> <div class="storyWeb"> hii </div> //main view <div> <story :story="stories[0]"/> </div> //here it prints ...

Django redirects to an alternative template instead of the default one

After renaming my login.html file to login1.html instead of deleting it, I have been using Django-registration and Django-registration-views from Github. However, despite this change, Django continues to call registration/login1.html. Is there a way for me ...

Learn how to style inline HTML elements, such as headings, labels, input fields, and buttons, which are typically displayed as block elements

Title. For my dictionary chrome extension, I am looking to inline a label, input field, and button all in one line. This is what I want to achieve: Define:[ INPUT ] DEFINE The brackets represent the input field and "DEFINE" is the ...

Utilizing React JS: Displaying or Concealing Specific Components Based on the URL Path

Is there a way to dynamically change the navbar items based on the URL without having separate navbar components for each side? My current navbar design features 3 links on the left side and 3 links on the right, but I want to display only one side at a ti ...

Scroll with Angular to Move Elements

Is there a way to convert this Jquery Code into an angularJs directive? http://jsfiddle.net/MMZ2h/4/ var lastScrollTop = 0; $("div").scroll(function (event) { var st = $(this).scrollTop(); if (st > lastScrollTop) { $('img').a ...

Unable to adjust custom link menu font size

I have designed a unique custom menu called SANDUSKY with the css classes of no-hover. It is positioned alongside HOME, ABOUT, GALLERY, STORE, CONTACT which serve as the navigation menus. While attempting to customize SANDUSKY, I successfully altered the c ...

gulp-minify-css-names not altering any files during processing

For quite some time, I have been on the search for a solution that can assist me in automating the process of renaming/minifying class names in HTML, JS, CSS files simultaneously throughout my entire project using gulp. While there are many solutions avai ...

The primary view seamlessly integrates with the page following the invocation of the partial view

Whenever the button is clicked, a different partial view is returned based on the selected value from the drop-down list. Controller: [HttpPost] public ActionResult Foo(SomeViewModel VM) { var model = VM; if (Request.IsAjaxRequest()) { ...

Adjust the Bootstrap flex settings to display only 3 cards instead of the default 4 cards - HTML

We currently have a bootstrap setup that displays 4 horizontal scroll cards by default. However, we are looking to only show 3 cards in view and allow the 4th card to be viewed by scrolling. Code On mobile view, the code is still displaying 4 tiles even ...

What is the process for adding information to the database when the checkbox is selected?

In my database, I have a table named "absent" that stores the ID of students and classes for absent students. Below is a table with student names, each accompanied by a checkbox. I am trying to insert into the absent table the IDs of students whose check ...

Displaying image while processing substantial ajax data

I implemented code to toggle the display of a loading image on my web page while making ajax requests. $(document).ajaxStart(function(){ $("#please_wait").css("display","block"); }); $(document).ajaxComplete(function(){ $("#please_wait").css(" ...

Can you explain the distinction between these two jQuery selectors?

Is the result of $('#e > #f') the same as $('#e #f') when using this markup: <div id="e"> <div id="f"></div> </div> ...

I have implemented the appropriate code to showcase a background color, yet it doesn't seem to be appearing on the screen. Additionally, I am utilizing Sass in this project

Could someone help me understand why the background color is not showing on my website? This is the CSS I am using html { font-size: 100%; -webkit-box-sizing: border-box; box-sizing: border-box; } *, *::before, *::after { -webkit-box-sizi ...

Parameter for Ajax URL

As a beginner in the world of Ajax, I'm on a mission to grasp the inner workings of this technology. I came across a tutorial on w3schools that sparked my curiosity. In the code snippet below, the 'url' is defined as demo_ajax_load.txt. Wil ...

Tips for relocating a CSS button

I have designed two buttons using css and I am looking to align them below the title "forecast customer activity". Due to extensive styling in css, the code might appear lengthy. Requesting assistance with a solution after reviewing the following code snip ...

The radio button default selection is checked, but the desired styles are not being applied

My webpage features two radio buttons, "No" and "Yes," with the default selection being "No." I have implemented CSS styles for the checked elements, but they only work once physically selected. My goal is to apply these styles immediately on page load wit ...

Switch a div to a computed location

I'm having trouble getting this code to animate a div to a calculated position. Can someone please help me troubleshoot? $(document).ready(function() { var is_Clicked = false; $("#togglebutton").click(function() { if (is_Cli ...

Troubleshooting: Issue with jQuery Ajax not receiving JSON response

Here is the JavaScript code I am using: $.ajax({ url: 'CheckColorPrice.php', type: 'POST', data: { url: '<? ...

Loading screen displayed while transitioning between routes within Angular

I have been struggling to implement a loading spinner in my project. How can I display a loading screen when changing routes in Angular? Here is the HTML code snippet: <div *ngIf="showLoadingIndicator" class="spinner"></div> ...