How about "Designing a dynamic navigation bar feature where a moving bar tracks the user's cursor over each element?"

I am currently working on developing a navigation bar that includes standard navigation elements along with an indicator bar positioned below the active page. My objective is to have this bar dynamically move beneath whichever element the user hovers over and adjust its size automatically based on predefined parameters, possibly considering the length of the text. While I have successfully created the navigation bar structure, I am unsure about how to implement this interactive effect. Should I continuously calculate the mouse's current position and factor in the variance between its current location and the hover point? The secondary goal of allowing for resizing is less critical but still desirable.

Here is a glimpse of what I have accomplished so far:

<header class="header">
            <div class="logo"> 
                <nav id="nav_bar">
                    <ul id="nav_ul">
                        <li><a href="#">apples</a></li>
                        <li><a href="#">bananas</a></li>
                        <li><a href="#">tomatos</a></li>
                        <li><a href="#">onions</a></li>
                    </ul>
                    <div id="container">
                        <div id="bar"></div>
                    </div>
                </nav>
            </div>
        </header>

CSS:

#nav_ul a{
    color: #685e6d;
    text-decoration: none;
    display: inline-block;
   -webkit-transition: color 0.4s ease-in-out;
   -moz-transition: color 0.4s ease-in-out;
   -ms-transition: color 0.4s ease-in-out;
   -o-transition: color 0.4s ease-in-out;
   transition: color 0.4s ease-in-out;
}

#nav_ul{
    display: inline-block;
    list-style-type: none;
}

#nav_ul li{
    display: inline-block;
    position: relative;
    left: 90px;
    bottom: 30px;
    font-size: 19px;
    margin-left: 40px;
    font-weight: 100;
    font-size: 16px;
}

#nav_ul a:hover{
    color: #4ad1fd;
}


#container{
    position: relative;
    left: 167px;
    height: auto;
    width: 530px;
    top: 5px;
}

#bar{
    position: absolute;
    left: 0;
    bottom: 0;
    height: 7px;
    width: 107px;
    background-color: #ffcc00;
    border-radius: 3px;
}

JSfiddle

Considering something similar to the image below:

Answer №1

If you want to add a dynamic touch to your menu, consider implementing a lavalamp effect like the example provided below...

/* ---- custom styles ------*/
html, body, div, a {
  margin: 0;
  padding: 0;
  border: 0;
  font: inherit;
  font-size: 100%;
  vertical-align: baseline; }

html {
  line-height: 1; }


/* --- basic styling ----*/

body {
  font-family: "Unica One";
  font-size: 1.5em;
  background: #f2f2f2;
  text-shadow: 0 1px 0 white; }

/* --- implementation specifics ----*/

