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

  • SEARCH
  • Home
  • 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 8332013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:35:44+00:00 2026-06-09T02:35:44+00:00

To get an elements offset I use: function(el) { if(el && el.parentNode) { var

  • 0

To get an elements offset I use:

function(el) {
    if(el && el.parentNode) {
        var x = 0;
        var y = 0;
        while(el) {
            x += el.offsetLeft;
            y += el.offsetTop;
            el = el.offsetParent;
        }
        return {x: x, y: y};
    }
    return null;
}

and to get its scroll offset I use:

function(el) {
    if(el && el.parentNode) {
        var x = 0;
        var y = 0;
        while(el) {
            if(el && el.parentNode) {
                x += el.scrollLeft;
                y += el.scrollTop;
                el = el.parentNode;
            } else {
                el = null;
            }

        }
        return {x: x, y: y};
    }
    return null;
}

Problem is I can’t figure out how to merge these two functions together so I can get an elements offset including any scrolling thats been done on the page. When setting an elements position I usually just have to reference both functions, but it seems like it makes sense to just get the total offset in one function. How can I achieve this? Is it event a good idea to do this all in one function, or are there specific use cases when it would be a good idea to get the offset without the scroll?

  • 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-09T02:35:45+00:00Added an answer on June 9, 2026 at 2:35 am

    You mean something like this?

    function f (el) {
      var offset = (function(el) {
        if(el && el.parentNode) {
          var x = 0;
          var y = 0;
    
          while(el) {
            x += el.offsetLeft;
            y += el.offsetTop;
            el = el.offsetParent;
          }
    
          return {x: x, y: y};
        }
    
        return null;
      })(el);
    
      var scroll = (function(el) {
        if(el && el.parentNode) {
          var x = 0;
          var y = 0;
    
          while(el) {
            if(el && el.parentNode) {
              x += el.scrollLeft;
              y += el.scrollTop;
              el = el.parentNode;
            } else {
              el = null;
            }
          }
    
          return {x: x, y: y};
        }
    
        return null;
      })(el);
    
      return { x: offset.x + scroll.x, y: offset.y + scroll.y }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

var arr1:Array = new Array(); arr1.push(item1); arr1.push(item2); arr1.push(item3); then arr1 and its elements get
I would like to know how to get the distance/offset/position of an element relative
how can i get elements uniquely from an array
I know how to get elements with a particular attribute : $("#para [attr_all]") But
How do you get elements in a header/footer of a odt doc? for example
I have 4 arraylist in jsp and I want to get elements of lists
I want to get the elements sorted by the number of their occurence. This
I have the string SUM([A2:A10],[B2:B10],[C2:C10],[D2:D10]) and I need to get the elements [A2:A10],[B2:B10],[C2:C10],[D2:D10] in
Does clojure have a powerful 'loop' like common lisp. for example: get two elements
How can I get all td elements with 'colspan' greater than 1 in jQuery?

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.