let lastX;
let lastY;
window.addEventListener('mousemove', function(e) {
var x = e.clientX;
var y = e.clientY;
if (x > lastX) {
console.log('Mouse moving right');
} else if (x < lastX) {
console.log('Mouse moving left');
}
if (y > lastY) {
console.log('Mouse moving down');
} else if (y < lastY) {
console.log('Mouse moving up');
}
lastX = x;
lastY = y;
});
js监听鼠标移动方向
最新推荐文章于 2024-11-09 00:53:03 发布
这段代码通过添加事件监听器到window对象,捕获鼠标移动时的clientX和clientY坐标。比较当前坐标与上一次坐标,判断鼠标是向右、左、上还是下移动,并在控制台打印相应的移动方向。

5111

被折叠的 条评论
为什么被折叠?



