Skip to content
Browse files

rough pass at em unit support in media queries

  • Loading branch information...
1 parent 30fe962 commit 06fb248a521f670ceb11572d254ff7941ce481cf scottjehl committed Sep 23, 2011
Showing with 26 additions and 10 deletions.
  1. +26 −10 respond.src.js
View
36 respond.src.js
@@ -145,15 +145,14 @@ window.matchMedia = window.matchMedia || (function(doc, undefined){
eachq = fullq.split( "," );
eql = eachq.length;
-
for( ; j < eql; j++ ){
thisq = eachq[ j ];
mediastyles.push( {
media : thisq.match( /(only\s+)?([a-zA-Z]+)(\sand)?/ ) && RegExp.$2,
rules : rules.length - 1,
- minw : thisq.match( /\(min\-width:[\s]*([\s]*[0-9]+)px[\s]*\)/ ) && parseFloat( RegExp.$1 ),
- maxw : thisq.match( /\(max\-width:[\s]*([\s]*[0-9]+)px[\s]*\)/ ) && parseFloat( RegExp.$1 )
+ minw : thisq.match( /\(min\-width:[\s]*([\s]*[0-9]+)(px|em)[\s]*\)/ ) && parseFloat( RegExp.$1 ) + ( RegExp.$2 || "" ),
+ maxw : thisq.match( /\(max\-width:[\s]*([\s]*[0-9]+)(px|em)[\s]*\)/ ) && parseFloat( RegExp.$1 ) + ( RegExp.$2 || "" )
} );
}
}
@@ -172,8 +171,9 @@ window.matchMedia = window.matchMedia || (function(doc, undefined){
currWidth = doc.compatMode === "CSS1Compat" && docElemProp || doc.body[ name ] || docElemProp,
styleBlocks = {},
lastLink = links[ links.length-1 ],
- now = (new Date()).getTime();
-
+ now = (new Date()).getTime(),
+ eminpx = parseFloat( docElem.currentStyle ? body.currentStyle.fontSize : document.defaultView.getComputedStyle( body, null ).getPropertyValue( "font-size" ) );
+
//throttle resize calls
if( fromResize && lastCall && now - lastCall < resizeThrottle ){
clearTimeout( resizeDefer );
@@ -185,10 +185,26 @@ window.matchMedia = window.matchMedia || (function(doc, undefined){
}
for( var i in mediastyles ){
- var thisstyle = mediastyles[ i ];
- if( !thisstyle.minw && !thisstyle.maxw ||
- ( !thisstyle.minw || thisstyle.minw && currWidth >= thisstyle.minw ) &&
- (!thisstyle.maxw || thisstyle.maxw && currWidth <= thisstyle.maxw ) ){
+ var thisstyle = mediastyles[ i ],
+ min = thisstyle.minw,
+ max = thisstyle.maxw;
+
+ if( min ){
+ min = min.replace( "px", "" );
+ if( min.indexOf( "em" ) ){
+ min = parseFloat( min.replace( "em", "" ) ) * eminpx;
+ }
+ }
+ if( max ){
+ max = max.replace( "px", "" );
+ if( max.indexOf( "em" ) ){
+ max = parseFloat( max.replace( "em", "" ) ) * eminpx;
+ }
+ }
+
+ if( !min && !max ||
+ ( !min || min && currWidth >= min ) &&
+ ( !max || max && currWidth <= max ) ){
if( !styleBlocks[ thisstyle.media ] ){
styleBlocks[ thisstyle.media ] = [];
}
@@ -274,4 +290,4 @@ window.matchMedia = window.matchMedia || (function(doc, undefined){
else if( win.attachEvent ){
win.attachEvent( "onresize", callMedia );
}
-})(this);
+})(this);

0 comments on commit 06fb248

Please sign in to comment.
Something went wrong with that request. Please try again.