forked from stowball/mqGenie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mq.genie.min.js
14 lines (14 loc) · 2.3 KB
/
mq.genie.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*!
* mqGenie v0.5.0
*
* Adjusts CSS media queries in browsers that include the scrollbar's width in the viewport width so they fire at the intended size
*
* Returns the mqGenie object containing .adjusted, .width & fontSize for use in re-calculating media queries in JavaScript with mqAdjust(string)
*
* Copyright (c) 2014 Matt Stow
*
* http://mattstow.com
*
* Licensed under the MIT license
*/
(function(d,b){if(!b.addEventListener){d.mqGenie={adjustMediaQuery:function(i){return i}};return}function e(k,l){var o=k.cssRules?k.cssRules:k.media,n,p=[],j=0,m=o.length;for(j;j<m;j++){n=o[j];if(l(n)){p.push(n)}}return p}function a(i){return e(i,function(j){return j.constructor===CSSMediaRule})}function g(j){var k=d.location,i=b.createElement("a");i.href=j;return i.hostname===k.hostname&&i.protocol===k.protocol}function c(i){return i.ownerNode.constructor===HTMLStyleElement}function f(i){return i.href&&g(i.href)}function h(){var n=b.styleSheets,k,m=n.length,j=0,l=[];for(j;j<m;j++){k=n[j];if(f(k)||c(k)){l.push(k)}}return l}b.addEventListener("DOMContentLoaded",function(){d.mqGenie=(function(){var r=b.documentElement;r.style.overflowY="scroll";var l=d.innerWidth-r.clientWidth,s={adjusted:l>0,fontSize:parseFloat(d.getComputedStyle(r).getPropertyValue("font-size")),width:l,adjustMediaQuery:function(j){if(!mqGenie.adjusted){return j}var i=j.replace(/\d+px/gi,function(w){return parseInt(w,10)+mqGenie.width+"px"});i=i.replace(/\d.+?em/gi,function(w){return((parseFloat(w)*mqGenie.fontSize)+mqGenie.width)/mqGenie.fontSize+"em"});return i}};if(s.adjusted){if("WebkitAppearance" in r.style){var k=/Chrome\/(\d*?\.\d*?\.\d*?\.\d*?)\s/g,q=navigator.userAgent.match(k),u;if(q){q=q[0].replace(k,"$1");u=q.split(".");u[0]=parseInt(u[0]);u[2]=parseInt(u[2]);u[3]=parseInt(u[3]);if(u[0]<=29){if(u[0]===29&&u[2]<1548&&u[3]<57){s.adjusted=false}else{if(u[0]<29){s.adjusted=false}}}}else{s.adjusted=false}if(!s.adjusted){return s}}var t=h(),m=t.length,p=0,n,v;for(p;p<m;p++){n=a(t[p]);v=n.length;for(var o=0;o<v;o++){n[o].media.mediaText=n[o].media.mediaText.replace(/m(in|ax)-width:\s*(\d|\.)+(px|em)/gi,function(i){if(i.match("px")){return i.replace(/\d+px/gi,function(j){return parseInt(j,10)+s.width+"px"})}else{return i.replace(/\d.+?em/gi,function(j){return((parseFloat(j)*s.fontSize)+s.width)/s.fontSize+"em"})}})}}}return s})()})})(window,document);