Window: deviceorientation event

The deviceorientation event is fired when fresh data is available from an orientation sensor about the current orientation of the device as compared to the Earth coordinate frame. This data is gathered from a magnetometer inside the device.

See Orientation and motion data explained for details.

Bubbles No
Cancelable No
Interface DeviceOrientationEvent
Event handler property window.ondeviceorientation


if (window.DeviceOrientationEvent) {
    window.addEventListener("deviceorientation", function(event) {
        // alpha: rotation around z-axis
        var rotateDegrees = event.alpha;
        // gamma: left to right
        var leftToRight = event.gamma;
        // beta: front back motion
        var frontToBack = event.beta;

        handleOrientationEvent(frontToBack, leftToRight, rotateDegrees);
    }, true);

var handleOrientationEvent = function(frontToBack, leftToRight, rotateDegrees) {
    // do something amazing


DeviceOrientation Event Specification (DeviceOrientation Event)
# deviceorientation

Browser compatibility

BCD tables only load in the browser

See also