Detect if device is iOS

前端 未结 17 1596
一整个雨季
一整个雨季 2020-11-22 01:39

I\'m wondering if it\'s possible to detect whether a browser is running on iOS, similar to how you can feature detect with Modernizr (although this is obviously device detec

17条回答
  •  甜味超标
    2020-11-22 02:19

    Wherever possible when adding Modernizr tests you should add a test for a feature, rather than a device or operating system. There's nothing wrong with adding ten tests all testing for iPhone if that's what it takes. Some things just can't be feature detected.

        Modernizr.addTest('inpagevideo', function ()
        {
            return navigator.userAgent.match(/(iPhone|iPod)/g) ? false : true;
        });
    

    For instance on the iPhone (not the iPad) video cannot be played inline on a webpage, it opens up full screen. So I created a test 'no-inpage-video'

    You can then use this in css (Modernizr adds a class .no-inpagevideo to the tag if the test fails)

    .no-inpagevideo video.product-video 
    {
         display: none;
    }
    

    This will hide the video on iPhone (what I'm actually doing in this case is showing an alternative image with an onclick to play the video - I just don't want the default video player and play button to show).

提交回复
热议问题