我正在尝试使用此代码来检测鼠标方向是上升还是下降:
<html>
<head></head>
<body>
<div style="width: 500px; height: 500px; background: red;"></div>
</body>
</html>
var mY = 0;
$('body').mousemove(function(e) {
mY = e.pageY;
if (e.pageY < mY) {
console.log('From Bottom');
return;
} else {
console.log('From Top');
}
});
但是,这个代码不起作用,我期待.控制台日志始终显示“从顶部”
任何想法 ?
解决方法:
var mY = 0;
$('body').mousemove(function(e) {
// moving upward
if (e.pageY < mY) {
console.log('From Bottom');
// moving downward
} else {
console.log('From Top');
}
// set new mY after doing test above
mY = e.pageY;
});