获取鼠标相对于浏览器窗口坐标

<!DOCTYPE html>
<html>
<head>
    <style>
        div {
            position: fixed;
            top: 10px;
            left: 10px;
            padding: 10px;
            background-color: #f1f1f1;
            border: 1px solid #ccc;
        }
    </style>
</head>
<body>
    <div></div>
    <script>
        const d = document.querySelector('div')
        document.addEventListener('mousemove', function (e) {
            let x = e.clientX
            let y = e.clientY
            d.innerHTML = `X: ${x}, Y: ${y}`

        })
    </script>
</body>

</html>

image

posted @ 2023-08-16 17:23  弩哥++  阅读(18)  评论(0编辑  收藏  举报