admin管理员组文章数量:1202779
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
Share Improve this question asked Dec 10, 2012 at 15:23 Shadi AlmosriShadi Almosri 12k16 gold badges60 silver badges80 bronze badges 3 |1 Answer
Reset to default 19Detecting mobile devices:
Simple browser sniffing
if (/mobile/i.test(navigator.userAgent)) {...}
jQuery.browser.mobile plug-in (exhaustive browser sniffing)
Simple test for touch events
if ('ontouchstart' in window) {...}
Advanced test for touch events:
if (('ontouchstart' in window) || // Advanced test for touch events (window.DocumentTouch && document instanceof DocumentTouch) || ((hash['touch'] && hash['touch'].offsetTop) === 9)) {...}
Optionally use onorientationchange
for #3 and #4 above.
Combine 1 or more of these (and any other approaches) as needed. None of them are foolproof.
本文标签: jqueryDetecting if a device is able to change orientation in JavaScriptStack Overflow
版权声明:本文标题:jquery - Detecting if a device is able to change orientation in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738619491a2103108.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
onorientationchange
can be a property ofwindow
if that's available. My browser doesn't have it, but it seems like a valid event - stackoverflow.com/questions/5284878/… – Ian Commented Dec 10, 2012 at 15:37