Skip to content

Instantly share code, notes, and snippets.

@ArtemSites
Last active July 3, 2019 07:42
Show Gist options
  • Save ArtemSites/111519956f9ad974ae2e35a59fdea9cc to your computer and use it in GitHub Desktop.
Save ArtemSites/111519956f9ad974ae2e35a59fdea9cc to your computer and use it in GitHub Desktop.
Lazy Load img video audio iframe
jQuery(function() {
lazyLoad($('body'));
});
function lazyLoad($content) {
$content.find('img[data-src], source[data-src], audio[data-src], iframe[data-src]').each(function(){
$(this).attr('src', $(this).data('src'));
$(this).removeAttr('data-src');
if($(this).is('source')){
$(this).closest('video').get(0).load();
}
});
}
<video width="320" height="240" controls>
<source data-src="movie.mp4" type="video/mp4">
<source data-src="movie.ogg" type="video/ogg">
Your browser does not support the video tag.
</video>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment