Just starting to learn jquery and I'm struggling with this code, can someone help?

I am trying to move the .icon element to the right and animate it based on its position().left value.

Unfortunately, my code is not working as expected and I can't figure out why. It seems like such a simple task!

<script>
$("li.menu-item").hover(function(){
    var pos = $(this).position().left;
    $(".icon").animate({left : pos+'px'}, 1500 );
});
</script>

</head>
<body>

<div id="menu-wrapper">
    <ul id="main-nav">
        <li class="menu-item"><a href="">Blog</a></li>
        <li class="menu-item"><a href="">Sponsored</a></li>
        <li class="menu-item"><a href="">Facebook</a></li>
        <li class="menu-item"><a href="">Twitter</a></li>
        <li class="menu-item"><a href="">About</a></li>
        <li class="menu-item"><a href="">Contact</a></li>
    </ul;

    <div class="icon">move please</div>
</div><!-- //END menu-wrapper -->
</body>

CSS:

#menu-wrapper { position: fixed; bottom: 50px; margin: 0 auto; text-align:center;}

.icon {
    width: 9px;
    height: 9px;
    background: url(../images/plus-menu.png) no-repeat 0 0;
    position: absolute;
    left: -5px;
    bottom: -4px;
}

#main-nav { line-height: 1.0; float: left; margin-bottom: 1em; }

#main-nav{list-style: none;}

#main-nav li a {
    display: block;
    position: relative;
    text-decoration: none;
    margin-right: 20px;
    color: #b2b2b2;
    line-height: 19px;
    padding-bottom: 17px;
}

#main-nav li { float: left; position: relative; }

#main-nav li a:hover, #main-nav li a:active { color: #404040; }

Any assistance would be greatly appreciated.

Thank you

Answer №1

Enclose your code within $(document).ready()

<script>
$(document).ready(function()
{
    $("li.menu-item").hover(function(){
        var position = $(this).position().left;
        $(".icon").animate({left : position+'px'}, 1500 );

    });
});
</script>

Take a look at this entry-level tutorial for further information

Answer №2

Interesting. By making slight adjustments (adding a stop() function to pause the animation each time a new hover event occurs, preventing it from getting chained), the solution appears to be functioning properly in this demonstration.

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

I possess a single input field and I am seeking to have the focus shifted to it upon the activation of a button

Looking to enhance user input with an icon interaction: Clicking the icon should focus on the input Considering a solution for when clicking the icon to trigger focusout A code snippet has been implemented for the first requirement, seeking suggestions ...

Ajax request (with Spring) causing HTTP error 415Charsets deems invalid

My server doesn't receive a response when I send an Ajax request. PUT request: function modifyData() { var item={ id:idNum.replace('edit',''), data:newData }; console.log(item); ...

Deactivating checkboxes once the maximum selection has been made through jquery

I need assistance with my jQuery code that is supposed to disable checkboxes in a multidimensional array after reaching the maximum number of selections. However, I am having trouble identifying the issue within the code. Any guidance would be greatly ap ...

Experiencing issues with connecting to jQuery in an external JavaScript file

My jQuery formatting in the HTML file looks like this: <!doctype html> <html lang="en"> <head> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js" type="text/javascript"></script> </head> < ...

I'm wondering if there is a method for me to get only the count of input fields that have the class "Calctime" and are not empty when this function is executed

Currently, I am developing an airport application that aims to track the time it takes to travel between different airports. In this context, moving from one airport to another is referred to as a Sector, and the duration of time taken for this journey is ...

Unable to submit multiple forms simultaneously on the same HTML page

I have been encountering an issue with forms in a particular file. The problem arises when I click submit, rather than submitting the data from the filled form, it submits the data from the first form. Despite specifying unique ids for each form as shown i ...

CSS: Caption text below image remains on same line

I am struggling with positioning a div that contains an image and a caption. My goal is to make the caption break into a new line when it reaches 95% of the width of the image. Despite my efforts, I can't seem to achieve this. The text always pushes ...

Is there a way to customize the background color of the active list item class in Bootstrap 4?

Customizing Boostrap Code : <!-- Custom Sidebar --> <div class="bg-light border-right" id="sidebar-wrapper"> <div class="sidebar-heading"><strong><?= __('Admin Panel') ?></strong></div> <div class="li ...

Utilizing jQuery and Ajax with ASP.NET, create a sleek MVC modal login system

I need help troubleshooting my code. In my layout, when I click on the SignIn button, a modal form appears for inserting data. While in debug mode it shows that the data is being read, the next action doesn't execute. Any suggestions would be apprecia ...

Implementing PHP echo alerts using Javascript

My current task involves validating the IP address entered in a textbox using PHP. $('#check_ip').click(function() { var iptext = $('#Ip_Txt').val(); $.ajax({ type : "POST", url : "mypage.php", data : { iptext : ip ...

Utilize jQuery to implement a dynamic search feature that functions similar to cPanel's search bar, allowing

I am on the hunt for a feature akin to cPanel search. Imagine typing in the search box and having similar content remain while the rest disappears. For instance: <div>hello</div> <div>world</div> <div>good</div> <div ...

Transform the styles from a React component into Cascading Style Sheets

I have been tasked with transitioning all the styles from the JavaScript file to the CSS file while ensuring the design remains intact. I am facing an issue where using the CSS styles breaks the search field design. The original design can be seen here: S ...

Unable to show the input's value

Need help in taking user input to display calculated values //html <div class="empty"> <h5> Enter Empty Seats </h5> <ion-item> <ion-input placeholder="Enter Number of Empties.." type="number" name="emptySeats" [( ...

"Exploring the Functionality of Dropdown Menus in ASP.NET Core 1

Looking for a unique way to create a dropdown menu in ASP.Net Core 1.0? I've scoured the internet but haven't found a solution specifically tailored to this new platform. Can anyone provide guidance on how to build a large dropdown menu in Core 1 ...

Library for React and validation

Summary: React offers controlled components and Higher Order Components (HOC) as the foundation for creating validation libraries in NPM. However, integrating with existing components like React Select and customizing error message placement can be a chall ...

Tips on removing the red border from a text box within an HTML form with the help of AngularJS

My form has the following appearance: https://i.stack.imgur.com/T3NQW.png Upon clicking the submit button, the textbox fields display a red border due to the required attribute. Afterwards, I aim to click the reset button in order to remove the red bord ...

Is it possible to generate a "pop-up" window upon clicking on the register button?

As a backend programmer, I'm looking to create a popup window that appears in front of the current window when users click "register", eliminating the need for redirection to another page. I believe you understand the concept. How can I achieve this? ...

Locate the child element that has a particular class assigned to it

I need help writing a code to search through all children elements in order to find a div with a specific class. Unfortunately, the DIV I am looking for does not have an ID. Below is the sample HTML that I will be working with: <div class="outerBUB ...

Using conditional statements like 'if' and 'else' in JavaScript can

Can someone help me with solving my problem using if-else statements in Javascript? I need to filter names by gender and save them as keys - woman / man in local storage. Any assistance would be greatly appreciated. I am struggling to figure out how to im ...

Using jQuery AJAX to Transfer Data to a PHP Script

There is a div element with a data-id attribute. The goal is to use jQuery to detect when this div is clicked and then send or store the corresponding data-id value in a PHP file. However, the code provided raises an error when trying to obtain the data-id ...