Created
August 1, 2013 20:21
-
-
Save ThrivingKings/6134886 to your computer and use it in GitHub Desktop.
Use JavaScript and CSS to quickly upgrade your images for Retina displays (Retinaizer)
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
div { | |
background: url('/img/background.jpg'); | |
} | |
@media only screen and (-webkit-min-device-pixel-ratio: 2) { | |
div { | |
background-image: url('/img/[email protected]'); | |
background-size: 100px 100px; | |
// This should match the dimensions of the original resolution image | |
} | |
} |
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
var Retina = (typeof window.devicePixelRatio != 'undefined' ? window.devicePixelRatio > 1 : false); | |
if(Retina) { | |
var $img = document.getElementsByTagName("img"); | |
var i = $img.length; | |
var pattern = /\.[jpg|jpeg|png]+$/i; | |
var retinaizer = "@2x"; | |
while(i--) { | |
var _src = $img[i].src; | |
var _h = $img[i].height, _w = $img[i].width; | |
if(_src && _src.match(pattern)) { | |
var _ext = _src.match(pattern); | |
_src = _src.replace(pattern, retinaizer+_ext); | |
$img[i].src = _src; | |
$img[i].height = _h+"px"; | |
$img[i].width = _w+"px"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment