Skip to content

Instantly share code, notes, and snippets.

@neodigm
Created May 9, 2017 17:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neodigm/8dfe83fb7ea6ebe1cf34686c40427e93 to your computer and use it in GitHub Desktop.
Save neodigm/8dfe83fb7ea6ebe1cf34686c40427e93 to your computer and use it in GitHub Desktop.
JavaScript Media Query or jsMQ is a function that can tell you what the current state of the browser is (Small, Medium or Large) and notify you if there is a change.
jsMQcallback = $.Callbacks(), jsMQcurrent = "";
var jsMQ = {
getVW : function(){
vw=jQuery(window).width();
if(vw <= 480) return "s";
if((vw >= 480) && (vw <= 748)) return "sl";
if((vw >= 749) && (vw <= 965)) return "m";
if(vw >= 966) return "l";
},
pubVW : function(){
vw = jsMQ.getVW();
if(jsMQcurrent != vw){
jsMQcurrent = vw;
jsMQcallback.fire(vw);
}
},
isSmall : function(){ return ( jsMQ.getVW().charAt(0)=="s" ) ? true : false; },
isMedium : function(){ return ( jsMQ.getVW()=="m" ) ? true : false; },
isLarge : function(){ return ( jsMQ.getVW()=="l" ) ? true : false; }
};
setInterval(jsMQ.pubVW, 1800);
@neodigm
Copy link
Author

neodigm commented May 9, 2017

What is unique about this solution is that you can leverage JavaScript to fire events when the size of the browser changes even after the page has loaded.

Requires JQuery.
See also: https://api.jquery.com/jQuery.Callbacks/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment