Skip to content

Instantly share code, notes, and snippets.

@joho
Created June 7, 2013 05:18
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 joho/5727195 to your computer and use it in GitHub Desktop.
Save joho/5727195 to your computer and use it in GitHub Desktop.
// LAZY IMAGE LOADING FOR RESPONSIVE
var lazyImageLoad = function () {
$('.lazy-image-container').each(function (index) {
var thisElement,
noScriptElement,
imgElement;
thisElement = $(this);
if (!thisElement.parent().is(':hidden')) {
noScriptElement = thisElement.find("noscript");
if (noScriptElement.size() > 0) {
thisElement.append(noScriptElement.text());
noScriptElement.remove();
}
thisElement.removeClass("lazy-image-container");
}
});
}
// fire once on page load. no need to wait for domReady because we
// load this script at the end of the page
lazyImageLoad();
// register the function to run every time a new breakpoint matches
// on a media query. the timeout is important as you need to wait
// for a browser repaint to check visibility of elements.
if (typeof(metaQuery) !== 'undefined') {
metaQuery.bindGlobal(function () {
setTimeout(lazyImageLoad, 0);
});
}
<a href="#" class="lazy-image-container">
<!--[if gt IE 8]><!--><noscript><!--<![endif]-->
<img alt="some alt" src="some_image.jpg" />
<!--[if gt IE 8]><!--></noscript><!--<![endif]-->
</a>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment