开发者

Display Div relative to Href element

开发者 https://www.devze.com 2023-03-08 01:19 出处:网络
Need to position a div based on the href element position in the page. The code which I currentl have works on my machine which runs IE8.

Need to position a div based on the href element position in the page. The code which I currentl have works on my machine which runs IE8. The same code does not work on another machine with same configuration . Please help

BTW , the div is absolutely position and in the开发者_高级运维 javascript we have:

divobj.style.left = event.x - 185;
divobj.style.top = event.y - 5;


try to use this function.

function getElementPosition(element) {
        var x = 0; var y = 0;
        while(element !=null    )   {
            x += element.offsetLeft || 0;
            y += element.offsetTop  || 0;
            element = element.offsetParent;
        if (!element || element.style['position'] == 'relative' || element.tagName == 'BODY') break;
        }
        return {'x':x, 'y':y};
    }

it will retrun the position of your link element, but you have to pass that element as argument to this function.


The solution might be as simple as adding a unit of measurement, e.g.:

divobj.style.left = event.x - 185 + 'px'; divobj.style.top = event.y - 5 + 'px';

Some browsers assume 'px' as the unit of measurement; others don't make an assumption and therefore do nothing with what they consider an invalid value.

0

精彩评论

暂无评论...
验证码 换一张
取 消