N
Nikolai Prokoschenko
Hello,
this question is connected to AmCharts (http://amcharts.com/javascript/) but it's not *about* AmCharts, it's just the context of the problem I'm having. (NB: AmCharts' API and general code provide enough material for several pages of flaming )
AmCharts, like almost every charting solution, has a concept of a "balloon", those are small popups which follow the mouse cursor and show some more or less relevant information about the chart item under this cursor. They use some browser-detecting code to find mouse's position for these balloons, which failed for me on several occasions, so I monkey-patched that code by the code provided by Peter-Paul Koch at http://www.quirksmode.org/js/events_properties.html. For the record, here is his code:
function doSomething(e) {
var posx = 0;
var posy = 0;
if (!e) var e = window.event;
if (e.pageX || e.pageY) {
posx = e.pageX;
posy = e.pageY;
}
else if (e.clientX || e.clientY) {
posx = e.clientX + document.body.scrollLeft
+ document.documentElement.scrollLeft;
posy = e.clientY + document.body.scrollTop
+ document.documentElement.scrollTop;
}
// posx and posy contain the mouse position relative to the document
// Do something with this information
}
This works splendidly in most cases, but as you can see, it's relative to the document, which means it won't work properly for fixed elements (e.g., lightboxed charts) when the document is scrolled. It turns out, AmCharts' original code also fails on fixed elements. By removing the scrolling position from the calculations, I'd probably get correct values for fixed elements, but instead the values would be broken for elements inside the document flow. Unfortunately, in my case, it has to work for both types of elements
My idea was to get the element under cursor, go all the way up in the DOM tree to check whether there are any elements with fixed positioning and calculate the cursor position relative to the scroll position of that element instead of documentElement's. However, I'm not sure whether this is correct or even doable.
So the actual question is: how do I find the correct mouse position for both fixed and relatively positioned elements? Note: I don't need complete code, some hints and directions would suffice for a start.
Thanks.
Nikolai.
this question is connected to AmCharts (http://amcharts.com/javascript/) but it's not *about* AmCharts, it's just the context of the problem I'm having. (NB: AmCharts' API and general code provide enough material for several pages of flaming )
AmCharts, like almost every charting solution, has a concept of a "balloon", those are small popups which follow the mouse cursor and show some more or less relevant information about the chart item under this cursor. They use some browser-detecting code to find mouse's position for these balloons, which failed for me on several occasions, so I monkey-patched that code by the code provided by Peter-Paul Koch at http://www.quirksmode.org/js/events_properties.html. For the record, here is his code:
function doSomething(e) {
var posx = 0;
var posy = 0;
if (!e) var e = window.event;
if (e.pageX || e.pageY) {
posx = e.pageX;
posy = e.pageY;
}
else if (e.clientX || e.clientY) {
posx = e.clientX + document.body.scrollLeft
+ document.documentElement.scrollLeft;
posy = e.clientY + document.body.scrollTop
+ document.documentElement.scrollTop;
}
// posx and posy contain the mouse position relative to the document
// Do something with this information
}
This works splendidly in most cases, but as you can see, it's relative to the document, which means it won't work properly for fixed elements (e.g., lightboxed charts) when the document is scrolled. It turns out, AmCharts' original code also fails on fixed elements. By removing the scrolling position from the calculations, I'd probably get correct values for fixed elements, but instead the values would be broken for elements inside the document flow. Unfortunately, in my case, it has to work for both types of elements
My idea was to get the element under cursor, go all the way up in the DOM tree to check whether there are any elements with fixed positioning and calculate the cursor position relative to the scroll position of that element instead of documentElement's. However, I'm not sure whether this is correct or even doable.
So the actual question is: how do I find the correct mouse position for both fixed and relatively positioned elements? Note: I don't need complete code, some hints and directions would suffice for a start.
Thanks.
Nikolai.