English 中文(简体)
What is the proper way to get bounding box for HTML elements relative to the Window?
原标题:

I m writing a Firefox extension. I m trying to limit it to just XUL+Javascript (no XPCOM). When I get a mouseover event for an HTML element, I need to get its bounding box in the windows coordinate system (that is the built-in XUL document browser.xul).

The obvious place to start is to put something like this in the mouseover event handler:

var rect = e.target.getBoundingClientRect();

Which is great, but that gives me the rect in the HTML document s coordinate system, which is relative to the upper left corner of the HTML drawing area. I want to display a xul:panel element using panel.openPopup() near this image (but not using one of the predefined popup positions), so I need to translate the coordinates.

I ve tried doing the following (in the XUL dom) to get the offset s to do the translation, and it works for some sites, but not all, and doesn t seem to take into account the x translation needed for sidebars.

var appcontent = document.getElementById("appcontent");
if (appcontent) {
  chromeOffsetX = r.left;
  chromeOffsetY = r.top;
}

So, what s the best way to approach this?

Note: for IE extensions I would use (and have used) IDisplayServices::TransformRect()—is there something similar for Firefox?

Now with bounty!

最佳回答

Turns out getting the location is irrelevant because you can position items relative to the element using something like:

hoverPanel.openPopup(someElement, "overlap", offsetX, offsetY, false, false);
问题回答

暂无回答




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签