JavaScript를 사용하여 마우스 좌표를 얻으려면 코드는 다음과 같습니다 -
예시
<!DOCTYPE html>
<html>
<head>
<style>
body {
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
}
.sample {
font-size: 25px;
font-weight: 500;
}
</style>
</head>
<body>
<h1>Coordinates of mouse</h1>
<p class="sample"></p>
<h3>Hover the mouse over the screen to get its x and y axis position</h3>
<script>
let sampleEle = document.querySelector(".sample");
document.body.addEventListener("mousemove", (event) => {
sampleEle.innerHTML = "X axis: " + event.x + " Y axis: " + event.y;
});
</script>
</body>
</html> 출력
위의 코드는 다음과 같은 출력을 생성합니다:
