<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Get Mouse Position</title>
<style>
#mousePosition {
position: absolute;
top: 100px;
left: 200px;
width: 50%;
height: 50%;
background: #ff0;
}
</style>
</head>
<body>
<div id="mousePosition" >Mouse Position: (0, 0)</div>
<script>
var mousePositionDiv = document.getElementById('mousePosition');
mousePositionDiv.addEventListener('mousemove', function(event) {
let msg = '';
msg += '所在区块位置: (' + event.offsetX + "," + event.offsetY + ')<br/>';
msg += '可视区域的坐标(滚动条之外的部分忽略掉): (' + event.clientX + ',' + event.clientY + ')<br/>';
msg += '所在浏览器位置(包括上面浏览器工具栏标签页): (' + event.screenX + ',' + event.screenY + ')\r\n';
mousePositionDiv.innerHTML = msg;
});
</script>
</body>
</html>