Skip to content

Instantly share code, notes, and snippets.

@jonsuh
Last active August 29, 2015 14:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonsuh/25eecda57806ef0ee51f to your computer and use it in GitHub Desktop.
Save jonsuh/25eecda57806ef0ee51f to your computer and use it in GitHub Desktop.
Retina image replacement
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"));
});
}
<style>
img.no-js {
display: none;
}
</style>
<img src=""
data-1x="/images/photo.jpg"
data-2x="/images/photo@2x.jpg"
class="no-js">
<noscript>
<img src="/images/photo.jpg">
</noscript>
<img src=""
data-1x="/images/photo.jpg"
data-2x="/images/photo@2x.jpg">
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;
}
}
};
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;
}
}
};
<img src="photo.jpg"
srcset="photo.jpg 1x,
photo@2x.jpg 2x,
photo-small.jpg 480w 1x,
photo-small@2x.jpg 480w 2x">
<img src="photo.jpg"
srcset="photo.jpg 1x, photo@2x.jpg 2x">
// 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