Results 1 to 8 of 8

Thread: Find X,Y position of an element on the page as a whole?

  1. #1
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default Find X,Y position of an element on the page as a whole?

    Is it possible to find the X,Y coordinates for the position of an object on the page as a whole, if they are positioned locally within a sub-region such as a <div>?

    I'm planning to work out some things for my PHP+HTML graphs, and although in theory I could do all of the math to add up the distances between the various divs and so forth, it would be a lot easier if there's some easy way to get the global X,Y coordinates for the objects instead.

    If so... it would be through Javascript, right? Or is there some way to figure this out in just HTML/CSS?
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  2. #2
    Join Date
    Nov 2006
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    1,920
    Thanks
    2
    Thanked 267 Times in 262 Posts

    Default

    Hi there djr33,

    here is a javascript example...
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
       "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <head>
    
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="language" content="english"> 
    <meta http-equiv="Content-Style-Type" content="text/css">
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    
    <title>where are you?</title>
    
    <style type="text/css">
    body {
        margin:0;
        background-color:#f0f0f0;
     }
    #mydiv{
        width:160px;
        padding:40px;
        margin:50px auto;
        background-color:#000;
     }
    #myimg {
        display:block;
        width:100px;
        height:100px;
        border:1px solid #fff;
        margin:auto;
     }
    #myp{
        width:162px;
        height:100px;
        margin:50px 0 0 20px;
        background-color:#00f;
     }
    </style>
    
    <script type="text/javascript">
    
    function init() {
    
       obj=document.body.getElementsByTagName('*');
    
    for(c=0;c<obj.length;c++) {
       alert(
       '\n\n****************************************'+
       '\n\nThe element type is '+obj[c].tagName.toLowerCase()+
       '.\n\nThe element id is '+obj[c].id+
       '.\n\nIt\'s position is..................'+
       '\n\ntop="'+obj[c].offsetTop+'px",  '+'left="'+obj[c].offsetLeft+'px".'+
       '\n\n****************************************'
       );
      }
     }
       window.addEventListener?
       window.addEventListener('load',init,false):
       window.attachEvent('onload',init);
    
    </script>
    
    </head>
    <body>
    
    <div id="mydiv">
     <img id="myimg" src="http://www.coothead.co.uk/images/anim2.gif" alt="">
    </div>
    
    <p id="myp"></p>
    
    </body>
    </html>
    
    coothead

  3. The Following User Says Thank You to coothead For This Useful Post:

    djr33 (10-11-2012)

  4. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Thanks! So offsetTop and offsetLeft are always relative to the page as a whole? That'll make things a lot easier for me if I end up doing it this way. I wasn't planning to use Javascript, but there's no reason I can't do that.

    Note: I'm using relatively positioned objects within embedded divs on the page. In case anyone knows of any problems that could create, I'd like to know. But it looks like the solution above would work.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #4
    Join Date
    Nov 2006
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    1,920
    Thanks
    2
    Thanked 267 Times in 262 Posts

    Default ~ super huma ~

    Hi there djr33,

    stone me mate , you're super human.

    It only took you two minutes to read my post, try my example and type a two paragraph reply.

    It takes me that long just to find out if I'm still alive.

    coothead

  6. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    That's weird. I didn't even notice the time-- I just came on the forum to check what was going on and happened up on that post, right when you posted it I think.


    Also, I hadn't tried the code yet-- I trusted you on it. But I have tried it now, played with a bit, and it seems to work really well. (So maybe I'm not quite as superhuman as you think.)
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  7. #6
    Join Date
    Nov 2006
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    1,920
    Thanks
    2
    Thanked 267 Times in 262 Posts

    Default

    Don't say that.

    We all need super heroes.

    coothead

  8. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    The offsetLeft and offsetTop are not always the offsets on the page in all browsers. Often they are. Using jQuery you can do:

    Code:
    var o = $('#selectorid').offset();
    o will then be an object with top and left properties that are the offsets on the page:

    Code:
    alert('top = ' + o.top + ' left = ' + o.left);
    But using that I've found that finding the position of things inside tables can be buggy though. It seems fine for everything else, and I've always found a way with tables. I think finding the td's offset.

    If using ordinary javascript, Quirks Mode has a fairly good way:

    http://www.quirksmode.org/js/findpos.html

    that compensates for some browsers not always giving the offset relative to the page.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  9. The Following User Says Thank You to jscheuer1 For This Useful Post:

    djr33 (10-11-2012)

  10. #8
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Ok, thanks John. I'll work on implementing this soon, once I've fixed a couple other bugs in my project. Nearly done...
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Similar Threads

  1. Resolved dynamic DOM jQuery find element
    By ggalan in forum JavaScript
    Replies: 10
    Last Post: 11-27-2011, 09:19 PM
  2. find X Y positions of an element
    By ericc in forum JavaScript
    Replies: 4
    Last Post: 12-15-2010, 05:38 PM
  3. How to find out which element has focus?
    By Tohaki in forum JavaScript
    Replies: 2
    Last Post: 10-30-2008, 02:00 PM
  4. Replies: 3
    Last Post: 05-22-2007, 04:25 AM
  5. Change the Position of a Page Element script issue
    By Soren Twilight in forum JavaScript
    Replies: 1
    Last Post: 06-07-2006, 01:34 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •