Y-Position of Element on Web Page

Discussion in 'ASP .Net' started by Lit, Aug 26, 2007.

  1. Lit

    Lit Guest

    Hi,

    How can I get the Y-Position ( Vertical ) of an Control via JavaScript from
    the top of the Window.

    I have tried the following script but no luck I keep on getting 15

    function getY( oElement )
    {
    var iReturnValue = 0;
    while( oElement != null ) {
    iReturnValue += oElement.offsetTop;
    oElement = oElement.offsetParent;
    }
    return iReturnValue;
    }



    I don't understand why, I keep on getting 15 for an element in the middle of
    the page that is 900 pixels down on the page.

    The loop is going up the parents many times but the total answer is 15. I
    have tried similar script that I found on the Internet but getting the same
    result. is this due to some elements have no ID?

    Thank you,

    Lit
     
    Lit, Aug 26, 2007
    #1
    1. Advertising

  2. Following returns y position as integer in pixels relative to nearest
    absolute position container (which usually is client area of browser)

    function findPosY(obj)
    {
    var curtop = 0;
    if (obj.offsetParent)
    {
    while (obj.offsetParent)
    {
    curtop += obj.offsetTop
    obj = obj.offsetParent;
    }
    }
    else if (obj.y)
    curtop += obj.y;
    return curtop;
    }
     
    Muhammad Naveed Yaseen, Aug 26, 2007
    #2
    1. Advertising

  3. You may have some parent in hierarchy which has 'position' style set
    to 'fixed' or 'absolute' which would mess up OffsetTop aggregation.
     
    Muhammad Naveed Yaseen, Aug 26, 2007
    #3
  4. Lit

    Lit Guest

    Muhammad,

    To my Surprise I had the script function at the bottom of the page and the
    function only works from an Event, ( for testing I just dropped the
    JavaScript loose) this could be due to render-completion issues, also the
    Border size of elements and Font Size when Images and text are under the
    same element can mess things up.

    the reason I needed the y-position is because I was trying to find a
    solution to SetFocusOnError for Validation to set the focus on the element
    with the error -70 pixels because it was too close to the top and does not
    look good.

    Thank you for any further input in this matter,

    Lit


    "Muhammad Naveed Yaseen" <> wrote in message
    news:...
    > You may have some parent in hierarchy which has 'position' style set
    > to 'fixed' or 'absolute' which would mess up OffsetTop aggregation.
    >
     
    Lit, Aug 26, 2007
    #4
  5. If it is just an offset scroll relative to current position you may
    try window.scrollBy(0, -70)
     
    Muhammad Naveed Yaseen, Aug 26, 2007
    #5
  6. Lit

    Lit Guest

    I am doing that, but where to hook into the end of a validation
    "SetFocusOnError" in ASP.NET 2.0 is unknown. I am currently doing all my
    validation using Custom Validation to deal with this.

    I wish Microsoft can allow us too hook into After Validation event. Or
    better yet After the SetFocusOnError I can append and run a script of my
    own.

    Thank you,

    Lit


    "Muhammad Naveed Yaseen" <> wrote in message
    news:...
    > If it is just an offset scroll relative to current position you may
    > try window.scrollBy(0, -70)
    >
    >
     
    Lit, Aug 26, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. HANM
    Replies:
    2
    Views:
    767
    Joseph Kesselman
    Jan 29, 2008
  2. Konrad Hammerer
    Replies:
    14
    Views:
    631
    Jonathan N. Little
    Feb 7, 2008
  3. Microsoft Newsserver

    page element - absolute position

    Microsoft Newsserver, Apr 18, 2008, in forum: ASP .Net
    Replies:
    0
    Views:
    345
    Microsoft Newsserver
    Apr 18, 2008
  4. James Wong
    Replies:
    4
    Views:
    536
    James Wong
    Jul 14, 2004
  5. steven acer

    position of an element relative to the page

    steven acer, Oct 29, 2007, in forum: Javascript
    Replies:
    4
    Views:
    188
    steven acer
    Oct 30, 2007
Loading...

Share This Page