Created
February 20, 2014 08:31
-
-
Save Takazudo/9109198 to your computer and use it in GitHub Desktop.
devicePixelRatio className tweak
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* `html` class tweaking | |
* if `devicePixelRatio` was 2, switch html's class | |
* from `dpr-1` to `dpr-2` | |
*/ | |
(function() { | |
var html = document.getElementsByTagName("html")[0]; | |
/* helpers */ | |
var hasClass = function(node, cls) { | |
return new RegExp("(\\s|^)" + cls + "(\\s|$)").test(node.className); | |
}; | |
var addClass = function(node, cls) { | |
if (hasClass(node, cls)) { | |
return; | |
} | |
return node.className = "" + node.className + " " + cls; | |
}; | |
var removeClass = function(node, cls) { | |
if (!hasClass(node, cls)) { | |
return; | |
} | |
node.className = node.className | |
.replace(new RegExp("(\\s|^)" + cls + "(\\s|$)"), " ") | |
.replace(/\s+/g, " ").replace(/^\s|\s$/, ""); | |
}; | |
/* tweak `html` className */ | |
(function() { | |
var dpr = window.devicePixelRatio; | |
if (!dpr) { | |
return; | |
} | |
if (dpr >= 1.5) { | |
removeClass(html, 'dpr-1'); | |
addClass(html, 'dpr-2'); | |
} | |
}()); | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment