Reputation: 301
I want a pure JavaScript solution
This is what I am doing. I hope you get the clear idea:
let isdown = false;
document.addEventListener("mouseup", () => {
iddown = false;
});
document.addEventListener("mousedown", () => {
isdown = true;
});
document.addEventListener("mouseover", (event) => {
if (isdown) {
console.log("I am here");
let middlehalf = window.innerHeight / 2;
console.log(middlehalf, event.offsetY);
if (e.offsetY > middlehalf) {
playerPos += 20;
} else {
playerPos -= 20;
}
}
});
summary:
Upvotes: 1
Views: 1301
Reputation: 7162
When you use offsetY, you get the coordinates relative to the parent, here the document. If you scroll down, then the coordinates will shift down as well. You can use clientY, which gives you the coordinates in the viewport, relative to the screen itself.
document.addEventListener("mouseover", (event) => {
if (isdown) {
let middlehalf = window.innerHeight / 2;
if (e.clientY > middlehalf) {
playerPos += 20;
} else {
playerPos -= 20;
}
}
});
Upvotes: 3