Animating lines with JQuery beneath navigation links

Currently in the process of revamping my portfolio website, and it's still a work in progress. Recently stumbled upon a jquery tutorial that enables a line to animate under anchor elements on hover, which I find quite intriguing. However, I am facing two issues with this feature.

  1. While the line gets the appropriate width and left position on hover, it fails to retain these attributes when not active.

  2. Uncertain about how to update the active element so that the line remains with the link that was clicked. At present, the first anchor tag is always considered the active element, causing the line to animate back to it. I believe a click function is needed to clear the current active element and assign the active class to the new one, but I'm not entirely sure.

The code can be found below, or you can view the issue live on my site Here

Below is the code snippet:

HTML:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<header id="header">
    <div class="container">
        <nav id="example-one">
            <ul>
                <li><a class="nav" href="#home">Welcome</a></li>
                <li  class="current_page_item"><a class="nav" href="#featuredWork">Work</a></li>
                <li><a class="nav" href="#caseStudy">Case Study</a></li>
                <li><a class="nav" href="#about">About</a></li>
                <li><a class="nav" href="#contact">Contact</a></li>
            </ul>
        </nav>
    </div>
</header>

CSS:

#magic-line { 
position: absolute;
bottom: 10px; 
/*left: 0; 
width: 100px; */
height: 3px;
background: #fe4902;
}

JQuery:

//Magic line

$(function() {


/* Add Magic Line markup via JavaScript, because it ain't gonna work without */

$("#example-one").append("<li id='magic-line'></li>");


/* Cache it */
var $magicLine = $("#magic-line");

$magicLine
.width($(".current_page_item").width())
.css("left", $(".current_page_item a").position().left)
.data("origLeft", $magicLine.position().left)
.data("origWidth", $magicLine.width());

$("#example-one li").find("a").hover(function() {
$el = $(this);
leftPos = $el.position().left;
newWidth = $el.parent().width();

$magicLine.stop().animate({
    left: leftPos,
    width: newWidth
});
}, function() {
$magicLine.stop().animate({
    left: $magicLine.data("origLeft"),
    width: $magicLine.data("origWidth")
});    
});


});

Appreciate any assistance or guidance given. Thank you in advance.

Answer №1

It seems like this could be the solution you're looking for.

All you have to do is remove

function() {
        $magicLine.stop().animate({
            left: $magicLine.data("origLeft"),
            width: $magicLine.data("origWidth")
        });    
    });

This might be causing the slider to quickly return to its original position. Are you looking for the slider to only stay in place when clicked?

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

The Beauty of HTML5 UI

Among the plethora of HTML5 UI frameworks available, I stumbled upon a few that caught my eye: Kendo Wijmo jqWidgets Zebra Sencha SproutCore YUI XUI Shield UI The abundance of options has left me feeling overwhelmed. Upon exploring some of them, I notic ...

The lower section of the scrollbar is not visible

Whenever the vertical scroll bar appears on my website, the bottom half of it seems to be missing. For a live demonstration, you can visit the site HERE (navigate to the "FURTHER READING" tab). HTML: <!DOCTYPE html> <html lang="en"> <h ...

Session Comparison Tool

Currently, I am in the midst of a project that requires the capability to compare multiple products. The client has requested a page that can directly link to such comparisons. To fulfill this requirement, I have decided to utilize URLs like this: /compar ...

Form submission is not functioning as expected

I am having trouble with my form submission. When I try to submit the form, nothing happens. I have tried various solutions but have not been able to resolve the issue. I have reviewed all available resources but still cannot figure out why my code is not ...

Issues with scaling background videos in HTML5

I'm having trouble making a video scale properly with the browser window while still covering the entire area. Does anyone know why my current approach isn't working? HTML: <div class="bgVideoWrap"> <video id="bgVideo" loop="true" aut ...

Transferring multiple parameters between components using Navigate and ParamMap

My concern is that I'm unsure of how to pass multiple parameters through the component's route. I need to send two attributes. Is there a specific syntax required in defining the route? Below is my desired functionality for passing the attribut ...

Is there a way for me to extend an absolute div to the full width of its grandparent when it is within an absolute parent div?

Here is a structured example of my HTML and CSS: <div class="grandparent"> <div class="row">...</div> <div class="absolute-parent"> <div class="absolute-child">...</div> ...

The dropdown checklist feature, DropDownCheckList, is experiencing technical difficulties and is currently not

I have downloaded the jQuery plugin version 1.4 from this link and saved it on my local machine. In my HTML code, I am referencing it locally and also using jQuery version 1.7.1. However, when I try to create a dropdown checklist on page load, it is not ...

Overlaying a div on top of an iframe

Struggling to make this work for a while now. It seems like a small issue related to CSS. The image isn't overlaying the IFrame at the top of the page as expected, it's going straight to the bottom. Here is the code snippet: .overlay{ width: ...

Adjusting window size when page is resized

While browsing through SO, I stumbled upon this interesting piece of code: var w = window, d = document, e = d.documentElement, g = d.getElementsByTagName('body')[0], x = w.innerWidth || e.clientWidth || g.clientWidth, y = w. ...

What is the best way to make a child div's height match its parent's height?

I am facing an issue with aligning 2 divs on the same line, separated by a vertical line while ensuring that the line always has the height of the parent div. Despite trying various solutions suggested online, none seem to work for me. I cannot use positi ...

Equal dimensions for multiple div elements in both height and width

Looking to create responsive blocks using jQuery. I have two cube blocks with different widths. After writing a few lines of code to make them responsive, the second block now takes the height of the first div and changes on resize. Check out this example ...

How can I resolve the issue of not returning anything ondrop in my code?

Whenever I drop my div containing an image, I don't see anything. And when I try to access its ID, I get a null value. How can I extract information from a div with an image and append a row with it? For code samples or to check the codepen example, v ...

Design an ARIA menu role with HTML and CSS code

Currently, my code is set up, but I'm unsure about integrating the tab/arrows to navigate to the sub-menu. You can view the code on jsfiddle: https://jsfiddle.net/Fep5Q/60/ This snippet shows a portion of the HTML code I've implemented: <di ...

turn on labels for every individual cell in the bar graph

I'm working on setting labels of A, B, and C for each bar chart cell to display all my data on the chart. I attempted using data.addColumn('string', 'Alphabets'); but it's not functioning as expected. It seems like a simple ta ...

What is the best way to pinpoint particular text within a paragraph that includes numerous line breaks?

So here is the puzzling situation I'm grappling with. Here's a peek at the HTML snippet: <p>This paragraph has <br><br> two unusual line breaks <br><br> and it happens TWICE!</p> The problem arises when tryi ...

optimal method for displaying HTML strings with components in Vue

My website is a Blog/News site featuring posts that contain HTML content stored in the database. In addition to posts, I also want to include elements like sliders which cannot be generated using v-html. I explored Vue's Render Functions to find a so ...

Unable to modify the hover color on the image or icon

After creating a circle icon with an image in the center, I wanted to make the image change colors on hover. The main focus of the icon is the circle itself. In my attempt to achieve this effect, I included the following code for the circle icon: .circle- ...

Unbelievable attribute: sup causing peculiar styling in link element

We've recently come across an issue where the text-decoration of an underline in an anchor tag appears strange when there is a sup element present. The visual glitch looks like this: For reference, here's the HTML structure we are working with: ...

Implement a select element with an onchange event that triggers an AJAX

As a newcomer to Javascript, I've been struggling to find a solution to my issue. The goal is to add an additional select option when the previous select option is changed. I attempted to implement the code below, but it's not functioning correct ...