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

A contact form overlaying a muted Google Maps view in an HTML website

Currently, I am in the process of developing a website that features a contact form. It would be great if I could incorporate Google Maps as a background, but with a greyed-out effect instead of a plain white page. After trying out a few solutions, I ende ...

Is it possible to supersede the pre-set validation of the html5 input types for url and email?

When using the input type url in html5, what are the default rules for a valid URL? If a custom pattern attribute is also specified along with the type, will it override the default validation rules? For example, in the line below, will the pattern attribu ...

Why is JSP compilation essential in achieving its goals?

At the moment, I am involved in modifying JSP and subsequently uploading it to the server for compilation. Upon compilation, a .class file is generated for the modified JSP. If I were to remove the original JSP from the server, would it still function pr ...

Contrasting the impact of declaring a function inside document.ready versus outside of it

Is there a distinction between declaring a function within document.ready and outside of it? How does the location of defining a function impact when it can be called? For example, are there any considerations or discrepancies to keep in mind when placin ...

Although JQuery is able to remove a row from the page, the record in the database is not

I'm having some trouble using jQuery and Ajax to delete a row on the page as well as a record in the database. The row on the page is successfully removed, but the PHP file isn't being called. I only have an echo statement in the PHP for error ch ...

Changing the value in a URL String using JavaScript

I am in possession of a String that contains a URL resembling the following: var url ="http://ispeakphone.com/checkout/cart/add/uenc/aHR0cDovL2lzcGVha3Bob25lLmNvbS9zYW1zdW5nL3NhbXN1bmctZ2FsYXh5LXMvZ2FsYXh5LXM5LXBsdXMuaHRtbA,,/product/619/form_key/foxmD7jg ...

focusing on a specialized format using the serialize() method

Following up on my previous question which was answered so promptly by Meder, there is now an additional query that has arisen while creating a reusable jQuery form submission without redirecting the user. Issue The jQuery serialize() function is applyin ...

Having trouble including a YouTube iframe code within the document ready function

I am having trouble getting the youtube iframe API code to work properly within my $(document).ready() function. When I try to add the code inside the function, the player does not load. However, when I move the code outside of the document.ready, the play ...

Tips on handling a nested HTML hyperlink in Selenium using Python with IE11

Can anyone assist me in resolving the issue of clicking on a hyperlink within an HTML page that contains nested HTML elements up to 4 levels deep? I have attempted various methods using XPATH, but they do not seem to work during runtime. I am utilizing Sel ...

Using jQuery to add a timed slide effect to an element

After the page loads, I want an element to slide left after 10 seconds without disappearing from the page completely. The goal is for it to move 200px to the left and then return to its original position when clicked. I'm unsure about how to set the ...

Achieving Text Wrapping Around Unconventional Shapes Using CSS

I'm currently facing a challenge in getting text to wrap around an irregular shape on a web page. Despite numerous attempts, I have yet to find a solution that meets my requirements. Attached is an image illustrating the layout I am aiming for. The ...

Tips for accessing the information received from an AJAX call

When making an AJAX post request for processed data from the database in the form of an array [value1, value2, value3,...,valueN], I aim to use it on a ChartJS object. Here is the AJAX Request: $(document).ready($.post('callMeForAJAX.jsp', func ...

When using Firefox to scale down a div with CSS `moz-transform`, the border details are unfortunately not retained

When viewing the following HTML in Firefox, you may notice that the right and bottom border is missing. However, Chrome and Internet Explorer display it correctly. Is this a bug in Firefox, or is there another method I can use to make it look consistent ...

How can I adjust the width of each <li> element in a JQuery Slider?

Please note: I have read several related posts on Stack Overflow and they did not address my specific issue. The Issue In my slider panel, there is an unordered list with varying numbers of <li> elements that have different widths. Each li contains ...

Tips for Validating a Strongly Typed View in MVC3 without Using a Model

I'm facing a roadblock in my project. The project consists of 3 layers: Data access using ado.net data model. A WCF service that retrieves data from the data access ado.net data model and sends a serialized class. An MVC web application that connec ...

Is it possible to import the identical file twice consecutively using html and typescript?

I encountered an issue with an input element in my HTML file. Here's what it looks like: <input type="file" (change)="receiveFile($event)" id="inputFileButton" hidden /> This input element is designed for users to import files. Wh ...

Glitch in Mean App: front-end feature malfunctioning during data storage in mongodb

I am encountering difficulties while working on a MEAN app. I attempted to establish a connection between the backend (Node.js, Express.js) and the frontend (Angular 6), but encountered some issues. The backend port is http://localhost:3000, and the fron ...

Changing color of navigation bar link upon refreshing the page using Bootstrap

I am new to the world of HTML/CSS/Bootstrap and recently made a change to the color of a link on the navbar. However, I am encountering an issue where the original blue color of the link briefly appears when I refresh the page. I would like to prevent this ...

Having trouble locating specific elements on Yahoo News with Selenium in Python?

I am currently facing some challenges with extracting comments from Yahoo News using Selenium. I have successfully clicked on the comments button to open it, but now I am struggling to locate the text of the comments section. from selenium import webdriver ...

What is the best way to include a subscript (small letter) beneath a word using html and css bootstrap 5?

Is it possible to add a small letter (sub) below a word in HTML and CSS? I have included a screenshot for reference. I want to display "Max 100, Min 1" just like shown in the image. Here is my code: <input type="text" class="textarea" ...