Detect mouse direction - JavaScript
Asked Answered
F

5

14
var direction = ""
var mousemovemethod = function (e) {
    var oldx = 0;
    if (e.movementX < oldx) {
        direction = "left"
    } else if (e.movementX > oldx) {
        direction = "right"
    }
    oldx = e.pageX;
}

This is how I detect the mouse direction and it works so good but it works only on Chrome, how I can make this compatible with other browsers (Firefox, Opera and at least ie8+ or ie9+). No jQuery please.

Faction answered 18/6, 2014 at 20:26 Comment(0)
H
18

Stick with pageX and define oldx in a higher scope, otherwise it's always zero

var direction = "",
    oldx = 0,
    mousemovemethod = function (e) {

        if (e.pageX < oldx) {
            direction = "left"
        } else if (e.pageX > oldx) {
            direction = "right"
        }

        oldx = e.pageX;

}

FIDDLE

Haemoglobin answered 18/6, 2014 at 20:39 Comment(1)
So very thank you for fixing oldx problem I was wondering why it didn't work with pageX...Faction
J
13

on mouse move - to left, to right, to up, to down - javascript FIDDLE

var direction = "";
    var oldx = 0;
    var oldy = 0;
    mousemovemethod = function (e) {
    
 if (e.pageX > oldx && e.pageY == oldy) {
                direction="East";
            }
            else if (e.pageX == oldx && e.pageY > oldy) {
                direction="South";
            }
            else if (e.pageX == oldx && e.pageY < oldy) {
                direction="North";
            }
            else if (e.pageX < oldx && e.pageY == oldy) {
                direction="West";
            }
        
        document.body.innerHTML = direction;
        
        oldx = e.pageX;
         oldy = e.pageY;
        
}

document.addEventListener('mousemove', mousemovemethod);
Johnstone answered 17/7, 2019 at 5:34 Comment(0)
R
3
let direction = "";
if (e.movementY > 0 && e.movementX == 0) {
    direction = "down";
  } else if (e.movementY < 0 && e.movementX == 0) {
    direction = "up";
  } else if (e.movementX > 0 && e.movementY == 0) {
    direction = "right";
  } else if (e.movementX < 0 && e.movementY == 0) {
    direction = "left";
  }
Rento answered 2/8, 2021 at 19:36 Comment(1)
I just needed it in the X direction and it seems that e.movementX > 0 means moving towards right and < 0 means moving towards left, exactly what I needed, I think this must be the selected answer as in 2021 it's fully supported on all major browsers, thanks.Hoffmann
M
2

This code will give you the 'direction' of 'X' and 'Y' in directionX and direction Y variable and the distance traveled by 'X' and 'Y' axis.

 let oldX = 0, oldY = 0;

 function captureMouseMove($event){
      let directionX = 0, directionY = 0, diffX = 0, diffY = 0;
      if ($event.pageX < oldX) {
          directionX = "left"
          diffX = oldX - $event.pageX;
      } else if ($event.pageX > oldX) {
          directionX = "right"
          diffX = $event.pageX - oldX;
      }

      if ($event.pageY < oldY) {
          directionY = "top"
          diffY = oldY - $event.pageY;
      } else if ($event.pageY > oldY) {
          directionY = "bottom";
          diffY = $event.pageY - oldY;
      }

      oldX = $event.pageX;
      oldY = $event.pageY;
  }

  window.addEventListener('mousemove', captureMouseMove);
Mingrelian answered 10/2, 2018 at 7:42 Comment(0)
E
2

You don't need to keep track of old position.

   e.clientX - window.innerWidth/2 > 0 ? 'right' : 'left'
   e.clientY - window.innerHeight/2 > 0 ? 'bottom' : 'top'
Else answered 10/11, 2020 at 8:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.