Skip to content

Instantly share code, notes, and snippets.

@lionhurt
Created March 28, 2018 14:36
Show Gist options
  • Save lionhurt/6b1c3a39fbec8c245ca3fd84de949fc6 to your computer and use it in GitHub Desktop.
Save lionhurt/6b1c3a39fbec8c245ca3fd84de949fc6 to your computer and use it in GitHub Desktop.
(function($) {
if (isRetina) { //check if current device is retina
$('img[data-hidpi]').each(function() { // loop through images with data-hidpi attribute
$(this).attr('src', $(this).attr('data-hidpi')); //replace src attribute value with big image
});
}
})(jQuery);
function isRetina(){
return ((window.matchMedia && (window.matchMedia('only screen and (min-resolution: 192dpi), only screen and (min-resolution: 2dppx), only screen and (min-resolution: 75.6dpcm)').matches || window.matchMedia('only screen and (-webkit-min-device-pixel-ratio: 2), only screen and (-o-min-device-pixel-ratio: 2/1), only screen and (min--moz-device-pixel-ratio: 2), only screen and (min-device-pixel-ratio: 2)').matches)) || (window.devicePixelRatio && window.devicePixelRatio >= 2)) && /(iPad|iPhone|iPod)/g.test(navigator.userAgent);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment