Created
March 23, 2012 15:17
-
-
Save donovanh/2171648 to your computer and use it in GitHub Desktop.
Retina Article: Image tag reference
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
.logo { | |
background:url('/your_logo.png'); | |
height:50px; | |
width:150px; | |
} | |
@media only all and (-webkit-min-device-pixel-ratio: 2) { | |
/* Media query for double density devices */ | |
.logo { | |
background:url(/your_larger_logo.png); /* This is your double size image */ | |
background-size:150px 50px; /* Make sure the double size image is displayed within the original size */ | |
} | |
} |
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
<img src="http://www.gravatar.com/avatar/068ce5abf934fd1ae13067300a36d160?s=125" class="hi-res-available" data-rel="http://www.gravatar.com/avatar/068ce5abf934fd1ae13067300a36d160?s=250" width="125"> |
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
// Query the device pixel ratio. | |
//------------------------------- | |
function getDevicePixelRatio() { | |
if(window.devicePixelRatio === undefined) return 1; // No pixel ratio available. Assume 1:1. | |
return window.devicePixelRatio; | |
} | |
// Process all document images marked as highres-available | |
// If the screen is (approx) 800 or bigger across (high-res tablet upwards) | |
//----------------------------- | |
function processTaggedImages() { | |
if (getDevicePixelRatio() > 1 && $(window).width() >= 700) { | |
$('img.high-res-available').each(function(i, image) { | |
// Set the src to that in the data-rel | |
var thisDataRel = $(this).attr('data-rel'); | |
if (thisDataRel) $(this).attr('src', thisDataRel); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment