Skip to content

Instantly share code, notes, and snippets.

@gerbenvandijk
Created December 9, 2013 09:49
Embed
What would you like to do?
Simple retina <img>
<img src="myimage.jpg" data-2x="myimage-2x.jpg">
if(window.devicePixelRatio >= 1.2){
var elems = document.getElementsByTagName('*');
for(var i=0;i < elems.length;i++){
var attr = elems[i].getAttribute('data-2x');
if(attr && elems[i].tagName == 'IMG'){
elems[i].src = attr;
} else if(attr){
elems[i].style.cssText += 'background-image: url('+attr+')';
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment