是否可以在浏览器视口中获取div的位置?不在文档中。在窗口内 [英] Is it possible to get the position of div within the browser viewport? Not within the document. Within the window

查看:131
本文介绍了是否可以在浏览器视口中获取div的位置?不在文档中。在窗口内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这在IE7或Firefox中是否可以执行?

Is this doable in either IE7 or Firefox?

推荐答案

文档,然后减去滚动位置。

You can do it in both - get the position relative to the document, then subtract the scroll position.

var e = document.getElementById('xxx');
var offset = {x:0,y:0};
while (e)
{
    offset.x += e.offsetLeft;
    offset.y += e.offsetTop;
    e = e.offsetParent;
}

if (document.documentElement && (document.documentElement.scrollTop || document.documentElement.scrollLeft))
{
    offset.x -= document.documentElement.scrollLeft;
    offset.y -= document.documentElement.scrollTop;
}
else if (document.body && (document.body.scrollTop || document.body.scrollLeft))
{
    offset.x -= document.body.scrollLeft;
    offset.y -= document.body.scrollTop;
}
else if (window.pageXOffset || window.pageYOffset)
{
    offset.x -= window.pageXOffset;
    offset.y -= window.pageYOffset;
}

alert(offset.x + '\n' + offset.y);

这篇关于是否可以在浏览器视口中获取div的位置?不在文档中。在窗口内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