Skip to content

Instantly share code, notes, and snippets.

@csytan
Created April 19, 2011 07:31
Show Gist options
  • Save csytan/926966 to your computer and use it in GitHub Desktop.
Save csytan/926966 to your computer and use it in GitHub Desktop.
Lazy loads videos as you scroll. Place the iframe src in a "data-embed" attribute. Use with jQuery.
$(document).ready(function(){
var vid_links = $('a.video');
function showVideos(){
if (!vid_links.length) return;
var window_bottom = $(window).scrollTop() + $(window).height() + 500;
vid_links.each(function(){
var link = $(this);
if (link.offset().top < window_bottom){
link.replaceWith(
'<iframe src="' + link.attr('data-embed') + '" class="video" frameborder="0"></iframe>');
vid_links = vid_links.slice(1);
}
});
}
showVideos();
$(window).scroll(showVideos);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment