Last active
August 29, 2015 14:24
-
-
Save jimimaher/d8b0c901f111474c69f6 to your computer and use it in GitHub Desktop.
Image Swapper
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
// 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