Created
April 24, 2014 17:08
-
-
Save mojaray2k/11262042 to your computer and use it in GitHub Desktop.
Get Mouse Coordinates in JavaScript. You can get the user's mouse coordinates in three distinct ways in JavaScript: Relative to the browser window, relative to the page, and relative to the entire screen. Let's see how we can do this in each case.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
body { | |
font-family: Arial, sans-serif; | |
text-align: center; | |
padding-top: 20px; | |
color: #555; | |
height: 3000px; | |
} | |
h1 { | |
font-size: 1.5em; | |
} | |
output { | |
font-size: 45px; | |
color: darkblue; | |
position: fixed; | |
bottom: 20px; | |
left: 20px; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<h1>Click Anywhere to View Your Mouse Coordinates Relative to the Browser or iFrame Window. Test by scrolling down, too.</h1> | |
<output id="op">[coordinates will display here]</output> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var op = document.getElementById('op'); | |
function showCoords(e){ | |
op.innerHTML = 'clientX value: ' + | |
e.clientX + '<br>' + | |
'clientY value: ' + | |
e.clientY; | |
} | |
document.onclick = function (e) { | |
showCoords(e); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment