方法一: 使用事件对象的clientX和clientY属性获取光标坐标:
document.addEventListener('mousemove', function(event) {
var x = event.clientX;
var y = event.clientY;
console.log('光标位置:', x, y);
});
方法二: 使用getBoundingClientRect()方法获取目标元素的位置信息:
var targetElement = document.getElementById('target');
var rect = targetElement.getBoundingClientRect();
var x = rect.left;
var y = rect.top;
console.log('目标元素的位置:', x, y);