Skip to content

Instantly share code, notes, and snippets.

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