Skip to content

Instantly share code, notes, and snippets.

@Takazudo
Created February 20, 2014 08:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Takazudo/9109198 to your computer and use it in GitHub Desktop.
Save Takazudo/9109198 to your computer and use it in GitHub Desktop.
devicePixelRatio className tweak
/**
* `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