简体   繁体   中英

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? 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. For example, I have a main div wrapper as container of the documents and two divs as columns inside. Normally, the columns will be shown side by side to each other inside the wrapper 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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