Animate your menu on hover with jQuery!

I am experiencing an issue with centering the background image below a link. When using mouseleave, the image does not return exactly to the center of the list. Any assistance on how to resolve this would be greatly appreciated. Thank you!

Here is the reference JsFiddle: JsFiddle


I am creating a simple menu within a list that utilizes animation to follow the cursor onmouseover and return to its original position on mouseleave.

Upon loading the page, I want the image to always appear below the current position. How can I achieve this in a div with margin 0 auto? Example of desired effect can be found here.

/*jQuery*/
$('li').mouseover( function() {    

    var top = $(this).offset().top + $(this).height() + 5;
    var left = $(this).offset().left + ($(this).width() / 2);

    $('#marker').stop().animate( { top: top, left: left  }, 200 );

});

$('li').mouseleave( function() { 

});

I hope the explanation provided is clear enough for you to assist me with resolving this issue!

Answer №1

Include a new style to the current section. Modify the mouse exit behavior. Jsfiddle

Markup

<div id="menu-section">
    <ul>
        <li>Tab1</li>
        <li class="currentsection">Tab2</li>
        <li>Tab3</li>    
        <li>Tab4</li>    
        <li>Tab5</li>        

    </ul>    

    <div id="location-marker">*</div>

</div>

Script

$('li').mouseleave( function() { 
    var top = $('.currentsection').offset().top + $(this).height() + 5;
    var left = $('.currentsection').offset().left + ($(this).width() / 2);

    $('#location-marker').stop().animate( { top: top, left: left  }, 200 );    
});

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

Display options in a dropdown menu according to the value selected in another dropdown menu on an edit form in Laravel

After discovering a solution on another website, I successfully implemented it. However, I am facing an issue when trying to use this setup in my edit form (ideally within the same form as my create form since they both share the same structure). On the ed ...

Retrieve the webpage content (including any iframes) using a Firefox plugin

Hello there! I am currently working on retrieving data from a webpage that is updated using Javascript. Initially, I attempted to create a Java program to periodically fetch this page from the server, but the information was being updated too slowly. The ...

Export Image as Json File

I am currently working on creating a browser extension and I am trying to figure out how to save images locally. My plan is to store the images in a json file, but I'm feeling quite lost on how to accomplish this. Below is the code I have so far, alth ...

Accurate understanding of URL parameters and hashtags

Imagine an HTML document containing two blocks, where the content of only one block can be displayed at a time by toggling between them using menu buttons and/or URL parameters with the same JavaScript functions provided below: <div class="block1&q ...

To access the context menu popup and the first element of a row in a datatable, simply right-click on the row

Currently, I am working on writing code for a datatable where when you right-click a row, two actions should be triggered: a context menu should appear, and I need to retrieve the first cell of that row to pass it to my Ajax function. I have managed to wr ...

What is the maximum character limit for the JQuery validation plugin?

Currently, I am utilizing the JQuery validation plugin in my project. My goal is to set a maxlength for one of the fields. Here's an example of how it can be done by defining rules externally: rules: { Message: { required: false, maxLe ...

Creating golden phone numbers from a numerical series using JQuery

My latest work assignment involves generating gold numbers from a number series such as 52xxxxxx. Gold numbers could be examples like 52999999, 52727272, or something similar. I'm feeling a bit overwhelmed and unsure of where to begin with this task. ...

Challenge with Bootstrap Popover: Unable to Capture Button Click Event inside Popover

First of all, here's a link to the problem on jsfiddle: jsfiddle.net The issue I'm facing is with a popover that contains html content including a button with the class "click_me". I've set up jQuery to listen for a click on this button and ...

After integrating Vue + Inertia into my Laravel 10 project, I encountered an issue where I am receiving an error stating 'autocomplete not a function' when trying to load a Vue

BACKGROUND: My Laravel application is well-established and developed using Laravel, Blade, Javascript, and JQuery. Loading a blade view from the Laravel section of the site does not show any errors in Chrome dev tools. It's important to note that I ut ...

Challenges arise when utilizing CSS3 animations in conjunction with transitions triggered by toggling a JavaScript class

Struggling to activate an animation while updating a class using JavaScript for a PhoneGap app. Planning on utilizing -webkit- prefixes for compatibility. However, the animations are currently unresponsive in Chrome during testing, both when applied to th ...

What is the best way to determine the accurate size of a div's content?

There are 2 blocks, and the first one has a click handler that assigns the block's scrollWidth to its width property. There is no padding or borders, but when the property is assigned, one word wraps to the next line. The issue seems to be that scrol ...

Tips for creating multiple functions within a single JavaScript function

Is there a way to combine these two similar functions into one in order to compress the JavaScript code? They both serve the same purpose but target different CSS classes. The goal is to highlight different images when hovering over specific list items - ...

Obtain a collection of data- attribute values using jQuery

I am attempting to extract all the values from the data-hiringurl attributes found on this particular page . When I used var data = $("li").attr('data-hiringurl'); and var data = $("li").data('hiringurl'); in the console, an error mess ...

Steps to dynamically update a dropdown list with the value of another field

I am currently working on a dropdown list featuring all countries. Here is the code snippet: <select id="Country" onchange="country();"> <option id="3">Japan</option> <option id="4">Canada</option> <option id="5"> ...

Monitoring changes to a SCSS file

I'm feeling completely baffled. I've put in my best effort to resolve this issue. The problem lies with my SCSS file and its watcher that doesn't appear to be functioning properly. A regular CSS file is not being generated at all. Here&apos ...

Is VS Code highlighting compatible with template string CSS in Emotion?

Is there a way to enable syntax highlighting for emotion's template string css in VS Code? I've been looking for plugins, but all of them seem to focus on snippets. I have tried using css({ camelCaseCssProps:...}), which works. However, I am att ...

passing a javascript variable to html

I am facing an issue with two files, filter.html and filter.js. The file filter.html contains a div with the id "test", while the file filter.js has a variable named "finishedHTML." My objective is to inject the content of "finishedHTML" into the test div ...

The dilemma of selecting objects in Three.js

Currently, I am developing a small web application that utilizes three.js. However, I have encountered an issue: I created a prototype with my three.js content and everything functions correctly (the canvas size in the prototype matches the browser window ...

Utilize JQuery UI dialog to display a confirmation dialog and return either true or false based

My plan is to submit a form only after validating it properly. To achieve this, I will first use an ajax post request to check for any duplicate values in the form. If duplicates are found, the ajax call will return HTML code; otherwise, it will return &ap ...

Is there a way to determine the bounding rectangle of a specific word within a paragraph when you only have its index?

I am currently developing a text-to-speech functionality for a react application that can emphasize the word being spoken by highlighting it with a background color. This feature closely resembles the layout of the Firefox reader view. However, my curren ...