admin管理员组

文章数量:1428513

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?

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?

Share Improve this question edited Sep 23, 2015 at 11:28 T.Todua 56.7k22 gold badges260 silver badges264 bronze badges asked Nov 4, 2014 at 23:46 SeekingTruthSeekingTruth 1,0543 gold badges15 silver badges23 bronze badges 1
  • possible duplicate of Determine distance from the top of a div to top of window with javascript – T.Todua Commented Sep 23, 2015 at 11:24
Add a ment  | 

3 Answers 3

Reset to default 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;}

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;
}

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.

本文标签: reactjsPlain Javascript replication to offset and positionStack Overflow