Detect mobile device rotation with jQuery

房东的猫 提交于 2019-12-12 01:04:37

问题


I am searching for a jQuery-plugin or Javascript-library that detects the rotation of a device in degrees, not just orientation, using the G-sensor. I know it can be done - check http://wagerfield.github.io/parallax/ - but I haven't managed to find something like this yet.

It would be great if it created a kind of global that updates realtime.

Thanks!


回答1:


you can use jquery mobile lib orientationchange event.

$(window).on("orientationchange",function(event){
    var x = event.beta,  // -180 to 180
        y = event.gamma, // -90 to 90
        z = event.alpha; // 0 to 360
});

without jQuery:

window.addEventListener('orientationchange', function(event){
    var x = event.beta,  // -180 to 180
        y = event.gamma, // -90 to 90
        z = event.alpha; // 0 to 360
});

also try this..

if (window.DeviceOrientationEvent) {
    window.addEventListener('deviceorientation', function(event){
    });
}


来源:https://stackoverflow.com/questions/21060833/detect-mobile-device-rotation-with-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!