Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/449.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用JS获取文档的绝对坐标?_Javascript_Jquery_Html - Fatal编程技术网

Javascript 如何使用JS获取文档的绝对坐标?

Javascript 如何使用JS获取文档的绝对坐标?,javascript,jquery,html,Javascript,Jquery,Html,如何获得html/正文的绝对屏幕坐标? 我找到了所有的window.screenTop、screenLeft、outerheight/innerHeight——但这不是解决方案 编辑:我更新了图像以澄清我需要什么 试试: 及 请注意,screenY不考虑浏览器顶部的地址栏、书签栏等 编辑: 您可以使用(window.outerHeight-window.innerHeight)获取附加偏移: window.screenY + window.outerHeight - window.innerHe

如何获得html/正文的绝对屏幕坐标? 我找到了所有的window.screenTop、screenLeft、outerheight/innerHeight——但这不是解决方案

编辑:我更新了图像以澄清我需要什么

试试:

请注意,screenY不考虑浏览器顶部的地址栏、书签栏等

编辑:

您可以使用(window.outerHeight-window.innerHeight)获取附加偏移:

window.screenY + window.outerHeight - window.innerHeight
但这假设您在浏览器底部没有任何工具栏(如果您将devtools停靠在打开状态,工具栏将断开)

只需获取body元素:

var $body = $(this.ie6 ? document.body : document);
并使用以下属性:$body.width(),$body.height()

还可以获取窗口尺寸

var $window = $(window);
var wWidth  = $window.width();
var wHeight = $window.height();
已更新 尝试使用以下方法:

您可以这样做(做一些假设):


您可以使用JQuery
scrollLeft
scrollTop
函数。到目前为止,这可能是重复的感谢,但不幸的是,这没有帮助。我做了一个新的形象,让我更清楚我需要什么!DOM在浏览器的上下文中工作。我很确定你不能从JavaScript中获得桌面上的屏幕偏移量。如果我错了,请纠正我!这也是我的印象,我想如果可能的话,我会找到它的——当然最好问一下,返回0/0也没有帮助,因为不清楚尸体在窗户里的什么地方。我可以得到浏览器的左上角位置和内部高度/宽度,但我需要偏移!尝试使用以下内容:元素。主体上/左上方的getBoundingClientRect()始终为0/0漂亮!我可以接受的解决办法!;)谢谢,非常感谢!回答得好!读者注意:
外部
值可能是实像素,而
内部
值可能是“CCS像素”。
var $body = $(this.ie6 ? document.body : document);
var $window = $(window);
var wWidth  = $window.width();
var wHeight = $window.height();
var rect = element.getBoundingClientRect();
console.log(rect.top, rect.right, rect.bottom, rect.left);
//this is the border width of the OS window.
var border= (window.outerWidth-window.innerWidth)/2; 

//Assuming the window border is homogeneous and there is nothing in
// the bottom of the window (firebug or something like that)
var menuHeight=(window.outerHeight-window.innerHeight)-border*2;

var absX=window.screenX + border;
var absY=window.screenY+border+menuHeight;