你如何获得浏览器的document.body相对于屏幕的坐标? [英] How do you get a browser's document.body coordinates with respect to the screen?

查看:239
本文介绍了你如何获得浏览器的document.body相对于屏幕的坐标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

必须有办法找到这个。
我需要弄清楚document.body相对于屏幕的坐标

There must be a way of finding this. I need to figure out the coordinates of document.body with respect to a screen

我需要弄清楚图像中问号的坐标。这将是文档正文相对于屏幕的位置。该图像给出了我可以使用的属性的描述。
有可能吗?

I need to figure out the coordinates for the question mark in the image. This will be the position of the document's body with respect to the screen. The image gives a description of the properties I could use. Is it possible?

推荐答案

WebDeveloper.com上的bobbel 建议通过比较内部和外部窗口宽度来获取边框的宽度,以及然后从内外高度差减去该值,得到可添加到screenX和screenY的值的窗口标题大小:

bobbel on WebDeveloper.com suggests getting the width of the border by comparing the inner and outer window width, and then subtracting that from the inner-outer height difference to get the size of the window header for values you can add to screenX and screenY:

// compute width of borders
var borderWidth = (window.outerWidth - window.innerWidth) / 2;

// compute absolute page position
var innerScreenX = window.screenX + borderWidth;
var innerScreenY = (window.outerHeight - window.innerHeight - borderWidth) + window.screenY;

当然,这不能保证是正确的 - 用户完全有可能在浏览器中设置一个条形码底部或侧面/底部宽度不同的窗口装饰 - 但对于我能想到的每个默认浏览器/窗口配置中的顶级浏览器窗口,它是一个准确,实用的解决方法。 (当然,这不适用于iframe中的网页。)

Granted, that's not guaranteed to be correct - it's entirely possible the user has a browser with a bar at the bottom, or window decorations that are different widths on the sides / bottom - but it's an accurate, practical workaround for the top-level browser window in every default browser/windowing configuration I can think of. (Of course, this won't work for pages in an iframe.)

正确解决方法是在whatwg邮件列表上提出一个规范,使innerScreenX和innerScreenY成为标准,如关于Chromium问题151983的讨论关于为Chrome实现mozInnerScreenX / Y的类似值。但似乎没有人花时间这么做(虽然有这个疯狂的提议,用于实现异步API函数来计算它。)

The proper solution for this would be to propose a spec on the whatwg mailing list to make innerScreenX and innerScreenY a standard, as described in the discussion on Chromium issue 151983 regarding implementing an analogous value to mozInnerScreenX/Y for Chrome. It would appear that nobody has taken the time to do so, though (although there's this crazy proposal to implement an asynchronous API function to "calculate" it).

这篇关于你如何获得浏览器的document.body相对于屏幕的坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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