Calculating mouse position on Web page instead of from top left of window in jQuery
Just for fun I am trying to make user mouse-position click counter (heatmap sort of thing) and I have run into an issue. My only problem is that I am stuck when it comes to browsers being resized. Since I am trying to keep it general I am assuming no knowledge about the document structure and I am not sure how to calculate the mouse position relative to where it is on the actual page. Since all websites are going float differently on the page I am somewhat stuck.
(Sorry if this doesn't make much sense).
Another option is to record which element is clicked on and also record where it is in the DOM by backtracking and recording offsets. This seems like a poor choice also though because the DOM might change but the View could stay the same. This would negate using offsets from any elements (except maybe body).
I have tried using something like:
jQuery(document).bind({
'click' : function(event){
var X = $('body').offset().left;
var Y = $('body'开发者_JAVA百科).offset().top;
mouseX = event.pageX - X;
mouseY = event.pageY - Y;
...
but that doesn't help because you don't know how wide the margins might get between the background any the content.
Any thoughts or help on this would be greatly appreciated. I am sure this is very common to all of you jQuery and javascript experts out there.
$(document).scrollTop() plus the pageY should give you the vertical position of the cursor relative to the document top.
精彩评论