Flex - 如何获得包含HTML偏移的绝对X和Y坐标?

时间:2010-02-18 05:25:06

标签: javascript html flex coordinates

我使用Flex通过ExternalInterface调用JS函数,需要绝对的X和Y坐标来创建弹出菜单。 Flex应用程序显示在HTML页面的中心,因此需要考虑HTML X和Y偏移量。

我尝试过使用LocalToGlobal和ContentToGlobal函数,但这些只是给我相对于Flex应用程序的X和Y坐标,它没有考虑将Flex应用程序放在中心的HTML X和Y偏移量。页面或不同的屏幕分辨率。

使用JavaScript检索HTML X和Y偏移量的最佳方法是什么?是否有我可以使用的Flex功能,它提供基于HTML页面的绝对X和Y坐标?

谢谢!

1 个答案:

答案 0 :(得分:1)

如果我理解正确,听起来像是:

  • 您在HTML页面的中心有一个小型Flex应用程序
  • 在某些事件中,您想要创建一个HTML弹出窗口(新的浏览器弹出窗口)。
  • 该弹出窗口应该在HTML页面中居中。

如果这是正确的,您不需要使用localToGlobal或globalToLocal;你只是在寻找浏览器视口边界。这是我目前用于放置与浏览器边界相关的项目的方法(所有这些都是javascript):

function getBrowserBounds()
{
        var size = [0, 0]; 
        if (typeof window.innerWidth != "undefined") { 
            size = [window.innerWidth, window.innerHeight];
        } 
        else if (typeof document.documentElement != "undefined" && typeof document.documentElement.clientWidth != "undefined" && document.documentElement.clientWidth != 0) {
            size = [document.documentElement.clientWidth, document.documentElement.clientHeight]; 
        }
        else {
            size = [document.getElementsByTagName("body")[0].clientWidth, document.getElementsByTagName("body")[0].clientHeight]; 
        }
        var bounds = null;
        if (navigator.userAgent.indexOf("MSIE") != -1) // Internet Explorer
            bounds = [window.screenLeft, window.screenTop, size[0], size[1]];
        else
            bounds = [window.screenX, window.screenY, size[0], size[1]];
        var width = bounds[0] + (bounds[2]/2);
        var height = bounds[1] + (bounds[3]/2);
        return bounds;
}

返回浏览器视口的边界。从那里,您可以创建一个以浏览器为中心的弹出窗口,无论浏览器位于笔记本电脑/桌面屏幕范围内,使用此方法:

function centerPopup(windowHeight, windowWidth, windowName, windowUri)
{
    var bounds = getBrowserBounds();
    var centerWidth = bounds[0] + ((bounds[2] - windowWidth) / 2);
    var centerHeight = bounds[1] + ((bounds[3] - windowHeight) / 2);

    newWindow = window.open(windowUri, windowName, 'resizable=0,width=' + windowWidth + 
        ',height=' + windowHeight + 
        ',left=' + centerWidth + 
        ',top=' + centerHeight);

    newWindow.focus();
    return newWindow.name;
}

如果有效,请告诉我。

最佳, 兰斯