4

I am building a over lay mechanism for Reactjs and using Jquery in Reactjs is like smashing your thumb with a hammer. You don't do it.

So I have these two function in jquery which are super helpful

window.jQuery(DOMNode).offset();
window.jQuery(elem).position();

I have looked every where for the javascript equivalents. I have even looked at their implementation details but alas I do not want to our right copy that because well that would be the same as just using the functions them selves.

Does any one know of any functions that would get me the same or similar return values of these functions?

T.Todua
  • 53,146
  • 19
  • 236
  • 237
SeekingTruth
  • 1,044
  • 2
  • 15
  • 23
  • possible duplicate of [Determine distance from the top of a div to top of window with javascript](http://stackoverflow.com/questions/9880472/determine-distance-from-the-top-of-a-div-to-top-of-window-with-javascript) – T.Todua Sep 23 '15 at 11:24

3 Answers3

6

You can use something like this (it does not check existence of the element)

function getOffset(element, target) {
    var element = document.getElementById(element),
        target  = target ? document.getElementById(target) : window;
    var offset = {top: element.offsetTop, left: element.offsetLeft},
        parent = element.offsetParent;
    while (parent != null && parent != target) {
       offset.left += parent.offsetLeft;
       offset.top  += parent.offsetTop;
       parent = parent.offsetParent;
    }
    return offset;
}

var tmp = getOffset('element', 'parent');
alert('Offset to parent top = ' + tmp.top + ' and left = ' + tmp.left);
var tmp = getOffset('element');
alert('Offset to window top = ' + tmp.top + ' and left = ' + tmp.left);
#parent {
    position: relative;
    top: 50px;
    left: 50px;
    border: 1px solid red;
    width: 50%;
}

#element {
    position: relative;
    top: 25px;
    left: -25px;
    border: 1px solid blue;
    width: 50%;
}
<div id='parent'>
    <div id='element'>
        Test
    </div>
</div>

Results for offset with respect to the window are different from 75 and 25 because browser has default margin for the <body> which can be set to 0 by body {margin: 0px;}

Cheery
  • 16,063
  • 42
  • 57
0

I use this functions to get coordinates of an element when I'm not implementing jquery:

function find_PosX(obj){
    var curleft=0;
    if(obj.offsetParent)
        while(1){
            curleft+=obj.offsetLeft;
            if(!obj.offsetParent)
                break;
                obj=obj.offsetParent;
        }
    else if(obj.x)curleft+=obj.x;
    return curleft;
}

function find_PosY(obj){
    var curtop=0;
    if(obj.offsetParent)
        while(1){
            curtop+=obj.offsetTop;
            if(!obj.offsetParent)
                break;
                obj=obj.offsetParent;
        }
    else if(obj.y)curtop+=obj.y;
    return curtop;
}
0

I use

elOffsetTop = el.offsetTop - el.scrollTop + el.parentNode.offsetTop - el.parentNode.scrollTop;
elOffsetLeft = el.offsetLeft - el.scrollLeft + el.parentNode.offsetLeft - el.parentNode.scrollLeft;

I just came into this and this was best for me.

thednp
  • 4,401
  • 4
  • 33
  • 45