<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
div{
height: 2000px;
width: 2000px;
background-color: darkmagenta;
}
section{
height: 100px;
width: 100px;
margin: 100px auto;
background-color: #bfa;
}
</style>
</head>
<body>
<div>
<section></section>
</div>
<script>
var div=document.querySelector('div')
var sec=document.querySelector('section')
sec.addEventListener('click',function (e) {
console.log(e.offsetX)//返回的坐标是相对于事件源盒子的距离
console.log(e.offsetY)//返回的坐标是相对于事件源盒子的距离
console.log(e.screenX)//相对于屏幕的左上角
console.log(e.screenY)//相对于屏幕的左上角
console.log(e.pageX)// 鼠标在整个页面的位置
console.log(e.pageY)//鼠标在整个页面的位置
console.log(e.clientX)//鼠标相对于视口的位置
console.log(e.clientY)//鼠标相对于视口的位置
})
</script>
</body>
</html>
鼠标对象坐标offsetX和pageX,screenX和 cilentX 的区别
最新推荐文章于 2023-11-17 01:55:00 发布