2010-02-18 40 views
0

我正在通過使用Flex的ExternalInterface調用JS函數,它需要絕對的X和Y座標來創建彈出式菜單。 Flex應用程序顯示在HTML頁面的中心,因此需要考慮HTML X和Y偏移量。Flex - 如何獲得絕對的X和Y座標,包括HTML偏移量?

我已經嘗試過使用LocalToGlobal和ContentToGlobal函數,但這些只是給了我相對於Flex應用程序的X和Y座標,它並沒有考慮Flex中應用程序的HTML X和Y偏移量該頁面或改變不同的屏幕分辨率。

是使用JavaScript檢索HTML X和Y偏移量的最佳方法嗎?是否有我可以使用的Flex函數,它提供基於HTML頁面的絕對X和Y座標?

謝謝!

+1

是彈出菜單將在Flex或HTML?如果它是flex,並且flex已經在html的中心,爲什麼你需要swf的x/y值?你不能只使用localtoglobal函數? – Amarghosh 2010-02-18 06:58:08

回答

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; 
} 

讓我知道是否可行。

最佳, 蘭斯

0

@viatropos 我們可以得到它,而無需使用window.open()完成。因爲我在瀏覽器頂部有一個高度爲50的應用程序,並且想要在瀏覽器中間彈出一個彈出窗口。我想彈出一個窗口,所以我沒有要傳遞的URL