allen
专注于java ee技术,包括struts,jsf,webwork,spring,hibernate,ibatis
posts - 7,  comments - 9,  trackbacks - 0
获取距窗口的y值
function getTop(e){
    var offset=e.offsetTop;
    if(e.offsetParent!=null) offset+=getTop(e.offsetParent);
    return offset;
}
获取距窗口的x值
function getLeft(e){
    var offset=e.offsetLeft;
    if(e.offsetParent!=null) offset+=getLeft(e.offsetParen)
    return offset;
}

除了上面的函数以外,还有一种快速方法,可以立刻获得网页元素的位置。

那就是使用getBoundingClientRect()方法。它返回一个对象,其中包含了left、right、top、bottom四个属性,分别对应了该元素的左上角和右下角相对于浏览器窗口(viewport)左上角的距离。

所以,网页元素的相对位置就是

 

 var X= this.getBoundingClientRect().left;

 
var Y =this.getBoundingClientRect().top;

 

再加上滚动距离,就可以得到绝对位置

 

 var X= this.getBoundingClientRect().left+document.documentElement.scrollLeft;

 
var Y =this.getBoundingClientRect().top+document.documentElement.scrollTop;
posted on 2011-11-28 14:43 robbin163 阅读(207) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航:
 

<2025年2月>
2627282930311
2345678
9101112131415
16171819202122
2324252627281
2345678

常用链接

留言簿(3)

随笔分类

随笔档案

文章分类

文章档案

搜索

  •  

最新评论

阅读排行榜

评论排行榜