.nav {
  text-align: center;
  overflow: hidden;
  margin: 2em auto;
  width: 480px;
  position: relative; }
  .nav a {
    display: block;
    position: relative;
float: left;
padding: 1em 0 2em;
    width: 25%;
text-decoration: none;
  color: #393939;
  -webkit-transition: .7s;
  -moz-transition: .7s;
  -o-transition: .7s;
  -ms-transition: .7s;
  transition: .7s; }
  .nav a:hover {
    color: #c6342e; }

.effect {
position: absolute;
  left: -12.5%;
  -webkit-transition: 0.7s ease-in-out;
  -moz-transition: 0.7s ease-in-out;
  -o-transition: 0.7s ease-in-out;
  -ms-transition: 0.7s ease-in-out;
  transition: 0.7s ease-in-out; }

.nav a:nth-child(1):hover ~ .effect {
  left: 12.5%; }
.nav a:nth-child(2):hover ~ .effect {
  left: 37.5%; }
.nav a:nth-child(3):hover ~ .effect {
  left: 62.5%; }
.nav a:nth-child(4):hover ~ .effect {
  left: 87.5%; }

/* ----- underline animation -----*/

.ph-line-nav .effect {
  width: 90px;
  height: 2px;
  bottom: 36px;
  background: #c6342e;
  box-shadow: 0 1px 0 white; 
  margin-left:-45px;
}
<div class="ph-line-nav nav">
<a href="#">Home</a>
<a href="#">About</a>
<a href="#">Gallery</a>
<a href="#">Contact</a>
<div class="effect"></div>
</div>

For more details and examples, visit

Answer №2

Check out this jQuery solution: (JSFiddle for the code snippet if it doesn't work)

function MoveBar(element) {
    var bar = $('#bar');
    var width = element.outerWidth();
    var leftPos = element.offset().left;
    bar.animate({
        width: width,
        left: leftPos
    });
}
var currentPage = $('#nav_ul li a.current'); // Set the current page
MoveBar(currentPage);
$('#nav_ul li a').hover(function () {
    MoveBar($(this));
}, function () {
    MoveBar(currentPage);
});
#nav_ul a {
    color: #685e6d;
    text-decoration: none;
    display: inline-block;
    -webkit-transition: color 0.4s ease-in-out;
    -moz-transition: color 0.4s ease-in-out;
    -ms-transition: color 0.4s ease-in-out;
    -o-transition: color 0.4s ease-in-out;
    transition: color 0.4s ease-in-out;
}

#nav_ul {
    display: inline-block;
    list-style-type: none;
}

#nav_ul li {
    display: inline-block;
    position: relative;
    left: 90px;
    bottom: 30px;
    font-size: 19px;
    margin-left: 40px;
    font-weight: 100;
    font-size: 16px;
}

#nav_ul a:hover {
    color: #4ad1fd;
}

#container {
    position: relative;
    left: 0;
    height: auto;
    width: 530px;
    top: 5px;
}

#bar {
    position: absolute;
    left: 0;
    bottom: 0;
    height: 7px;
    width: 107px;
    background-color: #ffcc00;
    border-radius: 3px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<header class="header">
    <div class="logo">
        <nav id="nav_bar">
            <ul id="nav_ul">
                <li><a href="#" class="current">apples</a>
                </li>
                <li><a href="#">bananas</a>
                </li>
                <li><a href="#">tomatos</a>
                </li>
                <li><a href="#">onions</a>
                </li>
            </ul>
            <div id="container">
                <div id="bar"></div>
            </div>
        </nav>
    </div>
</header>

It may need some adjusting, but the functionality should be what you're looking for.

Answer №3

By including the CSS rule #nav_ul a:hover, you can easily create a static colored bar under the items on hover by adding

border-bottom-style: solid; border-color:<color>
. This simple addition will provide a visual indication of what the user is currently hovering over, without any sliding or animation effects.

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

Tips for positioning images in the center of a footer

