Screen coordinates of object

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AliR
    New Member
    • Oct 2006
    • 1

    Screen coordinates of object

    Hi everybody!

    I need a script, which calculates screen coordinates of given object (e.g. textbox or div). By "screen coordinates" I mean offset from upper left corner of the screen (not frame or browser window).

    Thanks in advance.
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Use the screenX/screenY properties.

    Comment

    • hsriat
      Recognized Expert Top Contributor
      • Jan 2008
      • 1653

      #3
      You won't be able to get that if the HTML is rendered itself. But if you have set the position, and left and top of the elements by CSS (or javascript), only then you would be able to retrieve the x and y coordinates as [code=javascript]
      var x = parseInt(elemen tObj.style.left );
      var y = parseInt(elemen tObj.style.top) ;[/code]

      Comment

      • hsriat
        Recognized Expert Top Contributor
        • Jan 2008
        • 1653

        #4
        Originally posted by acoder
        Use the screenX/screenY properties.
        And that was a really old post! ...:D

        Comment

        • Markus
          Recognized Expert Expert
          • Jun 2007
          • 6092

          #5
          Originally posted by hsriat
          And that was a really old post! ...:D
          Must've been bored.

          Like me

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Originally posted by hsriat
            You won't be able to get that if the HTML is rendered itself. But if you have set the position, and left and top of the elements by CSS (or javascript), only then you would be able to retrieve the x and y coordinates as [code=javascript]
            var x = parseInt(elemen tObj.style.left );
            var y = parseInt(elemen tObj.style.top) ;[/code]
            That would give you the x/y coordinates within the browser, not the screen

            ...and yes, it's an old post, but no-one answered it (poor thing!)

            Comment

            • hsriat
              Recognized Expert Top Contributor
              • Jan 2008
              • 1653

              #7
              Originally posted by markusn00b
              Must've been bored.

              Like me
              But you don't reply in your own threads... :-/

              Comment

              • hsriat
                Recognized Expert Top Contributor
                • Jan 2008
                • 1653

                #8
                Originally posted by acoder
                That would give you the x/y coordinates within the browser, not the screen
                oh.. I see... good point..
                So one can still calculate the browser's offset wrt the screen by doing
                [CODE=javascript]var offsetX = event.screenX - event.pageX; //(or clientX)
                var offsetY = event.screenY - event.pageY; //(or clientY)[/CODE]
                And then add that in x and y calculated above...
                what say? :p

                Originally posted by acoder
                ...and yes, it's an old post, but no-one answered it (poor thing!)
                I hadn't joined by the time. :p

                Comment

                • acoder
                  Recognized Expert MVP
                  • Nov 2006
                  • 16032

                  #9
                  Originally posted by hsriat
                  oh.. I see... good point..
                  So one can still calculate the browser's offset wrt the screen by doing
                  [CODE=javascript]var offsetX = event.screenX - event.pageX; //(or clientX)
                  var offsetY = event.screenY - event.pageY; //(or clientY)[/CODE]
                  And then add that in x and y calculated above...
                  what say? :p
                  screenX and screenY should be enough, see link.
                  Originally posted by hsriat
                  I hadn't joined by the time. :p
                  Neither had I ;)

                  Comment

                  • hsriat
                    Recognized Expert Top Contributor
                    • Jan 2008
                    • 1653

                    #10
                    Originally posted by acoder
                    screenX and screenY should be enough, see link.
                    But still, how would you tell what are the coordinates of a <div> element (for instance) wrt the screen? I'm not getting what you are trying to say.

                    This is what I understand from screenX and pageX:

                    Comment

                    • acoder
                      Recognized Expert MVP
                      • Nov 2006
                      • 16032

                      #11
                      Originally posted by hsriat
                      But still, how would you tell what are the coordinates of a <div> element (for instance) wrt the screen? I'm not getting what you are trying to say.
                      I see now what you mean. Yes, screenX/Y would give the screen co-ordinates, pageX/Y the co-ordinates relative to the document. However, it's not supported in IE, so you'll have to use clientX/Y plus scrolling offset in IE to get the same value.

                      Comment

                      • hsriat
                        Recognized Expert Top Contributor
                        • Jan 2008
                        • 1653

                        #12
                        Originally posted by acoder
                        I see now what you mean. Yes, screenX/Y would give the screen co-ordinates, pageX/Y the co-ordinates relative to the document. However, it's not supported in IE, so you'll have to use clientX/Y plus scrolling offset in IE to get the same value.

                        I know its not an IE thing. But while having a logical conversation, we should keep IE out. :D
                        Of course it would need some fix as usual. :)

                        Comment

                        Working...