简体   繁体   English

在JS / Jquery中将热图上的鼠标单击坐标映射为不同的分辨率和屏幕尺寸

[英]Mapping mouse click coordinates on heatmap for different resolution & screen sizes in JS/Jquery

I would like to collect mouse click data from a webpage which is accessed on different resolutions & screen size devices and accurately map those click coordinates on an overlay for displaying heatmap. 我想从可在不同分辨率和屏幕尺寸的设备上访问的网页上收集鼠标单击数据,并将这些单击坐标准确地映射到覆盖图上以显示热图。 How can I do it in JavaScript/jQuery? 如何在JavaScript / jQuery中做到这一点? Also, considering the case when the page being displayed can be scrolled up/down and browser window resized anytime by the user thus changing the relative positions of the DOM elements at the screen. 另外,考虑这样一种情况,即用户可以随时上下滚动正在显示的页面,并调整浏览器窗口的大小,从而改变DOM元素在屏幕上的相对位置。 For example, I have a main div wrapper as container of the documents and two divs as columns inside. 例如,我有一个主要的div包装器作为文档的容器,两个divs作为内部的列。 Normally, the columns will be shown side by side to each other inside the wrapper div. 通常,这些列将在包装器div中彼此并排显示。 But when the user resizes the page to a smaller size, the column to the right will move and sit under the 1st column. 但是,当用户将页面尺寸调整为较小尺寸时,右侧的列将移动并位于第一列的下方。 So, its X and Y offset has changed according to the page. 因此,其X和Y偏移量已根据页面进行了更改。 Now, if a user clicks on this 2nd column, the clicks won't map accurately to screen when I see the heatmap on my device in full page viewI'm a beginner, so I would need to know the details. 现在,如果用户单击第二列,那么当我以全页视图查看设备上的热图时,这些点击将无法准确映射到屏幕上。我是一个初学者,所以我需要知道详细信息。

I am working on same thing to collect click coordinates to be plotted on heatmap. 我正在同一件事上收集要绘制在热图上的点击坐标。 The method I used is to get the click coordinate and converting them into pixel average relative to the document and then when plotting again convert them into coordinates. 我使用的方法是获取点击坐标并将其转换为相对于文档的像素平均值,然后在再次绘制时将其转换为坐标。

window.addEventListener("click",function(){

    var posx = 0;
    var posy = 0;
    TotalX = document.body.scrollWidth; 
    TotalY = document.body.scrollHeight;
    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;
    }
    console.log("X" + ((posx * 100)/TotalX)); //Just For Debugging
    console.log("Y" + ((posy * 100)/TotalY)); // For Debugging
    return {
        x : ((posx * 100)/TotalX),
        y : ((posy * 100)/TotalY)
    };

});

The coordinates are not found relative to document would work pretty well in desktop clicks but Not with mobile clicks. 找不到相对于文档的坐标在桌面点击中效果很好,但在移动点击中效果不佳。 You can put a condition to avoid mobile clicks or can record them differently. 您可以设置条件来避免移动点击,也可以不同地记录它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM