Skip to content

Instantly share code, notes, and snippets.

@etyp
Forked from jimimaher/gist:d8b0c901f111474c69f6
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save etyp/8dc802969495cdf8a6b5 to your computer and use it in GitHub Desktop.
Save etyp/8dc802969495cdf8a6b5 to your computer and use it in GitHub Desktop.
// example HTML:
// <img class="background-image" src="images/mobile/everydaycare.jpg">
// Description: Serve mobile image first, and swap directory if page is desktop
function swapImageMobileFirst() {
var $bgImages = $('img.background-image');
var loadCount = 0;
$bgImages.each(function(){
// Get image src
var src = $(this).attr('src');
// If mobile, use mobile image
if (screenWidthValue === "mobile") {
var res = str.replace("desktop", "mobile");
}
// If not mobile, use desktop image
else {
var res = str.replace("mobile", "desktop");
}
// Set new src
$(this).attr('src',res);
// On load, increase loadCount
$(this).on('load',function(){
loadCount++;
//after all images are loaded, do stuff
if (loadCount === $bgImages.length){
postImageLoad();
alert('all '+loadCount+' images are loaded.');
}
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment