I found this solution in another topic. It works correctly when moving your mouse downwards.
However, if you move your mouse upwards with a little curve in it, the console will both log From Top and From Bottom.
$(document).ready(function() {
var mY = 0;
$(document).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;
});
});
code {
background: #ededed;
padding: 0 5px;
}
span {
color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
So instead of moving <span>straight up</span>, go from the <span>left bottom corner</span> to the <span>right top corner</span> with a small <span>curve</span>. Moving your mouse up or down. It will log both <code>From Top</code> and <code>From Bottom</code>
How can I accurately measure if the mouse is moving upwards or downwards?