Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7803529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:36:44+00:00 2026-06-02T01:36:44+00:00

I am using the following jQuery code to find the position of the active

  • 0

I am using the following jQuery code to find the position of the active link on my top menu navigation:

$(document).ready(){
    // Handles the triangle image sprite for the top navigation
$('#topnav a')
    .on({ 

        mouseenter: function() {

            var pos = $("#topnav a.active-top").offset();
            console.log("Top: " + pos.top + " Left: " + pos.left );

            // Get the position of the current hovered "a" element
            var upSprite = $("#upArrow").show(),
                pos = $(this).offset(),
                aWidth = $(this).width(),
                offsetTop = 27, // Adjust this to raise or lower the sprite
                offsetLeft = aWidth / 2; // Centers the element under the link


            upSprite
                .css({
                    "top": pos.top + offsetTop,
                    "left": pos.left + offsetLeft
                });

            //console.log("Top: " + pos.top + " Left: " + pos.left);

        },

        mouseleave: function() {
            // Hide the arrow once the mouse leaves
            $('#upArrow').hide();
        }

    });
}

Now when I paste the exact same code outside of this event handler

 $(document).ready(function () {        
     var pos = $("#topnav a.active-top").offset();
     console.log("Top: " + pos.top + " Left: " + pos.left );
}

I get a complete different value for my pos.left.

As I understand .offset() should give me the position relative the document and unlike .position() which gives me the position relative to the parent container.

Is the code being fed a different type of context when it’s in the scope of the .on() event handler? I have tried using $.proxy() to no avail. Any tips are appreciated. Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T01:36:46+00:00Added an answer on June 2, 2026 at 1:36 am

    $(document).ready() fires once the DOM has loaded; but before all images have loaded. You’ll find the offset is being changed after you’re retrieving the offset() by the repainting of the elements due to this.

    To fix this you can handle the $(window).load() event instead, or continue to retrieve the position inside the click handler (which is what I’d recommend).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following Javascript code (using jQuery): floatUpAndDown(); function floatUpAndDown() { $(#bird).animate({top: '+=30px'},
i have the following jquery code: $(document).ready(function() { $('.restrictiveOptions input[type!=checkbox], .restrictiveOptions select').change(function() { //
I'm parsing a XML using jQuery with the following code: function appendNav(xml) { $(xml).find(Nav).each(function()
I have the following block of jQuery code that I'm using to copy some
I have the following code using jQuery Validate. $(#register).validate({ debug: true, errorClass:'error', validClass:'success', errorElement:'span',
I am using the following code to show a jQuery UI dialog when the
I am using the following code to submit different values var evalue= 'color:#d32;font-size:12px;'; jQuery.ajax({
How to get previous page URL using jQuery? I am using the following code
I have just started using jQuery and although following code gets the job done,
I'm using jQuery with rails and have the following piece of code $('#related').html(<%= render

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.