I need an easy way to detect CSS3 media query support using jquery

后端 未结 3 580
青春惊慌失措
青春惊慌失措 2020-12-11 05:15

I need a quick and dirty way to detect media query support using jquery. I defined a function as follows:

function mediaQueriesEnabled () {
   var v = parseF         


        
相关标签:
3条回答
  • 2020-12-11 05:53

    EDIT: I'm no longer curating this answer as I feel that Niet's is much better (see chosen answer). It should still work, but may need some testing.

    Cracking open Modernizr (which can test for media queries) we see it uses the window.matchMedia (MDN page) function. We can check in plain old vanilla JS to see if this function exists:

    function mediaQueriesEnabled () {
        if(typeof window.matchMedia != "undefined" || typeof window.msMatchMedia != "undefined") {
            return true;
        } else {
            return false;
        }
    }
    

    Or more elegantly:

    function mediaQueriesEnabled () {
        return (typeof window.matchMedia != "undefined" || typeof window.msMatchMedia != "undefined");
    }
    

    JSFiddle

    I've tested in the following browsers that support media queries, all correctly returned true:

    • Safari Mobile
    • Chrome 26

    I've tested in the following browsers that do not support media queries, all correctly returned false:

    • IE 7
    0 讨论(0)
  • 2020-12-11 06:05

    You can create a stylesheet with a media query and see if it works.

    In your HTML:

    <style>@media all and (min-width:1px) {
        .mediatest {position:absolute}
    }</style>
    

    (Or you could dynamically create the stylesheet, but that's harder)

    Then in your script:

    var d = document.createElement('div');
    d.className = "mediatest";
    document.body.appendChild(d);
    if( window.getComputedStyle && window.getComputedStyle(d).position == "absolute") {
        // supports media queries!
    }
    document.body.removeChild(d);
    
    0 讨论(0)
  • 2020-12-11 06:18

    Here's some working code that is based on @Kolink's answer. So far this code works great... I'll post back to this thread if I have any edits.

    The Javascript:

    var $globals = {
        mediaQuerySupport: false
    }
    
    function initMediaQueries () {
        // tests for media query support
        $globals.mediaQuerySupport = false;
        var d = document.createElement('div');
        d.className = "mediatest"; // found in core.css
        document.body.appendChild(d);
        if( window.getComputedStyle && window.getComputedStyle(d).position == "absolute") {
            $mlGlobals.mediaQuerySupport = true;
            mlDebug ('browser has media query support');
        }
        document.body.removeChild(d);
    }
    
    function mediaQueriesEnabled () {
       return ($globals.mediaQuerySupport);
    }
    
    $(document).ready(function() {
        initMediaQueries();
        if (mediaQueriesEnabled()) {
            ... do something
        }
    });
    

    The CSS (I added this to my main stylesheet file):

    @media all and (min-width:1px) {
        .mediatest {position:absolute}
    }
    
    0 讨论(0)
提交回复
热议问题