Skip to content

Instantly share code, notes, and snippets.

@dpk
Created June 17, 2015 10:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dpk/2080cf081de403210087 to your computer and use it in GitHub Desktop.
Save dpk/2080cf081de403210087 to your computer and use it in GitHub Desktop.
if (('devicePixelRatio' in window && window.devicePixelRatio >= 1.5) ||
('matchMedia' in window && window.matchMedia("(-webkit-min-device-pixel-ratio: 1.5), (min--moz-device-pixel-ratio: 1.5), (-o-min-device-pixel-ratio: 3/2), (min-resolution: 1.5dppx)").matches))
{
document.addEventListener('DOMContentLoaded', function() {
var elements = document.getElementsByClassName('retina');
for (var i = 0; i < elements.length; i++) {
var element = elements[i];
if (element.tagName !== 'IMG') continue;
var match = element.src.match(/^(.+)\.(.+?)$/);
if (match) {
element.src = match[1] + '@2x.' + match[2];
} else {
element.src = element.src + '@2x';
}
element.onload = function(event) {
event.target.width = event.target.naturalWidth / 2;
};
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment