Skip to content

Instantly share code, notes, and snippets.

@alycda
Last active December 16, 2015 21:29
Show Gist options
  • Select an option

  • Save alycda/5500060 to your computer and use it in GitHub Desktop.

Select an option

Save alycda/5500060 to your computer and use it in GitHub Desktop.
JavaScript: retina image replacement. #sublime
// http://css3.bradshawenterprises.com/blog/retina-image-replacement-for-new-ipad/
// Set pixelRatio to 1 if the browser doesn't offer it up.
var pixelRatio = !!window.devicePixelRatio ? window.devicePixelRatio : 1;
// Rather than waiting for document ready, where the images
// have already loaded, we'll jump in as soon as possible.
$(window).on("load", function() {
if (pixelRatio > 1) {
$('img').each(function() {
// Very naive replacement that assumes no dots in file names.
$(this).attr('src', $(this).attr('src').replace(".","@2x."));
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment