-
-
Save etyp/8dc802969495cdf8a6b5 to your computer and use it in GitHub Desktop.
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 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