Is there a native JavaScript (or through the use of a library such as JQuery/Modernizr etc) way to detect if a device is capable of changing orientation? I would like to use that as a method to differentiate between desktop and mobile.
Thanks,
Shadi
Is there a native JavaScript (or through the use of a library such as JQuery/Modernizr etc) way to detect if a device is capable of changing orientation? I would like to use that as a method to differentiate between desktop and mobile.
Thanks,
Shadi
检测移动设备:
简单的浏览器嗅探
if (/mobile/i.test(navigator.userAgent)) {...}
jQuery.browser.mobile插件(详尽的浏览器嗅探)
触摸事件的简单测试
if ('ontouchstart' in window) {...}
触摸事件的高级测试:
if (('ontouchstart' in window) || // Advanced test for touch events
(window.DocumentTouch && document instanceof DocumentTouch) ||
((hash['touch'] && hash['touch'].offsetTop) === 9)) {...}
可选择onorientationchange用于上面的#3 和#4。
根据需要组合其中的一种或多种(以及任何其他方法)。它们都不是万无一失的。