A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

How to find mouse position relative to the document using jQuery
Q:

How to find mouse position relative to the document using jQuery

0

How to find mouse position relative to the document using jQuery

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Use the jQuery event.pageX and event.pageY

The jQuery event.pageX can be used to find the mouse position relative to the left edge of the document, whereas the event.pageY can be used to find the mouse position relative to the top edge of the document. Let's check out an example to understand how it works:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Find Coordinates of Mouse Pointer</title>
<style>
    *{
        margin: 0;
    }
    html, body{
        height:100%;
    }
</style>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
$(document).ready(function() {
    $("body").mousemove(function(event){
        var relPageCoords = "(" + event.pageX + "," + event.pageY + ")";
        $(".mouse-cords").text(relPageCoords);
    });
});
</script>
</head>
<body>
    <p>Coordinates of mouse pointer with respect to the page are: <strong class="mouse-cords"></strong></p>
</body>
</html>

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now