单击热图和鼠标坐标! - JavaScript

发布于 2024-09-11 13:22:38 字数 601 浏览 3 评论 0原文

我正在尝试为我的一个网站构建一个简单的热图,但它似乎比我想象的要棘手得多!

1) 该网站有不同的主题,1 个是左对齐,另一个是中心对齐。

2) 屏幕尺寸会随着用户的不同而变化。

我需要跟踪网站上的点击,但不幸的是 event.PageX 和 event.PageY 是在考虑整个屏幕的情况下计算的。


示例

在第一个示例中,坐标为 [300, 500] 的单击可能位于大猩猩周围的某个位置(可能是他的鼻孔! =) )。

alt text

在另一个示例中,单击坐标为 [300. 500] 可能会位于主要内容区域之外的某个位置!

alt text


底线: 我该如何解决这个问题,以便我可以构建准确的 DIY 点击热图?

知道这一点真的很有趣!谢谢你们! =)

I'm trying to build a simple heatmap for one of my sites, but it seems a lot trickier that I thought!

1) There are different themes for the site, 1 is aligned to the left, another one is aligned to the centre.

2) Screen sizes change throughout the users.

I need to track clicks on the site, but unfortunately event.PageX and event.PageY are calculated taking in consideration the whole screen.


Examples

In the first example a click with coordinates [300, 500] will probably be located somewhere around the gorilla (maybe his nostrils! =) ).

alt text

In this other example a click with coordinates [300. 500] will probably be located somewhere outside the main content area!

alt text


Bottomline:
How can I address this problem, so that I can build an accurate DIY click heatmap?

This would be really interesting to know! Thanks guys! =)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

他是夢罘是命 2024-09-18 13:22:38

1)只需传递当前主题和坐标即可。

2) 获取主要内容区域的鼠标相对位置。这样您就可以避免不同浏览器宽度/高度的不同位置的问题。

关于鼠标位置的 jQuery 文档 中有一个特殊部分(如果您要使用它):这:

(请注意,给出的像素值是
相对于整个文档。如果
你想计算位置
在特定元素内,或在
视口,你可以看看offsetY
和 offsetX,并做一些
算术找出相对值。

下面是一个查找的示例
特定元素内的位置
而不是页面:

$("#special").click(function(e){
    var x = e.pageX - this.offsetLeft;
    var y = e.pageY - this.offsetTop;
    $('#status2').html(x +', '+ y);
});

话虽这么说,也许有点矫枉过正; Google Analytics 也具有热图功能。

1) Just pass the current theme along with the coordinates.

2) Get the relative mouse position of your main content area. This way you avoid the problem of different positions for different browser widths/heights.

There is a special part in the jQuery docs for Mouse Position (if you were to use it) about this:

(Note that the pixel values give are
relative to the entire document. If
you want to calculate the position
within a particular element, or within
the viewport, you can look at offsetY
and offsetX, and do a bit of
arithmetic to find the relative value.

Here is an example of finding the
position within the particular element
rather than the page:

$("#special").click(function(e){
    var x = e.pageX - this.offsetLeft;
    var y = e.pageY - this.offsetTop;
    $('#status2').html(x +', '+ y);
});

That being said, perhaps overkill; Google Analytics has a heatmap feature too.

千紇 2024-09-18 13:22:38

您可以根据底层对象(即大猩猩)存储每次点击的相对坐标,而不是存储每次点击的绝对坐标。

然后,在显示热图时,您会以您的分辨率显示相对于每个对象的点击次数。

为此,您只需获取每次点击的“目标”对象(这是事件参数的“目标”属性)并从点击坐标中减去它的坐标。

Rather than storing the absolute co-ords for each click you could store the reltive co-ord for each click based on the underlying object, i.e the gorilla.

Then when displaying the heatmap you display clicks relative to each object at your resolution.

To do this you would simply have to grab the 'target' object of each click (this is the 'target' property of the event arguments) and subtract it's co-ords from the click co-ords.

野の 2024-09-18 13:22:38

这是我的代码。它仅记录页面包装内的点击(而不是背景内容)。所以你只能得到相对位置。

$(function(){
  var o = $("#wrapperDiv"); // page wrapper div
  var lf = o.offset().left; // wrapper left position
  var lt = lf+o.width(); // wrapper right position
  $(document).click(function(e){ // bind click event to whole page
    var x = e.pageX; // mouse x position
    if(x >= lf || x <= lt){ // was the click inside wrapper div?
      $.get("heatmap.php", { // send ajax request to server width:
        x: x-lf, // x position of page
        y: e.pageY, // y position of page
        url: document.location.href.replace('http://'+document.domain, '') // request uri
      });
    }
  });
});

Here is my code. It logs clicks only inside page wrapper (not background n stuff). So you get relative positions only.

$(function(){
  var o = $("#wrapperDiv"); // page wrapper div
  var lf = o.offset().left; // wrapper left position
  var lt = lf+o.width(); // wrapper right position
  $(document).click(function(e){ // bind click event to whole page
    var x = e.pageX; // mouse x position
    if(x >= lf || x <= lt){ // was the click inside wrapper div?
      $.get("heatmap.php", { // send ajax request to server width:
        x: x-lf, // x position of page
        y: e.pageY, // y position of page
        url: document.location.href.replace('http://'+document.domain, '') // request uri
      });
    }
  });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文