Can I change the media query using javascript / jQuery?

前端 未结 7 973
逝去的感伤
逝去的感伤 2020-12-10 15:50

I have a style.css with a media query. In my javascript-file, I have a value for the desired mobile-width stored in a variable.

By changing the variable, I want to c

7条回答
  •  醉酒成梦
    2020-12-10 16:08

    You can use Enquire.js jQuery plugin.

    It is a JavaScript library for dealing with media queries in JavaScript.

    This is quick start code sample:

    enquire.register("screen and (max-width:45em)", {
    
        // OPTIONAL
        // If supplied, triggered when a media query matches.
        match : function() {},      
    
        // OPTIONAL
        // If supplied, triggered when the media query transitions 
        // *from a matched state to an unmatched state*.
        unmatch : function() {},    
    
        // OPTIONAL
        // If supplied, triggered once, when the handler is registered.
        setup : function() {},    
    
        // OPTIONAL, defaults to false
        // If set to true, defers execution of the setup function 
        // until the first time the media query is matched
        deferSetup : true,
    
        // OPTIONAL
        // If supplied, triggered when handler is unregistered. 
        // Place cleanup code here
        destroy : function() {}
    
    });
    

提交回复
热议问题