Skip to content

Instantly share code, notes, and snippets.

@jiripudil
Created September 5, 2012 18:52
Show Gist options
  • Save jiripudil/3642472 to your computer and use it in GitHub Desktop.
Save jiripudil/3642472 to your computer and use it in GitHub Desktop.
Mobile-first responsive images using jQuery
<img src="/mobile/version.jpg" data-tablet-src="/tablet/version.jpg" data-full-src="/full/version.jpg" alt="Picture" class="responsive">
var w = window.document.width,
sizeTablet = 768,
sizeFull = 1024;
$(document).ready(function() {
$('img.responsive').each(function(){
if (w > sizeFull) {
// serve full size for large screens
this.src = $(this).attr('data-full-src');
} else if (w > sizeTablet) {
// serve middle size for tablets
this.src = $(this).attr('data-tablet-src');
} else {
// wrap image with link to full size on small screens
var a = $('<a>');
a.attr('href', $(this).attr('data-full-src'));
$(this).wrap(a);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment