Skip to content

Instantly share code, notes, and snippets.

@yellowberri-snippets
Last active August 29, 2015 13:55
Show Gist options
  • Select an option

  • Save yellowberri-snippets/8744696 to your computer and use it in GitHub Desktop.

Select an option

Save yellowberri-snippets/8744696 to your computer and use it in GitHub Desktop.
JS: Lazy Load Background Images
// Requires imagesLoaded.js
// https://github.com/desandro/imagesloaded
$('html').imagesLoaded( function() {
var mobileWidth = 600;
var i = 0;
var selector = '';
var bgUrl = '';
if ( $(window).width() > mobileWidth ) {
selector = 'll-bg';
} else {
selector = 'll-bg-mobile';
}
// Return all background with ll-bg attribute.
var llBackgrounds = $('*').filter( function() {
return $(this).data( selector ) !== undefined;
});
console.log(llBackgrounds);
llBackgrounds.each(function(index) {
bgUrl = '';
bgUrl = "url('" + $(this).attr('data-' + selector) + "')";
$(this).css('background-image', bgUrl);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment