js 中获取 某一个控件位置的函数及用法

1、函数:

    function getposition(obj) {
        var r = new Array();
        r['x'] = obj.offsetLeft;
        r['y'] = obj.offsetTop;
        while (obj = obj.offsetParent) {
            r['x'] += obj.offsetLeft;
            r['y'] += obj.offsetTop;
        }
        return r;
    }

 

2、 用法:

 var p = getposition(document.getElementById('hintd'));

document.getElementById('hintdiv' + showtype).style.left = p['x'] + objleftoffset + "px";

document.getElementById('hintdiv' + showtype).style.top = p['y'] + objtopoffset + "px";

你可能感兴趣的:(function)