Hello, I am new to coding and would like some help adjusting the code for this website: The footer section at the bottom of each page features a row of logos. My goal is to make sure that these logo images display properly on mobile devices (without dropp ...

Different sized image grid display

Could you please take a look at this screenshot: I am facing an issue with the grid layout. The first image in the grid is too short, which results in a big gap between the upper left and lower left pictures. Here is what it looks like currently: Is ther ...

A helpful guide on how to dynamically input database values into the href attribute of an 'a' tag

How do I successfully pass an ID to href in my code? When I try to return it via req.params, it keeps coming back as undefined. I need the ID from the URL in order to use the findOne method to access the data stored in the database. I've searched thr ...

Obtain a nested array of objects from Mongoose's Model.find() method and then make modifications to the inner array

I need to retrieve an array of objects with a specific ID from my database within a server route, and then update a property of an object within that array (rather than returning just the objectID, I want to return the Document as an object with the specif ...

Placing a material UI Radio button within a row cell on a table

Here is the current design snapshot: https://i.sstatic.net/yRFWD.png I am working with Material UI's radio button component and I want to ensure that each row in the table can be selected only once. Although I am able to select a single row using the ...

The functionality of jQuery.hover with AJAX is malfunctioning

I am facing an issue with my jquery hover combined with $.post method. My initial intention was to create a series of select buttons where hovering would trigger a change in the image being displayed (the image path would be loaded via $.post). The image ...

Issue: Upon attempting to connect to a vsftpd server deployed on AWS using the npm module ssh2-sftp-client, all designated authentication methods have failed

Code snippet for connecting to the vsftpd server sftp.connect({ host: "3.6.75.65" port: "22" username: "ashish-ftp" password: "*******" }) .then(() => { console.log("result") }) .catch((err)=>{ ...

Enhance Vuetify functionality using TypeScript for custom components

I'm facing a challenge with extending a Vuetify component and setting default props in TypeScript. While I had success doing this in JavaScript, I am struggling to do the same in TS. Below is an example of how the Component was implemented in JS: imp ...

Dealing with alternating rows in a dynamic manner: tips and tricks

Exploring an example scenario: (view live demonstration) Sample HTML structure: <div class="wrapper"> <div city_id="1" class="odd">Street Name #1 in city 1</div> <div city_id="3" class="even">Street Name #2 in city 3</d ...

Storing the .NET Framework viewmodel in its own individual Javascript file

I have a question about structuring my design for SoC (Separation of Concerns). I currently have the following files: testController.cs testViewModel.cs testView.cshtml testScript.js Currently, I generate data in the controller, populate the testViewMod ...

Attempting to generate a dynamic animation of a bouncing sphere confined within a boundary using components, but encountering

I'm new to JavaScript and currently working on a project involving a bouncing ball inside a canvas. I was able to achieve this before, but now I'm attempting to recreate it using objects. However, despite not encountering any errors, the animatio ...

Utilizing JQuery to link keyboard inputs with function triggers

Is there a way to detect when a keyboard key is pressed and then execute this line of code? $('#submit_p').on('click',function(event){...} Appreciate it. ...

Exploring JavaScript capabilities with Google - managing and updating object names with numbers

After importing JSON data into Google Scripts, I am able to access various objects using the code snippet below: var doc = Utilities.jsonParse(txt); For most objects, I can easily retrieve specific properties like this... var date = doc.data1.dateTime; ...

An unexpected issue occurred: Unable to invoke method on NPObject

I am new to JSON and having trouble accessing object data. Here is the code snippet: <!doctype html> <html> <head> <meta charset="utf-8"> <title>ajax </title> </head> <body> <p id="p"></p& ...

The Amadeus flight booking feature is running smoothly, however, the Hotel Booking API is not functioning properly

When I initially integrated Amadeus for flight booking, everything worked smoothly. However, issues arose when trying to integrate hotel bookings. When utilizing the nodejs library of Amadeus for hotel offers, an error was encountered stating that the acce ...

ajax is not functioning properly on scrolling div

I recently created a 1 on 1 support chat using ajax and it's been working well. However, I've encountered an issue where the div expands indefinitely when refreshed by ajax and is taller than its original height. I've tried using overflow: a ...

Link to text on tablet and phone

On my website, I have included a phone number in the HTML as text. When viewed on an iPad tablet, it automatically interprets the phone number and turns it into an underline blue link. Is there a way to format the phone number in a different style so it d ...

transferring a JavaScript variable to a different PHP page with JavaScript and retrieving the data

How can I pass a JavaScript variable to another page? Here is the code snippet provided: Code ` $('#disInfo').on('click','tr',function(){ var obj = $(this); var result= obj.find('td').eq(1).html(); wi ...

Angular route unable to detect Passport User Session

The navigation bar in the header features buttons for Register, Login, and Become a Seller. Upon user login, it displays logout and view profile buttons. This functionality is operational on all routes except one, causing a client-side error. user not def ...

Tips on adjusting the color of a link once it has been clicked?

Looking for a link with a specific style? a { color: #999; &:hover { color: #008da0; } &:visited { color: #999; } /* I added this to test if it's gonna fix it, but it didn't */ } Upon clicking the link, it turns blue and remains s ...