Using Modernizr to test for tablet and mobile - Opinions wanted

♀尐吖头ヾ 提交于 2019-11-27 12:02:30

Modernizr can check for touch events

For testing whether the device is a tablet or phone or desktop, I would probably lean more toward using User Agents. Take a look at the scripts on DetectMobileBrowsers.com

Modernizr simply tests for whether or not a browser supports certain features. As far as I know, it doesn't appear to test browser_type == mobile and stuff along those lines...

Keep in mind that a lot of modern desktop PCs these days have touch screens, so even detecting touch support does not guarantee that it is a mobile device or tablet.

This question is a bit old but I couldn't find a better answer when I googled it.

Most desktops now are touch enabled, so detecting it is irrelevent.

The best way to detect them is by screen size.

With Modernizr you can use if (Modernizr.mq('only all and (max-width: 480px)')) { ...}

You can use the following extension for Modernizr: https://www.npmjs.org/package/mobile-detect

Or (without Modernizr) you could use Restive.JS to add a ".phone" or ".tablet"-class to your body: http://docs.restivejs.com

(Look for "formfactor" down the page.)

This is a follow up answer to an old question as I believe the "correct" answer has changed with time.

  • Many laptops are touch enabled today
  • Newer mobiles have higher resolutions than the bulk of computer monitors out there while still having a significantly smaller physical screen

The best way I have found to work around this is calculate the physical screen size by multiplying the DPI of the screen in combination with the screen size in pixels.

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