Last active
August 29, 2015 14:03
-
-
Save jonsuh/25eecda57806ef0ee51f to your computer and use it in GitHub Desktop.
Retina image replacement
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 $images = $("img[data-1x]"); | |
if (window.devicePixelRatio == 2) { | |
$.each($images, function() { | |
var $this = $(this); | |
$this.attr("src", $this.data("2x")); | |
}); | |
} else { | |
$.each($images, function() { | |
var $this = $(this); | |
$this.attr("src", $this.data("1x")); | |
}); | |
} |
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
<style> | |
img.no-js { | |
display: none; | |
} | |
</style> | |
<img src="" | |
data-1x="/images/photo.jpg" | |
data-2x="/images/[email protected]" | |
class="no-js"> | |
<noscript> | |
<img src="/images/photo.jpg"> | |
</noscript> |
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="" | |
data-1x="/images/photo.jpg" | |
data-2x="/images/[email protected]"> |
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 = Retina || {}; | |
Retina = { | |
init: function() { | |
var $images = $("img[data-1x]"); | |
if (Retina.isRetina() == true) { | |
$.each($images, function() { | |
var $this = $(this); | |
$this.attr("src", $this.data("2x")); | |
}); | |
} else { | |
$.each($images, function() { | |
var $this = $(this); | |
$this.attr("src", $this.data("1x")); | |
}); | |
} | |
}, | |
isRetina: function() { | |
if (window.devicePixelRatio == 2) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
}; |
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 = Retina || {}; | |
Retina = { | |
init: function() { | |
var images = document.querySelectorAll("img[data-1x]"); | |
if (Retina.isRetina() == true) { | |
Array.prototype.forEach.call(images, function(el, i) { | |
var src = el.getAttribute("data-2x"); | |
el.setAttribute("src", src); | |
}); | |
} else { | |
Array.prototype.forEach.call(images, function(el, i) { | |
var src = el.getAttribute("data-1x"); | |
el.setAttribute("src", src); | |
}); | |
} | |
}, | |
isRetina: function() { | |
if (window.devicePixelRatio == 2) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
}; |
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="photo.jpg" | |
srcset="photo.jpg 1x, | |
[email protected] 2x, | |
photo-small.jpg 480w 1x, | |
[email protected] 480w 2x"> |
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="photo.jpg" | |
srcset="photo.jpg 1x, [email protected] 2x"> |
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
// Select all images with `data-1x` attribute | |
// (It assumes that if you have `data-1x` attribute set, | |
// it also has `data-2x` attribute set as well) | |
var images = document.querySelectorAll("img[data-1x]"); | |
// If the device is retina-enabled (has pixel ratio of 2) | |
if (window.devicePixelRatio == 2) { | |
// Loop through the array `images` | |
Array.prototype.forEach.call(images, function(el, i) { | |
// Get the value of the `data-2x` attribute | |
var src = el.getAttribute("data-2x"); | |
// Set var `src` as the value of the element's `src` attribute | |
el.setAttribute("src", src); | |
}); | |
// Else the device is not retina-enabled | |
} else { | |
// Loop through the array `images` | |
Array.prototype.forEach.call(images, function(el, i) { | |
// Get the value of the `data-1x` attribute | |
var src = el.getAttribute("data-1x"); | |
// Set var `src` as the value of the element's `src` attribute | |
el.setAttribute("src", src); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment